Detect class constants referenced only in PhpDoc types - #393
Merged
Conversation
janedbal
force-pushed
the
constants-in-phpdoc-types
branch
from
July 8, 2026 06:40
de7084e to
976fa90
Compare
janedbal
marked this pull request as ready for review
July 8, 2026 07:57
A class constant used solely inside a PhpDoc type (e.g. an int-range bound `int<1, self::MAX>`, a generic argument, or a bare `Foo::BAR` constant type) was reported as dead. PHPStan eagerly collapses such types down to a literal (`int<1, 100>`), discarding the constant reference, so it only survives in the raw PhpDoc type AST that no collector traversed. ConstantFetchCollector now walks the (cache-hit) resolved PhpDoc block for ConstFetchNode occurrences across any type position and emits the matching ClassConstantUsage, resolving self/static/parent and imported names via the NameScope. Constant masks (self::SIZE_*) stay out of scope (#223). Closes #391 Co-Authored-By: Claude Code
Pre-filter the per-node PhpDoc scan on a cheap substring check so only docblocks that can hold a class-constant reference reach getResolvedPhpDoc and the AST traversal. Co-Authored-By: Claude Code
Cover the self::SIZE_* skip branch and pin #223 behavior: a masked class-constant reference in a PhpDoc type does not mark those constants as used. Co-Authored-By: Claude Code
Drop the Scope dependency from resolvePhpDocConstFetchOwner and read the current class from the NameScope, mirroring PHPStan's own ConstExprNodeResolver. Co-Authored-By: Claude Code
janedbal
force-pushed
the
constants-in-phpdoc-types
branch
from
July 8, 2026 10:41
aff282b to
8c44622
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #391
Problem
A class constant referenced only inside a PhpDoc type — an int-range bound
int<1, self::MAX>, a generic argument, a bareFoo::BARconstant type, etc. — was reported asshipmonk.deadConstant.PHPStan eagerly collapses such types down to a literal (
int<1, self::MAX>→int<1, 100>), discarding the "this bound came fromself::MAX" link. The reference survives only in the raw PhpDoc type AST, which no collector traversed.Fix
ConstantFetchCollectornow, for any node carrying a docblock, walks the resolved PhpDoc block forConstFetchNodeoccurrences across any type position and emits the matchingClassConstantUsage.FileTypeMapper::getResolvedPhpDoc(), which is memoized — for the docblocks PHPStan already resolved during analysis this is a cache hit, not a re-parse.NameScope(imports, namespace) withself/static/parenthandled explicitly; owner/inheritance/enum-case resolution reuses the existinggetDeclaringTypesWithConstant().self::SIZE_*) stay out of scope — that is the enum-migration case from Mark constants are used when referenced in PHPDoc withself::SIZE_*#223, deliberately unchanged.Performance
The per-node scan is gated behind a cheap
str_contains($docComment->getText(), '::')pre-filter, so docblocks that cannot hold a class-constant reference short-circuit before any PhpDoc resolution or AST traversal.