squizlabs / PHP_CodeSniffer

PHP_CodeSniffer tokenizes PHP files and detects violations of a defined set of coding standards.
BSD 3-Clause "New" or "Revised" License
10.66k stars 1.48k forks source link

Cancel inherited sub-rule #442

Closed Max13 closed 9 years ago

Max13 commented 9 years ago

Hi there, I've created a "ruleset.xml" inheriting from PSR2 but canceling a rule that PSR2 is defining. I can't find how.

Here is the beginning of my file:

    <rule ref="PSR2">
        <exclude name="PSR1.Classes.ClassDeclaration.MissingNamespace" />
        <exclude name="Generic.Files.LineEndings" />
    </rule>

PSR2's includes this:

<rule ref="Generic.WhiteSpace.ScopeIndent">
    <properties>
        <property name="ignoreIndentationTokens" type="array" value="T_COMMENT,T_DOC_COMMENT"/>
    </properties>
</rule>

And I want the rule Generic.WhiteSpace.ScopeIndent to be applied to everything, in other words, the ignoreIndentationTokens to be applied to nothing or T_NONE. I wasn't able to exclude this default rule. I tried to override the property with empty value, with "T_NONE", I tried to exclude the rule then redefining it. Comments are still ignored by Generic.WhiteSpace.ScopeIndent. Is there a way to do that ?

aik099 commented 9 years ago

Does example from official doc help: http://pear.php.net/manual/en/package.php.php-codesniffer.annotated-ruleset.php ?

@gsherwood , I suggest placing link to this example ruleset.xml in README.md file because so many people can't find it or even know that it exists.

Konafets commented 9 years ago

@aik099, its all in the wiki at https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml

aik099 commented 9 years ago

I don't like GitHub wiki due limited page width. For that reason I prefer pear page.

Max13 commented 9 years ago

