Skip to content

Add @phpstan-ignore-var to keep inline @var tags from overriding inferred types - #6887

Open
jack-worman wants to merge 9 commits into
phpstan:2.3.xfrom
jack-worman:phpstan-ignore-var
Open

jack-worman wants to merge 9 commits into
phpstan:2.3.xfrom
jack-worman:phpstan-ignore-var

Conversation

@jack-worman

@jack-worman jack-worman commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Adds @phpstan-ignore-var for inline @var tags kept only for IDE autocompletion: PHPStan keeps the type it infers instead of applying them, but unlike Psalm's @psalm-ignore-var it still checks them.

  • It covers every @var tag in the PHPDoc (@var, @phpstan-var, @psalm-var, @phan-var) and can also directly follow the @var tag: /** @var Foo $foo @phpstan-ignore-var */.
  • A tag that contradicts the inferred type is still reported, as for any inline @var.
  • The tag is reported as having no effect where inline @var tags have none (above declarations, parameters, closures, elseif, else and catch, at the end of a class body) and in a PHPDoc without a @var tag.
  • The @phpstan-ignore comment parser skips it instead of reporting a parse error.

Complements #6888: a redundant tag the IDE still needs can be kept with @phpstan-ignore-var.

Related: phpstan/phpstan#9049

@jack-worman
jack-worman marked this pull request as draft October 30, 2026 03:18
@jack-worman jack-worman changed the title Add @phpstan-ignore-var to ignore @var in the same PHPDoc Add @phpstan-ignore-var to keep inline @var tags from overriding inferred types Oct 30, 2026
@jack-worman
jack-worman marked this pull request as ready for review October 30, 2026 04:11
@phpstan-bot

Copy link
Copy Markdown
Collaborator

This pull request has been marked as ready for review.

- recognise the tag on the line of a `@var` tag, as in `/** @var Foo $x @phpstan-ignore-var */`
- do not check ignored `@var` tags above `global` and `static` statements against an earlier variable
- report the tag above parameters, closures and arrow functions and at the end of a class body
…one-line form

- report the tag above a closure or arrow function passed as an argument or written as an array item, whose PHPDoc php-parser attaches to the argument or the item
- report it above `elseif`, `else` and `catch`, where no `@var` applies, instead of checking the ignored tags there
- recognise it in a `@var` description only at the start, so a description that mentions it keeps the `@var` applied
- cover an ignored `@var` over a variable that may not be defined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants