Fix phpstan/phpstan#13902: Chaining phpstan-assert fails to assert types#5127
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Fix phpstan/phpstan#13902: Chaining phpstan-assert fails to assert types#5127phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
- In TypeSpecifier::specifyTypesInCondition(), recurse into inner method calls when processing a method chain in null context, so assertions from all methods in the chain are applied - In TypeSpecifier::specifyTypesFromAsserts(), unwrap $this mapping through method calls whose return type equals the caller type, so outer assertions resolve to the original object - Only recurse when $expr->var is a MethodCall to avoid breaking ImpossibleCheckTypeHelper's rootExpr tracking - New regression test in tests/PHPStan/Analyser/nsrt/bug-13902.php
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 calling methods with
@phpstan-assertannotations in a chain (e.g.,$o->setA()->setB()where both return$this), neither assertion was recognized. Calling them separately ($o->setA(); $o->setB();) worked correctly.Changes
src/Analyser/TypeSpecifier.php-specifyTypesInCondition(): Added recursion into inner method calls when processing a method chain in null context (expression statements). When the outermost method call's assertions are processed and$expr->varis itself aMethodCall, the inner call is also processed and both results are combined. The recursion is guarded to only apply when$expr->var instanceof MethodCallto avoid breakingImpossibleCheckTypeHelper'srootExprtracking.src/Analyser/TypeSpecifier.php-specifyTypesFromAsserts(): When building the$argsMap['this']mapping, the code now walks back through the method chain, unwrapping through intermediate method calls whose return type equals the caller's type (indicating the method returns$this). This ensures that assertions like@phpstan-assert int $this->bon the outermost call resolve to the original variable (e.g.,$o->b) rather than the intermediate expression (e.g.,$o->setA()->b).Root cause
Two issues combined:
Missing recursion:
TypeSpecifier::specifyTypesInCondition()only processed the outermost method call's assertions. For$o->setA()->setB(), onlysetB()'s assertions were applied. The innersetA()call was never examined for its@phpstan-assertannotations.Wrong
$thisresolution: WhensetB()was called on$o->setA(), the$thisinsetB()'s assertion mapped to$o->setA()(the intermediate expression). This created a type narrowing on the expression key$o->setA()->binstead of$o->b, so the narrowing was invisible when accessing$o->b.Test
Added
tests/PHPStan/Analyser/nsrt/bug-13902.phpwhich tests both chained ($o->setA()->setB()) and separate ($o->setA(); $o->setB();) method calls with@phpstan-assertannotations, verifying that property types are correctly narrowed in both cases.Fixes phpstan/phpstan#13902