Skip to content

exclude-pattern not respected #683

Closed
@tomasnorre

Description

@tomasnorre

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
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
  1. ci: Fix strict types sniff exercism/php#857

To reproduce

Steps to reproduce the behavior:

  1. Create a file called KindergartenGarden.php in a folder called .meta with the code sample above
  2. Create ExplainStrictTypesSniff.php in folder src/Sniffs/StrictTypes/ from the code above
  3. Create phpcs.xml in base-folder with content from above
  4. Run vendor/bin/phpcs -s .meta/KindergartenGarden.php
  5. 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 master branch of PHP_CodeSniffer.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions