Fix phpstan/phpstan#13023: function.alreadyNarrowedType and function.impossibleType after updates to 2.1.15#5184
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
- When analyzing trait methods, $this is bound to each using class separately - is_a($this, SomeClass::class) was reported as "always true" in SomeClass context and "always false" in SomeClass2 context, but both are false positives - Added trait-aware check in ImpossibleCheckTypeHelper to treat $this as uncertain when inside a trait, since the same code is shared across multiple classes - New regression test in tests/PHPStan/Rules/Comparison/data/bug-13023.php Closes phpstan/phpstan#13023
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
When a trait method uses
is_a($this, SomeClass::class), PHPStan analyzes the method once per class that uses the trait. This caused false positives: in the context ofSomeClassit reported "always true" (function.alreadyNarrowedType), and in the context ofSomeClass2it reported "always false" (function.impossibleType). Both are incorrect since the trait code is shared across multiple classes.Changes
src/Rules/Comparison/ImpossibleCheckTypeHelper.phpto treat$thisas having uncertain type when inside a trait context, in both thesureTypesandsureNotTypesloopstests/PHPStan/Rules/Comparison/data/bug-13023.phptestBug13023intests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.phpRoot cause
ImpossibleCheckTypeHelper::findSpecifiedType()compares the result type from the type specifier against the argument type from the scope. When analyzing a trait method,$thisis typed asThisTypebound to the specific using class. TheisSuperTypeOfcheck then returns a definitive yes/no for each class context, causing the rule to report false positives. The fix detects when we're in a trait and the expression being checked is$this, and treats the result asmaybeinstead.Test
Added a regression test with a trait
MyTraitused by bothSomeClassandSomeClass2, where the trait method callsis_a($this, SomeClass::class). The test expects no errors.Fixes phpstan/phpstan#13023