Fix phpstan/phpstan#13735: don't forget scope-expressions on $this after static method call#5359
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Conversation
…atic method call - Static methods cannot modify instance properties on $this, so property fetch expressions on $this should not be invalidated by static calls - Added fromStaticCall parameter to invalidateExpression/shouldInvalidateExpression - Added isThisInstancePropertyAccessChain helper to detect $this->prop chains - Method calls on $this (e.g. $this->getFoo()) are still invalidated since the method body could reference static state - parent::__construct() correctly still invalidates $this properties since the method is not static
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
Static methods cannot have impact on
$thisinstance properties, so PHPStan should not forget narrowed types on$this->propertyexpressions after a static method call likeself::assertTrue(true).Changes
bool $fromStaticCallparameter toMutatingScope::invalidateExpression()andMutatingScope::shouldInvalidateExpression()insrc/Analyser/MutatingScope.phpisThisInstancePropertyAccessChain()helper method that checks whether an expression is a pure property/array access chain rooted at$thissrc/Analyser/ExprHandler/StaticCallHandler.phpto pass$methodReflection->isStatic()as thefromStaticCallparameter, ensuringparent::__construct()(non-static) still correctly invalidates$thispropertiesRoot cause
When a static method call with side effects was processed in
StaticCallHandler, it called$scope->invalidateExpression(new Variable('this'), true, ...)which invalidated ALL expressions containing$this, including simple property fetches like$this->foo. Since static methods have no$thiscontext, they cannot modify instance properties. The fix preserves instance property access chains on$thiswhile still invalidating method call results on$this(since method bodies could reference static state that was modified).Test
Added
tests/PHPStan/Analyser/nsrt/bug-13735.phpwhich verifies that$this->fooretains its narrowed type (Fooinstead ofFoo|null) after aself::assertTrue(true)static method call.Fixes phpstan/phpstan#13735