What are the main differences between the PSR-2 coding standard and the Symfony2 code standard for phpcs? What are the main differences between the PSR-2 coding standard and the Symfony2 code standard for phpcs? symfony symfony

What are the main differences between the PSR-2 coding standard and the Symfony2 code standard for phpcs?


The Symfony coding standards basically extend the PSR standards. PSR-2 doesn't specify rules for all situations, so the Symfony standards add some rules on top of PSR-2.

As you said the PHP CodeSniffer repository doesn't include a ruleset for Symfony. There are multiple third-party implementations of a Symfony2 ruleset, so the exact list of differences between them and the PSR-2 ruleset depends on the implementation you choose.

However, when looking to the differences between the standards of Symfony and PSR (not the rulesets for PHP CodeSniffer), some additions by the Symfony standards are:

  • Not adding spaces around the concatenation operator (.)
  • Adding a comma after each array item in a multi-line array, even after the last one
  • Adding a blank line before return statements (unless the return is alone inside a statement-group (like an if statement))
  • Declaring class properties before methods
  • Declaring public members first, then protected, then private (except class constructors and the setUp and tearDown methods for PHPUnit classes)

These are just some examples, take a look at the Symfony coding standards for the full list.

In my opinion the Symfony standards make sense and I try to use them whenever I can, even outside of Symfony projects.


From PhpStorm 2017.1. On the left Symfony, on the right PSR-2

  • Complex parameters in new lines.

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

But at Symfony site stands opposite rule:

Declare all the arguments on the same line as the method/function name, no matter how many arguments there are;

  • Not adding spaces around the concatenation operator

enter image description here

  • Adding a comma after each array item in a multi-line array, even after the last one

enter image description here

  • Adding a blank line before return statements (unless the return is alone inside a statement-group (like an if statement))

enter image description here

enter image description here