Skip to content

Fix phpstan/phpstan#13902: Chaining phpstan-assert fails to assert types#5127

Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-1xsa495
Open

Fix phpstan/phpstan#13902: Chaining phpstan-assert fails to assert types#5127
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-1xsa495

Conversation

@phpstan-bot
Copy link
Collaborator

Summary

When calling methods with @phpstan-assert annotations 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->var is itself a MethodCall, the inner call is also processed and both results are combined. The recursion is guarded to only apply when $expr->var instanceof MethodCall to avoid breaking ImpossibleCheckTypeHelper's rootExpr tracking.

  • 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->b on 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:

  1. Missing recursion: TypeSpecifier::specifyTypesInCondition() only processed the outermost method call's assertions. For $o->setA()->setB(), only setB()'s assertions were applied. The inner setA() call was never examined for its @phpstan-assert annotations.

  2. Wrong $this resolution: When setB() was called on $o->setA(), the $this in setB()'s assertion mapped to $o->setA() (the intermediate expression). This created a type narrowing on the expression key $o->setA()->b instead of $o->b, so the narrowing was invisible when accessing $o->b.

Test

Added tests/PHPStan/Analyser/nsrt/bug-13902.php which tests both chained ($o->setA()->setB()) and separate ($o->setA(); $o->setB();) method calls with @phpstan-assert annotations, verifying that property types are correctly narrowed in both cases.

Fixes phpstan/phpstan#13902

- 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants