Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
nereusz opened this issue Sep 22, 2020 · 2 comments
Closed

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

nereusz opened this issue Sep 22, 2020 · 2 comments
Milestone

Comments

@nereusz
Copy link

nereusz commented Sep 22, 2020

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.

@gsherwood
Copy link
Member

gsherwood commented Sep 23, 2020

Went back to see if this was an intentional change, and it appears to be: php-fig/fig-standards@d3a3d23

It doesn't reference a reason for the change, besides the wording being confusing. That leads me to believe that the author believed they were keeping the intended message from PSR-2, but making the wording clearer. I have no idea if that's correct, but the commit does say "Trailing whitespace is not allowed in any situation", so I think that is clear enough to ban it on lines that only contain whitespace.

@gsherwood gsherwood changed the title PSR-12: incorrectly handle empty line with only whitespaces PSR-12 not reporting error for empty lines with only whitespace Sep 24, 2020
@gsherwood gsherwood added this to the 3.5.7 milestone Sep 24, 2020
@gsherwood
Copy link
Member

I've pushed up the fix for this. Thanks for pointing out that PSR-12 had modified this rule from PSR-2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants