Skip to content

Look up by-reference parameters of methods called on typed properties of $this - #7238

Merged
ondrejmirtes merged 1 commit into
2.3.xfrom
keep-properties-passed-to-property-methods
Nov 28, 2026
Merged

ondrejmirtes merged 1 commit into
2.3.xfrom
keep-properties-passed-to-property-methods

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

PreservedPropertiesVisitor treated every argument of a method call on another object as a possible by-reference write, because the callee's signature is not known while the file is parsed. A narrowed private property passed to $this->finder->find($this->id) was therefore forgotten after a call on $this that runs that code.

When the receiver is a property of $this, the argument is now recorded like the arguments of static calls, new and functions, as 'Class::$property->method'. PreservedPropertiesResolver looks up the method on the property's declared type, with null removed so ?Finder and ?-> work too. Overrides cannot change whether a parameter is by reference. The property is still treated as written when the type has no class (mixed, object), the method is magic, or the call is in a trait or an anonymous class.

Neither class is shadowed by turbo-ext. The full test suite and make phpstan pass.

Closes phpstan/phpstan#15868

Amp thread: https://ampcode.com/threads/T-01a22093-364f-7598-b2ca-80a68e13ef9e

… of $this

PreservedPropertiesVisitor treated every argument of a method call on another
object as a possible by-reference write, so a narrowed property passed to
$this->dependency->method() was forgotten after a call on $this that runs that
code. When the receiver is a property of $this, the argument is now recorded
like the ones of static calls and functions, and PreservedPropertiesResolver
checks the parameter of the method on the property's declared type.

Amp-Thread-ID: https://ampcode.com/threads/T-01a22093-364f-7598-b2ca-80a68e13ef9e
@ondrejmirtes
ondrejmirtes merged commit d380653 into 2.3.x Nov 28, 2026
462 of 468 checks passed
@ondrejmirtes
ondrejmirtes deleted the keep-properties-passed-to-property-methods branch November 28, 2026 05:57
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.

Narrowed private property forgotten after a $this call that passes it to another object's method (since phpstan-src 45b569220, #7119)

1 participant