Repository navigation
Closed
Description
Created based on discussion: #680
Describe the bug
After adjusting (1) the Sniff according to https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki/Coding-Standard-Tutorial#creating-the-sniff my exclude-pattern isn't respected anymore.
Code sample
<?php
declare(strict_types=1);
class KindergartenGarden
{
public function getName(): string
{
return "Random Kindergarden";
}
}ExplainStrictTypesSniff.php
<?php
declare(strict_types=1);
namespace Exercism\Sniffs\StrictTypes;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Fixer;
use PHP_CodeSniffer\Sniffs\Sniff;
class ExplainStrictTypesSniff implements Sniff
{
private static string $explanation = <<<EOT
/*
* By adding type hints and enabling strict type checking, code can become
* easier to read, self-documenting and reduce the number of potential bugs.
* By default, type declarations are non-strict, which means they will attempt
* to change the original type to match the type specified by the
* type-declaration.
*
* In other words, if you pass a string to a function requiring a float,
* it will attempt to convert the string value to a float.
*
* To enable strict mode, a single declare directive must be placed at the top
* of the file.
* This means that the strictness of typing is configured on a per-file basis.
* This directive not only affects the type declarations of parameters, but also
* a function's return type.
*
* For more info review the Concept on strict type checking in the PHP track
* <link>.
*
* To disable strict typing, comment out the directive below.
*/
EOT;
private ?Fixer $fixer = null;
private int $position = 0;
private array $tokens = [
T_COMMENT,
];
public function register(): array
{
return [
T_DECLARE,
];
}
public function process(File $phpcsFile, $stackPtr): void
{
$this->fixer = $phpcsFile->fixer;
$this->position = $stackPtr;
if (!$phpcsFile->findPrevious($this->tokens, $stackPtr)) {
$phpcsFile->addFixableError(
'Missing explanation of declaration of strict types.',
$stackPtr - 1,
self::class
);
$this->fix();
}
}
private function fix(): void
{
$this->fixer->addContent($this->position - 1, self::$explanation);
}
}<?xml version="1.0"?>
<ruleset
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd"
name="Exercism PHP Coding Standard"
>
<description>Coding standard for Exercism PHP exercises</description>
<!-- Expect all files are UTF-8 -->
<arg name="encoding" value="utf-8" />
<!-- No warnings (ignore them) -->
<arg value="n" />
<!-- Show sniffs (it's easy to find solutions knowing the code) -->
<arg value="s" />
<!-- A TAB is 4 chars wide (does not replace them, for calculation only!) -->
<arg name="tab-width" value="4" />
<!-- Run on 60 files in parallel -->
<arg name="parallel" value="60" />
<file>exercises</file>
<file>src</file>
<!-- Include all sniffs in the PSR12 standard except... -->
<rule ref="PSR12">
<exclude name="PSR1.Classes.ClassDeclaration.MissingNamespace" />
<exclude name="PSR1.Classes.ClassDeclaration.MultipleClasses" />
<exclude name="PSR1.Files.SideEffects.FoundWithSymbols" />
</rule>
<rule ref="Squiz.Scope.MethodScope.Missing">
<exclude-pattern>*/concept/city-office/*</exclude-pattern>
<exclude-pattern>*/concept/windowing-system/*</exclude-pattern>
</rule>
<rule ref="src/Sniffs/StrictTypes/ExplainStrictTypesSniff.php">
<exclude-pattern>*/*Test\.php</exclude-pattern>
<exclude-pattern>*/.meta/*\.php</exclude-pattern>
<exclude-pattern>src/*</exclude-pattern>
<exclude-pattern>contribution/*.php</exclude-pattern>
</rule>
</ruleset>Custom ruleset
<rule ref="src/Sniffs/StrictTypes/ExplainStrictTypesSniff.php">
<exclude-pattern>*/*Test\.php</exclude-pattern>
<exclude-pattern>*/.meta/*\.php</exclude-pattern>
<exclude-pattern>src/*</exclude-pattern>
<exclude-pattern>contribution/*.php</exclude-pattern>
</rule>When I run vendor/bin/phpcs -s I don't get an error code, and the two errors are from the exclude-pattern files from above.
$ php vendor/bin/phpcs -s exercises/practice/kindergarten-garden
FILE: /home/tomas/Projects/exercism/php/exercises/practice/kindergarten-garden/.meta/example.php
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
2 | ERROR | [x] Missing explanation of declaration of strict types. (Exercism.StrictTypes.ExplainStrictTypes.Exercism\Sniffs\StrictTypes\ExplainStrictTypesSniff)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
FILE: /home/tomas/Projects/exercism/php/exercises/practice/kindergarten-garden/KindergartenGardenTest.php
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
2 | ERROR | [x] Missing explanation of declaration of strict types. (Exercism.StrictTypes.ExplainStrictTypes.Exercism\Sniffs\StrictTypes\ExplainStrictTypesSniff)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-------------------------------------------------------------------------------------------------------------------------------------------------------------------To reproduce
Steps to reproduce the behavior:
- Create a file called
KindergartenGarden.phpin a folder called.metawith the code sample above - Create
ExplainStrictTypesSniff.phpin foldersrc/Sniffs/StrictTypes/from the code above - Create
phpcs.xmlin base-folder with content from above - Run
vendor/bin/phpcs -s .meta/KindergartenGarden.php - Now you will see that the file isn't ignored.
vendor/bin/phpcs -s .meta/KindergartenGarden.php
FILE: /tmp/.meta/KindergartenGarden.php
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
2 | ERROR | [x] Missing explanation of declaration of strict types. (Exercism.StrictTypes.ExplainStrictTypes.Exercism\Sniffs\StrictTypes\ExplainStrictTypesSniff)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Expected behavior
I would expect the error not to be reported as the .meta directory is in the exclude-pattern.
Versions (please complete the following information)
| Operating System | Ubuntu 24.10 |
| PHP version | PHP 8.3.12 |
| PHP_CodeSniffer version | 3.11.0 |
| Standard | PSR12 |
| Install type | composer |
Please confirm
- I have searched the issue list and am not opening a duplicate issue.
- I have read the Contribution Guidelines and this is not a support question.
- I confirm that this bug is a bug in PHP_CodeSniffer and not in one of the external standards.
- I have verified the issue still exists in the
masterbranch of PHP_CodeSniffer.
Activity
jrfnl commentedon Nov 14, 2024
@tomasnorre Thanks for opening the issue.
I've already been looking into this and can confirm the issue.
To reproduce and investigate this, I've used a minimal setup with control:
Directory layout:
The content of the sniff files is the same with only minimal variation:
The only differences between the four sniffs are:
Exercism\Sniffs\StrictTypes.*WithExcludesniffs is updated fromControltoThisErrorShouldHaveBeenExcluded.Ruleset:
Command run from the project root:
Result
As the original discussion mentioned that it "used to work", I've done some git bisecting to validate this and see where things changed, starting at PHPCS 3.1.0.
My findings based on that are, however, different: This never worked (between PHPCS 3.1.0 and now) and it used to be worse.
This was reported in issue Sniff-specific ignore rules inside rulesets are filtering out too many files squizlabs/PHP_CodeSniffer#2391 and fixed via PR Check paths are accepted if no exclude-pattern matches squizlabs/PHP_CodeSniffer#2392.
SniffsDir.This was reported in issue Sniff properties not set when referencing a sniff using relative paths or non-native slashes squizlabs/PHP_CodeSniffer#2497 and fixed via PR Ruleset::processRule(): fix handling of rules included via path squizlabs/PHP_CodeSniffer#2501.
This will need further investigation and a fix, which probably needs to build on the prior fix from squizlabs/PHP_CodeSniffer#2501.
Detailed bisect analysis results and findings
PHPCS 3.1.0
Findings
PHPCS 3.5.0
Findings
Related to: Sniff-specific ignore rules inside rulesets are filtering out too many files squizlabs/PHP_CodeSniffer#2391
Fixed via: Check paths are accepted if no exclude-pattern matches squizlabs/PHP_CodeSniffer#2392
Released in PHPCS 3.5.0
PHPCS 3.5.4
Findings
Related to: Sniff-specific ignore rules inside rulesets are filtering out too many files squizlabs/PHP_CodeSniffer#2391
Fixed via: Check paths are accepted if no exclude-pattern matches squizlabs/PHP_CodeSniffer#2392
Released in PHPCS 3.5.0
Related to: Sniff properties not set when referencing a sniff using relative paths or non-native slashes squizlabs/PHP_CodeSniffer#2497
Fixed via: Ruleset::processRule(): fix handling of rules included via path squizlabs/PHP_CodeSniffer#2501
Released in PHPCS 3.5.4
PHPCS 3.11.1 /
masterFindings
Related to: Sniff-specific ignore rules inside rulesets are filtering out too many files squizlabs/PHP_CodeSniffer#2391
Fixed via: Check paths are accepted if no exclude-pattern matches squizlabs/PHP_CodeSniffer#2392
Released in PHPCS 3.5.0
Related to: Sniff properties not set when referencing a sniff using relative paths or non-native slashes squizlabs/PHP_CodeSniffer#2497
Fixed via: Ruleset::processRule(): fix handling of rules included via path squizlabs/PHP_CodeSniffer#2501
Released in PHPCS 3.5.4
tomasnorre commentedon Nov 14, 2024
Thanks @jrfnl for your thorough investigation. I'm not super familiar with PHPCS it self, but if there is anything I can do to help out, let me know.
jrfnl commentedon Nov 15, 2024
Well, actually...
Sorry, I don't know where my head was, but I've taken another look at this and suddenly realized that the problem is that - even after the proposed update in exercism/php#857 -, the naming conventions from PHPCS are still not followed for your sniff.
I've submitted a fix for this (tomasnorre/exercism-php#2) and with that fix included, things are working as expected.
With that in mind, I'm going to mark this ticket as a close candidate.
While, yes, this is a bug (as something is not working), I don't think PHPCS should have to support sniff file includes when the sniffs do not comply with the PHPCS naming conventions.
The fact that this is "partially" supported now is more by accident than by design and I think it is a good idea to formally and completely drop support for sniffs not complying with the PHPCS naming conventions in PHPCS 4.0.
tomasnorre commentedon Nov 15, 2024
Thanks for your PR and your further investigation. I totally overlooked that extra directory missing too.
jrfnl commentedon Feb 6, 2025
@tomasnorre I've added a new wiki page with more explicit information about the naming conventions. See: #689 (comment)
If you have a chance, would be great if you could give it a once over. Let me know if you have any feedback.
tomasnorre commentedon Feb 6, 2025
Thanks a lot @jrfnl I really like the wiki page. Thanks for taking care and improving the tool