Skip to content

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
phpstan-bot:create-pull-request/patch-mshdq66
Open

Fix phpstan/phpstan#13735: don't forget scope-expressions on $this after static method call#5359
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-mshdq66

Conversation

@phpstan-bot
Copy link
Copy Markdown
Collaborator

Summary

Static methods cannot have impact on $this instance properties, so PHPStan should not forget narrowed types on $this->property expressions after a static method call like self::assertTrue(true).

Changes

  • Added bool $fromStaticCall parameter to MutatingScope::invalidateExpression() and MutatingScope::shouldInvalidateExpression() in src/Analyser/MutatingScope.php
  • Added isThisInstancePropertyAccessChain() helper method that checks whether an expression is a pure property/array access chain rooted at $this
  • Updated src/Analyser/ExprHandler/StaticCallHandler.php to pass $methodReflection->isStatic() as the fromStaticCall parameter, ensuring parent::__construct() (non-static) still correctly invalidates $this properties

Root 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 $this context, they cannot modify instance properties. The fix preserves instance property access chains on $this while 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.php which verifies that $this->foo retains its narrowed type (Foo instead of Foo|null) after a self::assertTrue(true) static method call.

Fixes phpstan/phpstan#13735

…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
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.

1 participant