Php-cs-fixer: [Proposal] Array Items on Separate Lines Fixer

Created on 26 May 2015  路  11Comments  路  Source: FriendsOfPHP/PHP-CS-Fixer

I often find myself putting array items on separate lines. Specially when there are a lot of items in the arrays. Maybe we could add a fixer for that? Maybe it exists already? Example below:

// Before
protected $dates = ['deleted_at', 'ended_at', 'started_at'];

// After
protected $dates = [ 
    'deleted_at', 
    'ended_at', 
    'started_at'
];

What do you think?

kinfeature request

Most helpful comment

Do people always want this everywhere in there code though?

I think just simply adding it as an option adds no harm. Can always improve it with configurable settings after the fact. Maybe you don't want it everywhere in the code, but maybe some people do. This proposal seems to have been sitting here now for three years without any movement, but I'd love to see something come of this.

All 11 comments

Do people always want this everywhere in there code though? This fixer would be very annoying for the following:

$factory->render('foo', ['bar'], false);

as it would end up:

$factory->render('foo', [
    'bar'
], false);

@GrahamCampbell That is true. Maybe it should only apply to variables then?

+1 for variables

I'd love to see this in but as mentioned, only for properties and variables. Rest I prefer to do by hand

I'm looking for similar fixer, probably configurable.

// no keys, one line
$elements = [$path, $name]; 

// keys
$elements = [ 
    'file' => $path,
    'label' => $name
];

I'd appreciate starting points on fixer architecture with arrays, so I could make this happen.

If anyone is interested in this fixer, I made similar prototype: https://github.com/Symplify/CodingStandard/blob/master/README.md#indexed-php-arrays-should-have-1-item-per-line

feel free to send a PR

Not in my prirority list Now.

Do people always want this everywhere in there code though?

@GrahamCampbell Well, don't use it. :trollface:

More seriously, we should add a min_items option, like for this eslint rule: https://eslint.org/docs/rules/array-element-newline

BTW, we may done the same fixer for function signature parameters.

Do people always want this everywhere in there code though?

I think just simply adding it as an option adds no harm. Can always improve it with configurable settings after the fact. Maybe you don't want it everywhere in the code, but maybe some people do. This proposal seems to have been sitting here now for three years without any movement, but I'd love to see something come of this.

Is adding a configuration to WhitespaceAfterCommaInArrayFixer instead of creating a new fixer good idea? This way two fixers won't conflict. Even the name fits, since newline is also a whitespace.

Was this page helpful?
0 / 5 - 0 ratings