Repository navigation
[PHPStan] Add PreferGenericTemplateOnReturnedUnionParamRule to spot hidden generics - #8638
Merged
Merged
Conversation
TomasVotruba
force-pushed
the
phpstan-hidden-generic-return-rule
branch
2 times, most recently
from
October 12, 2026 09:07
dfa2ff3 to
b2bf1c3
Compare
…eturn instead of generic
TomasVotruba
force-pushed
the
phpstan-hidden-generic-return-rule
branch
from
October 12, 2026 09:08
b2bf1c3 to
dd582dc
Compare
TomasVotruba
enabled auto-merge (squash)
October 12, 2026 11:17
TomasVotruba
disabled auto-merge
October 12, 2026 11:17
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.
Adds a self-analysis PHPStan rule that spots hidden generics: a method taking a class-type union param (e.g.
New_|MethodCall) but declaring a broader single return type (their shared parentNode), then returning that very param. In such a case a@templategeneric narrows the return to the passed type instead of the broad parent.Complements the existing
PreferGenericTemplateOnSameUnionTypeRule, which only covers the case where both param and return are the same union. This one covers the broad-parent return.Guards against false positives:
@templateis already presentThe rule found 3 real cases in the codebase, 2 of which are fixed here with
@templategenerics:RemoveNullTagValueNodeRector::processVarTagNull()RemoveZeroBreakContinueRector::processVariableNum()The third (
SideEffectNodeDetector::resolveVariable()) is correctly skipped by the union-member guard.