Skip to content

Alternative syntax for control structures triggers false "multi-line if statement" error #2539

@sun

Description

@sun

Problem

Goal

  • Make correct code pass coding style tests.

Details

  • PSR-2 does not define rules for the alternative syntax for control structures. But its lack of rules for the alternative syntax should not affect the CodeSniffer's general ability to apply sensible, readable coding standards. Especially given the fact that PHPCS supports the alternate syntax already.

  • This issue affects many software projects using a PHP based templating system; for instance, WordPress, Drupal (7 and below), and others. Most of them predate PSR-2. You may not like these systems personally, but they do have their place and people are trying to use PHPCS to ensure consistent coding standards. Fixing the issue helps to increase adoption of modern tool chains.

  • This topic was brought up in similar or incomplete ways in a few issues in the past already, the closest ones were Issue checking alternative control structures syntax #935 (alternate syntax) and phpcbf with PSR2 errors on control structure alternative syntax #366 (legacy templates).

Test case

<?php

/**
 * @file
 * Test alternative syntax for control structures.
 */
?>

<?php foreach ([1, 2] as $number): ?>
    <?php echo $number ?> is a number.
<?php endforeach; ?>

<?php if ($number): ?>
    <?php echo $number ?> is a good number.
<?php else: ?>
    <?php echo $number ?> is a bad number.
<?php endif; ?>

<?php switch ($number): ?>
<?php case 1: ?>
    <?php echo $number ?> is a low number.
    <?php break; ?>

  <?php default: ?>
    <?php echo $number ?> is another number.
<?php endswitch; ?>

<?php while (--$number): ?>
    <?php echo $number ?> is a lower number.
<?php endwhile; ?>

Setup

$ composer global require "squizlabs/php_codesniffer=*"
$ composer global require 'drupal/coder:>8'
$ composer global require dealerdirect/phpcodesniffer-composer-installer
$ phpcs --version
PHP_CodeSniffer version 3.4.2 (stable) by Squiz (http://www.squiz.net)

Result

❌ Expected 1 space after closing parenthesis; found 0
❌ Expected 1 space after ELSE keyword; 0 found

$ phpcs --standard=Drupal -v alternative-syntax.php 
Registering sniffs in the Drupal standard... DONE (114 sniffs registered)
Creating file list... DONE (1 files in queue)
Changing into directory ./phpcs
Processing alternative-syntax.php [PHP => 161 tokens in 30 lines]... DONE in 9ms (9 errors, 2 warnings)

FILE: ./phpcs/alternative-syntax.php
---------------------------------------------------------------------------------------------
FOUND 9 ERRORS AND 2 WARNINGS AFFECTING 10 LINES
---------------------------------------------------------------------------------------------
  9 | ERROR   | [x] Expected 1 space after closing parenthesis; found 0
 13 | ERROR   | [x] Expected 1 space after closing parenthesis; found 0
 15 | ERROR   | [x] Expected 1 space after ELSE keyword; 0 found
 19 | ERROR   | [x] Expected 1 space after closing parenthesis; found 0
 20 | ERROR   | [x] Line indented incorrectly; expected 2 spaces, found 0
 22 | ERROR   | [x] Case breaking statements must be followed by a single blank line
 22 | ERROR   | [x] Case breaking statement indented incorrectly; expected 2 spaces, found 4
 23 | WARNING | [ ] Code after the BREAK statement on line 22 cannot be executed
 24 | WARNING | [ ] Code after the BREAK statement on line 22 cannot be executed
 28 | ERROR   | [x] Expected 1 space after closing parenthesis; found 0
 30 | ERROR   | [x] A closing tag is not permitted at the end of a PHP file
---------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 9 MARKED SNIFF VIOLATIONS AUTOMATICALLY
---------------------------------------------------------------------------------------------

Time: 78ms; Memory: 6MB

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions