Fix #13023: function.alreadyNarrowedType and function.impossibleType after updates to 2.1.15#5084
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Conversation
- In ImpossibleCheckTypeHelper::findSpecifiedType(), treat class-specific type checks on $this as uncertain when inside a trait, since $this is polymorphic across the different classes that use the trait - Added isThis() helper method for detecting $this expression references - New regression test in tests/PHPStan/Rules/Comparison/data/bug-13023.php Closes phpstan/phpstan#13023
| $argumentType = $scope->getType($assignedInCallVar->expr); | ||
| } | ||
|
|
||
| if ($isInTrait && self::isThis($sureType[0]) && $resultType->getObjectClassNames() !== []) { |
Contributor
There was a problem hiding this comment.
Could be great to handle https://phpstan.org/r/73b2d6e1-9661-4375-b01d-cdd6ec39c69e too.
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 contains
is_a($this, SomeClass::class)and the trait is used by multiple classes, PHPStan falsely reports:function.alreadyNarrowedType("will always evaluate to true") when analyzing the trait in the context ofSomeClassfunction.impossibleType("will always evaluate to false") when analyzing the trait in the context ofSomeClass2Both are false positives because the trait code is shared across classes and the check is valid polymorphic behavior.
Changes
src/Rules/Comparison/ImpossibleCheckTypeHelper.php:findSpecifiedType(), added checks in both thesureTypesandsureNotTypesloops to treat class-specific type checks on$thisas uncertain (TrinaryLogic::createMaybe()) when the scope is inside a traitisThis()private static helper to detect$thisvariable expressionstests/PHPStan/Rules/Comparison/data/bug-13023.phptestBug13023()inImpossibleCheckTypeFunctionCallRuleTest.phpRoot cause
PHPStan analyzes trait methods once per class that uses the trait, each time with
$thistyped as that specific class. TheImpossibleCheckTypeHelper::findSpecifiedType()method compares the narrowed type (e.g.,ObjectType('SomeClass')) against the argument type (e.g.,$this(SomeClass)or$this(SomeClass2)), producing a definitive yes/no result for each class context. However, since trait code is shared, these definitive results are false positives.The fix detects when we're in a trait context and the sure type expression is
$thiswith a class-specific result type (i.e.,getObjectClassNames() !== []). In that case, the result is treated as uncertain, preventing the false positive. Non-class-specific checks likeis_int($this)oris_object($this)are not affected, as they produce correct results regardless of which class uses the trait.Test
Added
tests/PHPStan/Rules/Comparison/data/bug-13023.phpwith a traitMyTraitused bySomeClassandSomeClass2, where the trait method callsis_a($this, SomeClass::class). The test expects no errors (empty array), confirming the false positives are suppressed.Fixes phpstan/phpstan#13023