Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am trying to figure out which code style to enforce with the phpcs code sniffer.

Since the popularity of Symfony2, it seems to be a good practice to use its code standard. On the other hand, its code style is based upon PSR2, so this seems to be the most basic style one should use.

Furthermore, in the basic installation of squizlabs/php_codesniffer, the Symfony2 standard is not included and has to be manually installed whereas the PSR2 is easily available.

So I want to know the the main difference of the Symfony2 guideline as compared to the PSR2 in order to decide which to use.

For instance, I realized that Symfony2 style enforces that Concat operator must not be surrounded by spaces while the PSR2 ignores this case. Yet I did not find an easy way to list the differences. I looked in the rulseset.xml but it was not that clear to me how it is set up.

I am interested in how to get a complete lists of the differences between the PSR2 and Symfony2 code standard.

like image 341
k0pernikus Avatar asked Jun 15 '15 16:06

k0pernikus


People also ask

What is PSR coding?

The PHP Standard Recommendation (PSR) is a PHP specification published by the PHP Framework Interop Group. Similar to Java Specification Request for Java, it serves the standardization of programming concepts in PHP.

Is PSR 2 deprecated?

Deprecated - As of 2019-08-10 PSR-2 has been marked as deprecated.

What is coding standards in laravel?

Laravel follows the PSR-0 and PSR-1 coding standards. In addition to these standards, the following coding standards should be followed: The class namespace declaration must be on the same line as <? php .


2 Answers

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.

like image 164
Nic Wortel Avatar answered Sep 25 '22 22:09

Nic Wortel


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

like image 41
user6827096 Avatar answered Sep 24 '22 22:09

user6827096