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

Superfluous whitespace #317

Closed
llaville opened this issue Nov 10, 2014 · 2 comments
Closed

Superfluous whitespace #317

llaville opened this issue Nov 10, 2014 · 2 comments

Comments

@llaville
Copy link

Hello Greg,

I use the latest version 2.0.0RC4 of phpcs, and I noticed something I think will be nice to catch to enhance the superfluous whitespace sniff.

If you've code with spaces line content, it's not detected as a violation (neither an error or a warning).

Here is an example :

<?php
if (is_int($var)) {
    return $var; 
}

if (empty($var['analysers'])) {
}

At end of return $var there are extra whitespace, and also some white spaces on blank line between each if statements.

So if I run the following command

phpcs -vvv --standard=PSR2  /path/to/sample/script

I got

--------------------------------------------------------------------------------
FOUND 1 ERROR(S) AFFECTING 1 LINE(S)
--------------------------------------------------------------------------------
 3 | ERROR | Whitespace found at end of line
--------------------------------------------------------------------------------

I expect to have

--------------------------------------------------------------------------------
FOUND 1 ERROR(S) AFFECTING 1 LINE(S)
--------------------------------------------------------------------------------
 3 | ERROR | Whitespace found at end of line
 5 | ERROR | Whitespace found at end of line
--------------------------------------------------------------------------------

See token 17 about line 3
See token 20 corresponding to line 5 with extra spaces

Here is a chunk of console output

   *** START LEVEL MAP ***
    Process token 0 on line 1 [lvl:0;]: T_OPEN_TAG => <?php\n
    Process token 1 on line 2 [lvl:0;]: T_IF => if
    Process token 2 on line 2 [lvl:0;]: T_WHITESPACE =>
    Process token 3 on line 2 [lvl:0;]: T_OPEN_PARENTHESIS => (
    Process token 4 on line 2 [lvl:0;]: T_STRING => is_int
    Process token 5 on line 2 [lvl:0;]: T_OPEN_PARENTHESIS => (
    Process token 6 on line 2 [lvl:0;]: T_VARIABLE => $var
    Process token 7 on line 2 [lvl:0;]: T_CLOSE_PARENTHESIS => )
    Process token 8 on line 2 [lvl:0;]: T_CLOSE_PARENTHESIS => )
    Process token 9 on line 2 [lvl:0;]: T_WHITESPACE =>
    Process token 10 on line 2 [lvl:0;]: T_OPEN_CURLY_BRACKET => {
    => Found scope opener for 1:T_IF
            * level increased *
            * token 1:T_IF added to conditions array *
            Process token 11 on line 2 [lvl:1;conds;T_IF;]: T_WHITESPACE => \n
            Process token 12 on line 3 [lvl:1;conds;T_IF;]: T_WHITESPACE =>
            Process token 13 on line 3 [lvl:1;conds;T_IF;]: T_RETURN => return
            Process token 14 on line 3 [lvl:1;conds;T_IF;]: T_WHITESPACE =>
            Process token 15 on line 3 [lvl:1;conds;T_IF;]: T_VARIABLE => $var
            Process token 16 on line 3 [lvl:1;conds;T_IF;]: T_SEMICOLON => ;
            Process token 17 on line 3 [lvl:1;conds;T_IF;]: T_WHITESPACE =>  \n
            Process token 18 on line 4 [lvl:1;conds;T_IF;]: T_CLOSE_CURLY_BRACKET => }
            => Found scope closer for 10:T_OPEN_CURLY_BRACKET
            * token T_IF removed from conditions array *
            * level decreased *
    Process token 19 on line 4 [lvl:0;]: T_WHITESPACE => \n
    Process token 20 on line 5 [lvl:0;]: T_WHITESPACE =>     \n
    Process token 21 on line 6 [lvl:0;]: T_IF => if

Hope my explains are enough !
Laurent

@gsherwood
Copy link
Member

You explained it very well, but the feature does already exist. It's just that the PSR2 standard explicitly says that whitespace is not allowed at the end of non-blank lines.

Take a look at this part of the standard: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md#23-lines

There MUST NOT be trailing whitespace at the end of non-blank lines.

Please don't ask me why this line is in the standard :)

To achieve this, the PSR2 ruleset.xml file includes this rule:

<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace">
  <properties>
    <property name="ignoreBlankLines" value="true"/>
  </properties>
</rule>

By default, the Squiz.WhiteSpace.SuperfluousWhitespace sniff does report an error on line 5. You can check this by running the following command

$ phpcs temp.php --standard=Squiz --sniffs=Squiz.WhiteSpace.SuperfluousWhitespace

FILE: temp.php
--------------------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 2 LINES
--------------------------------------------------------------------------------
 3 | ERROR | [x] Whitespace found at end of line
 5 | ERROR | [x] Whitespace found at end of line
--------------------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
--------------------------------------------------------------------------------

So for the PSR2 standard, there isn't anything I can do. But you can use that Squiz sniff the way it was originally intended and it will look for whitespace everywhere.

I hope that explains things well enough.

@llaville
Copy link
Author

Thank you so much Greg for your quick and efficient answer

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

No branches or pull requests

2 participants