PHP_CodeSniffer icon indicating copy to clipboard operation
PHP_CodeSniffer copied to clipboard

The same rule, only the last one takes effect

Open dongasai opened this issue 4 years ago • 6 comments

My executive information is as follows:

root@80525e84595d:/var/www/html# php vendor/bin/phpcs -a -p -vvv --ignore=1/pb_proto/*,1/db -
Processing ruleset /var/www/html/phpcs.xml
	Processing rule "Generic.Arrays.DisallowShortArraySyntax"
		=> /var/www/html/vendor/squizlabs/php_codesniffer/src/Standards/Generic/Sniffs/Arrays/DisallowShortArraySyntaxSniff.php
		=> added rule-specific absolute ignore pattern: 1/1/
	Processing rule "Generic.PHP.ForbiddenFunctions"
		=> /var/www/html/vendor/squizlabs/php_codesniffer/src/Standards/Generic/Sniffs/PHP/ForbiddenFunctionsSniff.php
		=> array property "forbiddenFunctions" set to "time=>\SQLUtil::getCurrTimeStamp"
		=> added rule-specific absolute ignore pattern: 1.php
		=> added rule-specific absolute ignore pattern: 1.php
	Processing rule "Generic.PHP.ForbiddenFunctions"
		=> /var/www/html/vendor/squizlabs/php_codesniffer/src/Standards/Generic/Sniffs/PHP/ForbiddenFunctionsSniff.php
		=> array property "forbiddenFunctions" set to "delete=>unset,var_dump=>,dump=>,var_export=>"
		=> added rule-specific absolute ignore pattern: 1/statistic/*
	Processing rule "Generic.PHP.ForbiddenFunctions"
		=> /var/www/html/vendor/squizlabs/php_codesniffer/src/Standards/Generic/Sniffs/PHP/ForbiddenFunctionsSniff.php
		=> array property "forbiddenFunctions" set to "var_export=>"
		=> added rule-specific absolute ignore pattern: function.php
	Processing rule "Generic.PHP.ForbiddenFunctions"
		=> /var/www/html/vendor/squizlabs/php_codesniffer/src/Standards/Generic/Sniffs/PHP/ForbiddenFunctionsSniff.php
		=> array property "forbiddenFunctions" set to "echo=>echos,print_r=>print_rt"
		=> added rule-specific absolute ignore pattern: 1/1/function.php
		=> added rule-specific absolute ignore pattern: 1/script/*
		=> added rule-specific absolute ignore pattern: 1/tests/*

However, only the last rule of generic.php.forbiddenfunctions will take effect. It is expected that all the rules will take effect and will be checked by polling

dongasai avatar May 13 '21 08:05 dongasai

@dongasai Could you show the relevant part of your ruleset ?

Based on the above, I get the impression you are trying to include the Generic.PHP.ForbiddenFunctions sniff multiple times with different property values and different file ignore patterns and expect the ignore patterns to be applied for each set of property values.

That's not how PHPCS works.

If just plainly set, each new property setting will overwrite the previous one.

If instead you want to add to the existing property value, make sure you use extend="true", like so:

 <rule ref="Generic.PHP.ForbiddenFunctions">
  <properties>
   <property name="forbiddenFunctions" type="array" extend="true">
    <element key="sizeof" value="count"/>
   </property>
  </properties>
 </rule>

Also see the code samples in the wiki: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-Ruleset

As for the file ignore patterns, those are cumulative and they are not linked to the property value, but only to the sniff. So all of the ignore patterns will be applied to the sniff during the run, independently of the value set for the property.

If you want to be able to apply the ignore patterns for each property setting, you will need to create custom sniffs which extend the Generic.PHP.ForbiddenFunctions sniff. Based on the above, you would need three different sniffs. You can then set the property value for each either in the sniff itself or via the ruleset based on the names of your custom sniffs and similarly, you can then apply ignore patterns for each of the custom sniffs in your ruleset as well.

jrfnl avatar May 13 '21 08:05 jrfnl

I read the source code. The sniff set is marked with its name. The same name will override the former. Oh, it seems that I can only customize the sniff

dongasai avatar May 13 '21 09:05 dongasai

This is not a good feature and we hope to optimize it

dongasai avatar May 13 '21 09:05 dongasai

Just to clarify what is being requested:

Are you trying to set different standards on different file in your code base? So, for example, you only want delete() to be banned for files under the 1/statistic/ directory?

gsherwood avatar May 23 '21 23:05 gsherwood

Just to clarify what is being requested:

Are you trying to set different standards on different file in your code base? So, for example, you only want delete() to be banned for files under the 1/statistic/ directory?

Yes, because different files run in different environments, the statistical folder is run by the developer, the control folder is run by the server (no output allowed), and the test folder is only used for testing (output allowed)

dongasai avatar Jul 09 '21 09:07 dongasai