Hi Marcus,
On 13 February 2014 13:38, Maurus Cuelenaere <
mcuel...@gmail.com> wrote:
> Hi all,
>
> if I understand the PSR-2 correctly, the following code is valid PSR-2:
>
>> if (
>> $foo === "bar" &&
>> $bar === "foo"
>> ) {
>> // ...
>> }
>
>
> However, PHP CodeSniffer thinks differently about this and reports it as an
> error:
>
>>
>> --------------------------------------------------------------------------------
>> FOUND 1 ERROR(S) AFFECTING 1 LINE(S)
>>
>> --------------------------------------------------------------------------------
>> 1 | ERROR | Expected 0 spaces after opening bracket; 1 found
>>
>> --------------------------------------------------------------------------------
>
> ( tracking issue at
https://github.com/squizlabs/PHP_CodeSniffer/pull/188 )
>
> Is this code snippet valid PSR-2 and if not, how should this construct be
> expressed?
Control structures are covered by Section 5 of PSR-2. The rules state:
- There MUST be one space after the control structure keyword
- There MUST NOT be a space after the opening parenthesis
- There MUST NOT be a space before the closing parenthesis
- There MUST be one space between the closing parenthesis and the opening brace
- The structure body MUST be indented once
- The closing brace MUST be on the next line after the body
The word “space” is not defined in rule 2 or 3, but can only be taken
literally in rule 4, so it must be taken literally for all rules. The
standard does not, therefore, preclude your example in my opinion.
> PS: I know it is frowned upon to have complex conditions in control
> structures, but I'm not looking to solve that issue.
Yes, it is frowned on ;). It’s generally more readable to extract them
into a separate variable assignment or format starting on the same
line as the starting parenthesis which is the generic rule
PHP_CodeSniffer is falling back on I presume. That is irrelevant in
this case since PSR-2 is effectively silent on how to format control
statement conditions over multiple lines.
If Greg doesn’t buy the obvious explanation, the only other way to get
PHP_CodeSniffer changed is to have another PSR-2 errata voted for by
PHP-FIG to formalise its position on this particular case.
Paddy
--
Pádraic Brady
http://blog.astrumfutura.com
http://www.survivethedeepend.com
Zend Framework Community Review Team
Zend Framework PHP-FIG Representative