Plugin-php: Multiline arrays collapsed into a single-line array

Created on 20 Jul 2018  路  6Comments  路  Source: prettier/plugin-php

Prettier should only inline an array definition if the source is already on a single line. This is how it works in JS.

Input/Expected:

$paginate = ['limit' => 32, 'order' => ['Competitions.publish_date' => 'DESC']];

$paginate = [
    'limit' => 32,
    'order' => ['Competitions.publish_date' => 'DESC']
];

$paginate = [
    'limit' => 32,
    'order' => [
        'Competitions.publish_date' => 'DESC'
    ]
];

Output:

$paginate = ['limit' => 32, 'order' => ['Competitions.publish_date' => 'DESC']];

$paginate = ['limit' => 32, 'order' => ['Competitions.publish_date' => 'DESC']];

$paginate = ['limit' => 32, 'order' => ['Competitions.publish_date' => 'DESC']];

JS prettier reference:

https://prettier.io/playground/#N4Igxg9gdgLgprEAuEASADgQwOYEsqbwAEAvEcEQDa4C2uMSRAzAEwA0REATgCZxeMKAHRABhCDXRwY9XNADOAOnQBXAEbV5ACwD6PQnBGMRAEQCiAZVEiiAXzsBuIVGcYc+A6XLOhManQZmdh8Ybj4BciIRcUlpWQVldU1dfXgjKJBzKxtbZ1snFyg3PAJiMmAQ-3pGVjYQsP5BZyIW1uiJKRkZBNUNXG09A3TTS2sQZtbcqHyfKBA2EAh0bqh5ZFBMLi4IAHcABU2ENZRMADcIXB55kDUuTDAAa2kLLDB8bGQYLhU4Bfx5fgwPZ3bA0TDIABmmEoAIWACt5AAPABCd0ez0wNDgABl8HBIdDYSAsFwAVxkDdMGoAJ6UaDXdDbAEAdTu6ApjLgZNO+IWXDgAEcVLh+cCcGCCTDfiAAXRPt9pfJ3pQ4ABFFQQeCSokwKnMy4wLTIdggL6YXDUKDYGISlBQaC8kAqAEAFSpxy+PwWjPwMH1PENyAAjCwAAy2WxAA

ugly

Most helpful comment

Not required, it is easy implement

All 6 comments

Hum. To be sure, you're saying that the output is wrong, and that youre input shouln't be touched?

I don't think that it's in the spirit of prettier (several ways to write the same thing), but more in the linters one.

And for me, the best option of refactoring in your examples is the second one.

Yes, in fact this is exactly how prettier works in javascript (look at the example link above)

Object looks like array in PHP, but not same
/cc @mgrip @czosel what do you think about this?

I think associative arrays in PHP can be treated the same way as objects in JS. Even though respecting the input formatting makes our implementation more complicated, I'd be in favor of following prettier for JS in this regard once more.
(Plus, we can probably copy a big part of the implementation from JS?)

Not required, it is easy implement

One more thing to consider when implementing this, it also applies to single dimensional associative arrays broken over multiple lines.

Input/Expected:

$paginate = ['limit' => 32, 'order' => 'DESC'];

$paginate = [
    'limit' => 32,
    'order' => 'DESC'
];

Example JS:

https://prettier.io/playground/#N4Igxg9gdgLgprEAuEASADgQwOYEsqbwAEAvEcEQDa4C2uMSRAzAEwA0REATgCZxeMAOiAAiAUQDKAYWFEAvgG5BUZRhz5CcUuWVEqteo1YddnXvyGjJMkMsXKQbEBHQxc0AM7JQmLlwgA7gAKvgheKJgAbhC4PI4gAEZcmGAA1nAwElhg+NjIMFwArnBO+B78MEHJ2DSYyABmmJTlTgBWHgAeAELJaRkSmDRwADL4cA1NLSBYXOVcyImYCQCelNDx6Fz4MADqsTAAFsgAjCwADE6bEOU7yegLm3BzkeNOXHAAjoW471U4tRNmiUQOU6PkisCPLlKHAAIqFCDwQFTGBLPY8Q7IdggAqYXDUKDYKQQGgAlBQaCvECFcoAFSW4QKxTkciAA

Was this page helpful?
0 / 5 - 0 ratings