Skip to content

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

Closed

Description

@villfa

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.

Activity

  1. janedbal commented on Jul 15, 2024

    @janedbal
    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

  2. calebdw commented on Jul 15, 2024

    @calebdw
    Contributor

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

  3. ondrejmirtes commented on Aug 8, 2024

    @ondrejmirtes
    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.

  4. petski commented on Sep 18, 2024

    @petski

    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.

  5. NanoSector commented on Apr 25, 2025

    @NanoSector

    @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?

  6. calebdw commented on May 1, 2025

    @calebdw
    Contributor

    PR has been submitted!

  7. phpstan-bot commented on Feb 23, 2026

    @phpstan-bot
    Contributor

    Closed via merging phpstan/phpstan-src#3976

  8. janedbal commented on Feb 23, 2026

    @janedbal
    Contributor

    Is this really closed by phpstan/phpstan-src#3976 ? That part about phpstan-ignore-line + phpstan-ignore-next-line seems not to be present there.

  9. ondrejmirtes commented on Feb 23, 2026

    @ondrejmirtes
    Member

    @janedbal My plan is to deprecate these two completely and remove them in PHPStan 3.0.

    The problem with them is that people use them with error identifiers, but they silently ignore all errors anyway. Since changing this behaviour would be a BC break, it's better to completely remove them.

    And if there's still need, we can introduce something like @phpstan-ignore-all that would fail with anything else inside the comment, aside from comment inside (...).

  10. ondrejmirtes commented on Feb 23, 2026

    @ondrejmirtes
    Member

    Reverting the PR for now, it didn't worked properly.

  11. phpstan-bot commented on Feb 23, 2026

    @phpstan-bot
    Contributor

    Closed via merging phpstan/phpstan-src#5032

  12. derrabus commented on Feb 23, 2026

    @derrabus

    Is this really fixed by phpstan/phpstan-src#5032? I still cannot disallow the old ignore comments, can I?

  13. ondrejmirtes commented on Feb 23, 2026

    @ondrejmirtes
    Member

    @derrabus No, see: #11340 (comment)

    The option only disallows @phpstan-ignore without comment for each identifier.

  14. derrabus commented on Feb 23, 2026

    @derrabus

    Shall we reopen this issue then? 🙂

  15. JakeQZ commented on Mar 5, 2026

    @JakeQZ

    The problem with them is that people use them with error identifiers, but they silently ignore all errors anyway.

    Yes, we missed that (the documentation was not clear). As a consumer, I think we would be happy with the removal of all ignores apart from the one that allows specification of which warnings to ignore.

  16. ondrejmirtes commented on Mar 16, 2026

    @ondrejmirtes
    Member

    Alright, I changed my mind. reportIgnoresWithoutComments: true will also disallow @phpstan-ignore-line and @phpstan-ignore-next-line. Since this a new option and the default is false, this is not a BC break.

    Also it makes sense in my eyes, because @phpstan-ignore-line and @phpstan-ignore-next-line don't have an actual syntax for comments. As opposed to @phpstan-ignore which has officially supported comments by the parser in parentheses.

  17. derrabus commented on Mar 24, 2026

    @derrabus

    Awesome, thank you!

  18. github-actions commented on Apr 25, 2026

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  19. locked as resolved and limited conversation to collaborators on Apr 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions