0

I have a developer who wants to use comma-first code style in a PHP codebase to make it easier to comment out variables without having to move the comma around.

PHPStorm, configured to conform to PSR-2 standard, automatically reformats the following code:

<?php

$example = new Triceratops([
     'option1' => 'green'
   , 'option2' => 'purple'
   , 'option3' => 'orange' 
   , 'option4' => 'yellow' 
   , 'option5' => $userSubmittedColor 
   , 'option6' => 'cyan' 
]);

to this:

<?php

$example = new Triceratops([
    'option1' => 'green'
    ,
    'option2' => 'purple'
    ,
    'option3' => 'orange'
    ,
    'option4' => 'yellow'
    ,
    'option5' => $userSubmittedColor
    ,
    'option6' => 'cyan'
]);

Google returns many references to `comma-first coding about javascript, but I didn't find anything about using it in PHP.

Does comma-first code conflict with PSR-2? If not, how do you configure PhpStorm to use it, or would this be considered a bug?

  • 2
    "I have a developer who wants to use comma-first code style in a PHP codebase to make it easier to comment out variables without having to move the comma around". Have you tried telling them to stop being so bloody lazy? Adopting an ugly coding style to make code easier to write and edit goes completely against the principles of "make code readable, then worry about absolutely everything else". – David Arno Mar 20 '18 at 22:03
  • Why would anyone do this in PHP? You're allowed to have a comma after the last element in an array. (and soon I think will also be allowed to have a comma after the last function parameter) – bdsl Mar 20 '18 at 22:42
  • the comma is a separator (like a semi-colon) - putting it at the end of the line distinguishes it from operators like a dot or plus which go first on the next line - writing code 'comma-first' makes it look like an operator, which it isn't – HorusKol Mar 20 '18 at 22:42
  • @bdsl - that will be nice... just have to hope that javascript and JSON adopt this eventually – HorusKol Mar 20 '18 at 22:43
2

I can't find anything in PSR-2 that this would specifically violate.

In PHP, it doesn't matter if the last element of an array contains a trailing comma on the last element or not, and in fact many prefer to add it in anyway since it's one less character to type when you have to come back and add another element later. I've seen the same style your co-worker is suggesting used in other languages since they don't allow the last trailing comma, but in PHP it is unnecessary.

I also find the style makes it a bit harder to read, but since PSR-2 doesn't specifiy a preference directly, to each their own.

NB- There are other groups that have extended the PSR-2 standards that do mention this, but as of now they have not been accepted by PHP-FIG. E.g. from here I find a bit of a contradiction:

Multi-line declaration/condition/concatenation
All operators should go in the newline as first character:
$foo = 'Some String' . ' concatenated';

[...]

Multi-line arrays
Arrays that span across multiple lines can have a trailing comma to make sure that adding new rows does not change the previous row, as well.

$array = [ 'first', 'second', // Note the trailing comma ];

This coding-style could either be interpreted to lean more towards your preference or your co-worker's, but is not in any way normative.

  • 2
    "since it's one less character to type when you have to come back and add another element later." it's more so that version control doesn't report the code changing on the line before the newly added element – HorusKol Mar 20 '18 at 22:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.