Skip to content

Option to disable phpstan-ignore-line and phpstan-ignore-next-line #11340

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

Open
villfa opened this issue Jul 15, 2024 · 6 comments · May be fixed by phpstan/phpstan-src#3976
Open

Option to disable phpstan-ignore-line and phpstan-ignore-next-line #11340

villfa opened this issue Jul 15, 2024 · 6 comments · May be fixed by phpstan/phpstan-src#3976

Comments

@villfa
Copy link
Contributor

villfa commented Jul 15, 2024

Feature request

Hi,

With the release of version 1.11, a new annotation, @phpstan-ignore, was introduced, allowing developers to specify an error identifier as a parameter. This feature provides a significant advantage over @phpstan-ignore-line and @phpstan-ignore-next-line, as it targets specific error identifiers rather than ignoring all potential errors on a given line. Consequently, it ensures that any new violations introduced on the same line are detected, improving overall code quality and reducing the risk of missing critical issues.

Given these benefits, I would like to request the addition of a configuration option to disable the use of @phpstan-ignore-line and @phpstan-ignore-next-line. By enabling this configuration, development teams can enforce the use of @phpstan-ignore with specific error identifiers, thereby promoting more precise and reliable error handling.

I believe that this feature will be a valuable addition to PHPStan, encouraging best practices and helping developers maintain higher standards of code quality.

Did PHPStan help you today? Did it make you happy in any way?

Thank you for considering this request. I appreciate your continued efforts in making PHPStan an indispensable tool for the PHP community.

@janedbal
Copy link
Contributor

You can easily deny the old approach by some super-simple CI script. We did the same thing here: shipmonk-rnd/phpstan-rules#246

@calebdw
Copy link
Contributor

calebdw commented Jul 15, 2024

It would also be nice to have the option to enforce that a comment is always given with the ignore

@ondrejmirtes
Copy link
Member

We already have this information available in AnalyserResult (getLinesToIgnore, getUnmatchedLineIgnores) so it's something that could be easily achieved with a few lines in AnalyserResultFinalizer.

@petski
Copy link

petski commented Sep 18, 2024

FWIW, I currently have this feature implemented in a custom rule like this:

public function getNodeType(): string
{
    return FileNode::class;
}

public function processNode(Node $node, Scope $scope): array
{
    /** @var array<int, non-empty-list<string>|null> $linesToIgnore */
    $linesToIgnore = $node->getAttribute('linesToIgnore');

    $errors = [];

    // identifiersToIgnore is null in case no specific identifiers are set, and a (non-empty) array in case specific identifiers are set
    foreach ($linesToIgnore as $lineToIgnore => $identifiersToIgnore) {
        // Skip line if specific identifiers are set
        if (is_array($identifiersToIgnore)) {
           continue;
        }

        $errors[] = RuleErrorBuilder::message('PHPStan ignored line without identifiers')
          ->tip('Add at least one identifier with the `@phpstan-ignore` directive or in the `parameters.ignoreErrors` config')
          ->nonIgnorable()
          ->line($lineToIgnore)
          ->identifier('someIdentifier')
          ->build()
        ;
    }

    return $errors;
}

Unfortunately, the interface of the FileNode doesn't seems to have a public getLinesToIgnore(), so I have to use the ->getAttribute('linesToIgnore') approach instead.

@NanoSector
Copy link

@petski Thank you, this also works well for us.

@ondrejmirtes You mentioned in the phpstan-strict-rules package that it should be a quick change using AnalyserResult; does this mean you're open to having this integrated into core PHPStan? I'd be happy to look into providing a PR for this.

Another feature we're looking into is requiring reasons for ignore lines; not sure if this is out of scope for this issue. Even with above custom rule it's possible to write an ignore line like @phpstan-ignore missingType.checkedException without providing a reason. Is this also something we could integrate?

@calebdw
Copy link
Contributor

calebdw commented May 1, 2025

PR has been submitted!

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

Successfully merging a pull request may close this issue.

6 participants