Add regression test for invariant template type narrowed by control flow in a method getter#5922
Merged
staabm merged 1 commit intoJun 23, 2026
Conversation
…low in a method getter - Add tests/PHPStan/Rules/Methods/data/bug-11776.php reproducing the reported false positive where a getter returning a property typed with `class-string<EnumAsFilterInterface<(int|string)&TOperation>>` was reported as not matching its own `@return`. - Register testBug11776() in ReturnTypeRuleTest covering the method-return path of the bug fixed in TemplateTypeVariance (invariant variance no longer mismatches when both arguments are the same template parameter narrowed by control flow). - Probed analogous constructs (passing the property to a method parameter of the same narrowed template type, and assigning it to another property of the same type); neither triggers the invariant variance equality check, so they were never affected. The function-return sibling is already covered by bug-13190.
VincentLanglet
approved these changes
Jun 23, 2026
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.
Summary
A getter returning a property typed with the same generic, control-flow-narrowed
template type as its own
@returnwas incorrectly reported as a return typemismatch (e.g.
should return class-string<EnumAsFilterInterface<TOperation of int|string>> but returns class-string<EnumAsFilterInterface<TOperation of bool|float|int|string>>).The underlying cause was fixed in
TemplateTypeVariance: an invariant variancecheck now returns
Yeswhen both type arguments are the same template parameter(same scope and name). This PR adds the missing regression test for the
method-getter manifestation of that bug.
Changes
tests/PHPStan/Rules/Methods/data/bug-11776.php— the verbatimreproducer from the issue's playground sample.
testBug11776()intests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php, asserting no errors.Root cause
TemplateTypeVariance::isValidVariance()compared the two template typearguments via
equals()in the invariant branch. When the same templateparameter was narrowed differently along control flow (e.g. the inferred
scalarbound vs. the(int|string)&TOperationintersection from theconstructor), the two
TemplateTypeinstances were notequals(), producing aspurious mismatch. The fix short-circuits to
Yeswhen both sides are the sametemplate parameter (matching scope and name). This central fix covers every
caller of the invariant variance check.
Test
bug-11776.phpreproduces the reported method-getter case; it fails with theexact issue error when the
TemplateTypeVariancefix is reverted and passeswith it.
the same narrowed template type, and assigning it to another property of the
same type — neither reaches the invariant equality check, so they were never
affected.
bug-13190.Fixes phpstan/phpstan#11776