Search code examples
laravelpint

How with laravel/pint to make keys and values are aligned with spaces on the same level?


In Laravel 9 app using laravel/pint (1.4) with "psr12" preset I prefer to see not code :

\DB::table('quizzes')->insert([
    'id' => 2,
    'question' => 'What does ORM stand for?',
    'quiz_category_id' => 1,
    'points' => 4,
    'active' => true,
]);

But:

\DB::table('quizzes')->insert([
    'id'               => 2,
    'question'         => 'What does ORM stand for?',
    'quiz_category_id' => 1,
    'points'           => 4,
    'active'           => true,
]);

where keys and values are aligned with spaces on the same level.

I tried to fix it and found property “binary_operator_spaces”

But looking at docs : https://github.com/laravel/pint/blob/main/resources/presets/laravel.php

I see only 1 rule :

'binary_operator_spaces' => [
    'default' => 'single_space',
],

Not sure, but how can I set this rule? Seems it does not hae any other rules...

Thanks!


Solution

  • Check the documentation for binary_operator_spaces

    You want to use that rule with the align_single_space_minimal option:

    'binary_operator_spaces' => [
        'default' => 'align_single_space_minimal',
    ],
    

    That is if you have the same setup as shown in the link you provided

    If you were using PHPCodeSniffer (which is what laravel is using behind the scenes) you would simply use it this way:

    $config->setRuleset([
        'Squiz.WhiteSpace.OperatorSpacing' => [
            'align_single_space_minimal' => true
        ]
    ]);
    

    Just remember that each space is 1 byte, and those add up... a pedantic-thought but one to keep in your head none-the-less (imho no one should use this rule or add pointless whitespace to array)