Skip to content

PSR-12 not reporting error for empty lines with only whitespace #3124

@nereusz

Description

@nereusz

Describe the bug
PSR-2 includes requirement: "There MUST NOT be trailing whitespace at the end of non-blank lines.".
But in PSR-12 requirement changed to: "There MUST NOT be trailing whitespace at the end of lines.". Any line. Not only blank ones.
PHPCS seems to still use PSR-2 requirement, even if call it with PSR-12 standard.

Code sample (test.php)

<?php

// some irrelevant code
  
echo "There are only two spaces in previous line";
//  
echo "There are two slashes and two spaces in previous line";

To reproduce
Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs --standard=PSR12 -vvv test.php
  3. See error message displayed
[...]
[PHP => 15 tokens in 7 lines]... 
        *** START TOKEN PROCESSING ***
                Process token 0: T_OPEN_TAG => <?php\n
                        Processing PHP_CodeSniffer\Standards\PSR12\Sniffs\Files\OpenTagSniff... DONE in 0 seconds
                        Processing PHP_CodeSniffer\Standards\PSR12\Sniffs\Files\FileHeaderSniff... DONE in 0.0001 seconds
                        Processing PHP_CodeSniffer\Standards\PSR1\Sniffs\Files\SideEffectsSniff... DONE in 0.0001 seconds
                        Processing PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\DisallowAlternativePHPTagsSniff... DONE in 0 seconds
                        Processing PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\DisallowShortOpenTagSniff... DONE in 0 seconds
                        Processing PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineEndingsSniff... DONE in 0 seconds
                        Processing PHP_CodeSniffer\Standards\PSR2\Sniffs\Files\EndFileNewlineSniff... DONE in 0 seconds
                        Processing PHP_CodeSniffer\Standards\PSR2\Sniffs\Files\ClosingTagSniff... DONE in 0 seconds
                        Processing PHP_CodeSniffer\Standards\Generic\Sniffs\Files\LineLengthSniff... DONE in 0.0001 seconds
                        Processing PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\SuperfluousWhitespaceSniff... DONE in 0 seconds
                        Processing PHP_CodeSniffer\Standards\Generic\Sniffs\WhiteSpace\ScopeIndentSniff... DONE in 0.0002 seconds
                        Processing PHP_CodeSniffer\Standards\Generic\Sniffs\WhiteSpace\DisallowTabIndentSniff... DONE in 0.0001 seconds
                Process token 1: T_WHITESPACE => \n
                        Processing PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\SuperfluousWhitespaceSniff... DONE in 0 seconds
                Process token 2: T_COMMENT => //·some·irrelevant·code\n
                        Processing PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\SuperfluousWhitespaceSniff... DONE in 0 seconds
                Process token 3: T_WHITESPACE => ··\n
                        Processing PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\SuperfluousWhitespaceSniff... DONE in 0 seconds
                Process token 4: T_ECHO => echo
                        Processing PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\LowerCaseKeywordSniff... DONE in 0 seconds
                Process token 5: T_WHITESPACE => ·
                        Processing PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\SuperfluousWhitespaceSniff... DONE in 0 seconds
                Process token 6: T_CONSTANT_ENCAPSED_STRING => "There·are·only·two·spaces·in·previous·line"
                Process token 7: T_SEMICOLON => ;
                        Processing PHP_CodeSniffer\Standards\Generic\Sniffs\Formatting\DisallowMultipleStatementsSniff... DONE in 0 seconds
                Process token 8: T_WHITESPACE => \n
                        Processing PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\SuperfluousWhitespaceSniff... DONE in 0 seconds
                Process token 9: T_COMMENT => //··\n
                        Processing PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\SuperfluousWhitespaceSniff... DONE in 0.0001 seconds
                Process token 10: T_ECHO => echo
                        Processing PHP_CodeSniffer\Standards\Generic\Sniffs\PHP\LowerCaseKeywordSniff... DONE in 0 seconds
                Process token 11: T_WHITESPACE => ·
                        Processing PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\SuperfluousWhitespaceSniff... DONE in 0 seconds
                Process token 12: T_CONSTANT_ENCAPSED_STRING => "There·are·two·slashes·and·two·spaces·in·previous·line"
                Process token 13: T_SEMICOLON => ;
                        Processing PHP_CodeSniffer\Standards\Generic\Sniffs\Formatting\DisallowMultipleStatementsSniff... DONE in 0 seconds
                Process token 14: T_WHITESPACE => \n
                        Processing PHP_CodeSniffer\Standards\Squiz\Sniffs\WhiteSpace\SuperfluousWhitespaceSniff... DONE in 0 seconds
        *** END TOKEN PROCESSING ***

[...]
----------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
 6 | ERROR | [x] Whitespace found at end of line
----------------------------------------------------------------------
[...]

Expected behavior
In output summary, there should be another error indicated in the line 4 with the same (similar?) message: Whitespace found at end of line

Versions:

  • OS: Linux (Debian)
  • PHP: 5.6 and 7.2
  • PHPCS: 3.5.6
  • Standard: PSR12

Additional info
What is worth mentioning, using Squiz standard there's no such behaviour and line 4 is marked as "Whitespace found at end of line", so it's somehow possible. Tested on the same file with only changing standard to Squiz.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions