Fix phpstan/phpstan#5473: TypeSpecifier should be called in processExprNode#5342
Closed
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Closed
Fix phpstan/phpstan#5473: TypeSpecifier should be called in processExprNode#5342phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
…ression calls - In TypeSpecifier, when processing a MethodCall with null context, also recursively process the var (object) expression to pick up assertions from intermediate calls like Assert::notNull($value)->throws(...) - For BooleanAnd with null context, use unionWith instead of intersectWith to properly combine assertions from both sides of && - New regression test in tests/PHPStan/Analyser/nsrt/bug-5473.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 a function/method call with
@phpstan-assertwas used as a sub-expression (e.g.,Assert::notNull($value)->throws(...)), the type narrowing from the assertion was lost. Similarly,assert($x !== null) && assert(strlen($x) > 1)as an expression statement did not properly combine both assertions.Changes
src/Analyser/TypeSpecifier.php:$expr->varto apply assertions from intermediate calls. This handles chained patterns likeAssert::notNull($value)->throws(...).unionWithinstead ofintersectWithto properly combine assertions from both sides of&&.Root cause
The TypeSpecifier only examined the outermost call in a chain when processing statement-level expressions with null context. For
Assert::notNull($value)->throws(...), it checkedthrows()for assertions (finding none) but never examined the intermediateAssert::notNull($value)call which carried the@phpstan-assert !null $valueannotation.For the
&&case,intersectWithwas used for null context which takes the union of types (broader) for common entries and drops entries not in both — losing narrowing information. UsingunionWith(which intersects types for common entries and keeps all unique entries) correctly combines both assertions.Test
Added
tests/PHPStan/Analyser/nsrt/bug-5473.phpcovering:assert() && assert()expression statement (was broken, now fixed)Fixes phpstan/phpstan#5473