Skip to content

Report functions that can be marked @phpstan-pure, with a fix - #6933

Open
SanderMuller wants to merge 1 commit into
phpstan:2.3.xfrom
SanderMuller:possibly-pure-fix
Open

SanderMuller wants to merge 1 commit into
phpstan:2.3.xfrom
SanderMuller:possibly-pure-fix

Conversation

@SanderMuller

Copy link
Copy Markdown
Contributor

This adds an opt-in parameter, checkMissingPurePhpDocTag. When it is enabled, PureFunctionRule and PureMethodRule report a function or method that has no purity tag but can be marked @phpstan-pure. The error has a fix that adds the tag, so --fix can mark a codebase.

A function is reported when all of these are true:

  • It has no @phpstan-pure or @phpstan-impure tag, and no pure-unless parameters.
  • It returns a value. A void return, a generator and a by-reference return are skipped.
  • It has no impure points and no explicit throw points, and no @throws other than void.
  • It has no asserts and no by-reference parameters, and it is not a constructor.

A method is reported only when it cannot be overridden: it is private, final, or in a final class. Trait methods are skipped.

Each fix round marks one call level. A caller of a function that was just marked gets its report in the next round. So --fix needs a few rounds on a large codebase.

I ran --fix on PHPStan's own src. It added 3663 tags in 1169 files over 5 rounds. After that, a normal self-analysis gave no purity errors. The only new errors came from ArenaCache::attach(): one staticMethod.resultUnused and one ignore.unmatched. I did not commit those tags.

The parameter needs a line in the config reference on phpstan.org. I can open that PR after this one.

🤖 Generated with Claude Code

With checkMissingPurePhpDocTag enabled, the pure function and method
rules report a function or method without a purity tag that returns a
value and has no impure points, no explicit throw points, no asserts,
no by-reference parameters and no by-reference return. Methods are
reported only when they cannot be overridden. The fix adds
@phpstan-pure to the PHPDoc. Callers of a function marked this way are
reported in the next fix round.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant