Php_codesniffer: Issue checking alternative control structures syntax

Created on 25 Mar 2016  Â·  4Comments  Â·  Source: squizlabs/PHP_CodeSniffer

<div>
    <?php if (true): ?>
      hello!
    <?php else : ?>
      bye
    <?php endif ?>
</div>
vagrant@vagrant-centos65 /vagrant/dev.100sp phpcs --standard=PSR1 test.php
# no errors, all's ok
vagrant@vagrant-centos65 /vagrant/dev.100sp phpcs --standard=PSR2 test.php

FILE: /vagrant/dev.100sp/test.php
----------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
 2 | ERROR | [x] Expected 1 space after closing parenthesis; found 0
----------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

Time: 25ms; Memory: 4Mb
vagrant@vagrant-centos65 /vagrant/dev.100sp phpcs test.php

FILE: /vagrant/dev.100sp/test.php
----------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 1 LINE
----------------------------------------------------------------------
 2 | ERROR | [ ] Missing file doc comment
 2 | ERROR | [x] There must be a single space between the closing
   |       |     parenthesis and the opening brace of a multi-line IF
   |       |     statement; found 0 spaces
----------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

Time: 19ms; Memory: 4Mb

phpcbf fixes file to

<div>
    <?php if ($a) : ?>
      hello!
    <?php else : ?>
      bye
    <?php endif ?>
</div>

Expected result:

  1. Don't touch and error if ($a):, it's ok already
  2. Touch and error else :

PSR1 and PSR2 don't say anything about such syntax. But case syntax errors on case 'expr' :.

Awaiting Feedback

All 4 comments

PSR1 and PSR2 don't say anything about such syntax

The alternate syntax literally isn't in the PSR1/2 standard.
If anything the sniff should flag "alternative syntax is not allowed / use brackets"

It's absurd to complain "alternative syntax is not allowed". How would you
write templates?
On Mar 29, 2016 07:58, "Brad Kent" [email protected] wrote:

PSR1 and PSR2 don't say anything about such syntax

The alternate syntax literally isn't in the PSR1/2 standard.
If anything the sniff should flag "alternative syntax is not allowed / use
brackets"

—
You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub
https://github.com/squizlabs/PHP_CodeSniffer/issues/935#issuecomment-202596914

@b1rdex

I have never used the alternative syntax. It certainly isn't required for templates.
Templates without alternate syntax:

  • twig
  • Nothing special about alternate syntax
<?php if ($this) { ?>
    <h1>This</h2>
<?php } else { ?>
    <h1>That</h1>
<?php } ?>
<p><?=$myParagraph?></p>

I prefer to keep all logic out of templates.
But this isn't about my preference... It's about PSR-1/2
https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md
Section 5 - Control Structures

The body of each structure MUST be enclosed by braces

It also says

Code MUST use 4 spaces for indenting, not tabs

Which I find very discriminatory against tabs and disable that sniff.

Stackoverflow re pros/cons of the alternate syntax (nutshell, it's coding preference)

Long story short. The PSR2 sniffs should complain about alternate syntax. Up to the user to disable/ignore said sniff.

I don't see the problem here. PSR-2 clearly states There MUST be one space between the closing parenthesis and the opening brace for control structures, which is what this sniff is enforcing.

Because you have chosen to use the alternate syntax, PHPCS sees the : as the opening brace.

Because PSR-2 does not define how alternate syntax is supposed to be written, the only options available to me are:

  1. Ban it completely based on the line 'The body of each structure MUST be enclosed by braces' as alternate syntax clearly doesn't use braces.
  2. Ignore it completely and just pretend it doesn't exist.
  3. Enforce the PSR-2 rules for alternate syntax where possible.

I've obviously chosen point 3. Point 2 is actually very hard to do because PHPCS itself supports alternate syntax and sets the open and close tokens in a way that lets the alternate syntax look the same as normal syntax. This lets generic sniffs support both without any additional effort. As PSR-2 uses these sniffs, I would instead need to write custom sniffs for PSR-2 that performed the same checks on normal syntax but ignored alternate syntax. It doesn't seem worth it, especially given the following code is clearly against PSR-2 in 3 other ways and so should produce errors:

<div>
    <?php if( $expression ): ?>
      hello!
    <?php endif ?>
</div>

So my summary is: I think everything is working as intended. Because PSR-2 doesn't define how alternate syntax should be used, all I can really do is apply as many of the existing rules as possible, which is what PHPCS and PHPCBF are doing here.

You disagree?

Was this page helpful?
0 / 5 - 0 ratings