Hi, after having read your links (I've already checked the doc on Github, unsuccessfully), I realized that laravel comments are not standard comments and maybe that's why it's ignored?

https://github.com/laravel/laravel/blob/v4.2.11/app/config/app.php=

gsherwood commented 9 years ago

This ruleset works for me. It shows errors on both the comment line and the echo line, whereas PSR2 only shows an error on the echo line.

<?xml version="1.0"?>
<ruleset name="MyStandard">
 <description>My custom coding standard.</description>
 <rule ref="PSR2">
    <exclude name="PSR1.Classes.ClassDeclaration.MissingNamespace" />
    <exclude name="Generic.Files.LineEndings" />
 </rule>

 <rule ref="Generic.WhiteSpace.ScopeIndent">
    <properties>
        <property name="ignoreIndentationTokens" type="array" value=""/>
    </properties>
 </rule>
</ruleset>

The code I am testing on is:

<?php
if ($foo) {
// he he he
echo $bar;
}
$ phpcs temp.php --standard=mystandard.xml

FILE: temp.php
-------------------------------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 2 LINES
-------------------------------------------------------------------------------------------
  3 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found 0
  4 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found 0
-------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-------------------------------------------------------------------------------------------

Are you sure comments are actually being ignored? Lines must indented at least 4 spaces by default. They can be indented more spaces and the sniff will not complain.

Max13 commented 9 years ago

Look at the file I linked in my previous answer, this file has different comments:

    /* This is
     | a very
     | strange comment
     */

It seems to be ignored. The sniffer does report the first and last line, but not the lines between

gsherwood commented 9 years ago

@Max13 I get the same errors with those comment styles as well.

Max13 commented 9 years ago

Here is what I have. ruleset.xml:

<?xml version="1.0"?>
<ruleset name="MCS">
    <description>Coding Standards based on PSR-2</description>
    <rule ref="PSR2">
        <exclude name="PSR1.Classes.ClassDeclaration.MissingNamespace" />
        <exclude name="Generic.Files.LineEndings" />
    </rule>
    <rule ref="Generic.WhiteSpace.ScopeIndent">
        <properties>
            <property name="ignoreIndentationTokens" type="array" value=""/>
        </properties>
    </rule>
    <rule ref="Generic.CodeAnalysis.EmptyStatement" />
    <rule ref="Generic.CodeAnalysis.UnconditionalIfStatement" />
    <rule ref="Generic.CodeAnalysis.UnnecessaryFinalModifier" />
    <rule ref="Generic.CodeAnalysis.UnusedFunctionParameter" />
    <rule ref="Generic.CodeAnalysis.UselessOverridingMethod" />
    <rule ref="Generic.Commenting.Fixme" />
    <rule ref="Generic.Commenting.Todo" />
    <rule ref="Generic.PHP.DeprecatedFunctions" />
    <rule ref="Generic.PHP.ForbiddenFunctions" />
</ruleset>

test.php:

<?php
return array(
    /*
    |--------------------------------------------------------------------------
    | Application Debug Mode
    |--------------------------------------------------------------------------
    |
    | When your application is in debug mode, detailed error messages with
    | stack traces will be shown on every error that occurs within your
    | application. If disabled, a simple generic error page is shown.
    |
    */
    'debug' => false,
);

command:

➜  Intra git:(master) ✗ vendor/bin/phpcs --standard=ext/CodingSyntax/ruleset.xml --report=full -a test.php

FILE: /Users/Max13/Dev/System/Olympic/Intra/test.php
--------------------------------------------------------------------------------
FOUND 2 ERRORS AFFECTING 2 LINES
--------------------------------------------------------------------------------
  5 | ERROR | [x] Spaces must be used to indent lines; tabs are not allowed
 16 | ERROR | [x] Spaces must be used to indent lines; tabs are not allowed
--------------------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
--------------------------------------------------------------------------------

I'm on OSX, PHP_CodeSniffer 2.1.0, PHP 5.4.34. I don't understand why I don't have the same result.

gsherwood commented 9 years ago

Sorry, I must have missed this comment come through.

Your ruleset looks fine. The code you have posted doesn't have an indentation errors in it, so I don't know what errors you are expecting to get. Indentation is not checked within array definitions anyway (they all tend to have their own indentation rules), so you'd need to use different example code to see the change you've made in your ruleset.

Try checking this code:

<?php

class MyClass
{
/*
|--------------------------------------------------------------------------
| MyFunction
|--------------------------------------------------------------------------
|
| A comment about the function.
|
*/
public function myFunction()
{
    echo 'foo';
}
}

Run the PSR2 standard over it and notice that the comment lines are ignored:

$ phpcs temp.php --standard=PSR2

FILE: temp.php
----------------------------------------------------------------------------------------------------
FOUND 4 ERRORS AFFECTING 4 LINES
----------------------------------------------------------------------------------------------------
  3 | ERROR | [ ] Each class must be in a namespace of at least one level (a top-level vendor name)
 13 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found 0
 14 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found 0
 15 | ERROR | [x] Line indented incorrectly; expected at least 8 spaces, found 4
----------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 3 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------------------------------------

Now run your ruleset over this same code and notice how errors are generated for all the comment lines:

$ phpcs temp.php --standard=ruleset.xml

FILE: temp.php
---------------------------------------------------------------------------------
FOUND 11 ERRORS AFFECTING 11 LINES
---------------------------------------------------------------------------------
  5 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found 0
  6 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found 0
  7 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found 0
  8 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found 0
  9 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found 0
 10 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found 0
 11 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found 0
 12 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found 0
 13 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found 0
 14 | ERROR | [x] Line indented incorrectly; expected at least 4 spaces, found 0
 15 | ERROR | [x] Line indented incorrectly; expected at least 8 spaces, found 4
---------------------------------------------------------------------------------
PHPCBF CAN FIX THE 11 MARKED SNIFF VIOLATIONS AUTOMATICALLY
---------------------------------------------------------------------------------

I don't see any problems.