Repository navigation
Keep remembered property types across calls on $this that cannot write them - #7119
Open
ondrejmirtes wants to merge 9 commits into
Open
ondrejmirtes wants to merge 9 commits into
ondrejmirtes wants to merge 9 commits into
Conversation
…e them A rich parser visitor reads each class's own code once while the file is parsed and attaches to every $this->m(), self::m() and static::m() call the properties no code the call can run writes. MethodCallHandler and StaticCallHandler keep the remembered types of those properties when they invalidate $this after an impure call. Only the class's own code is read. A call that can leave the class - an inherited or overridable method, parent::, another object, a function or new - forgets every public and protected property, and private ones its followed code, the class's closures or the methods it hands out as first-class callables write. Traits, closures, $this escaping, dynamic names, magic __get/__set/__call and hooked properties keep nothing. Amp-Thread-ID: https://ampcode.com/threads/T-01a1ffd7-32dc-7043-b3ac-2d8cf42d2441
…rtiesVisitor AnonymousClassVisitor replaces an anonymous class with an AnonymousClassNode that has no namespacedName. Whether PreservedPropertiesVisitor sees the original node or the replacement depends on the order the container lists the visitors in. Amp-Thread-ID: https://ampcode.com/threads/T-01a1ffd7-32dc-7043-b3ac-2d8cf42d2441
Contributor
|
While in this area there might also be room to fix phpstan/phpstan#15785 |
A same-class call that is not known to be impure now forgets the properties the class's own code it runs writes. Once the class lets $this out, a call that runs code outside the class keeps no property, private ones included. Amp-Thread-ID: https://ampcode.com/threads/T-01a1ffd7-32dc-7043-b3ac-2d8cf42d2441
staabm
reviewed
Nov 22, 2026
| if ($this->isFunctionArgumentByValue($functionNames, $position)) { | ||
| continue; | ||
| } | ||
| $propertyNames[$propertyName] = true; |
Contributor
There was a problem hiding this comment.
this line is not covered by tests
Comment on lines
+108
to
+111
| if (!$variant->isVariadic() || count($parameters) === 0) { | ||
| continue; | ||
| } | ||
| $parameter = $parameters[count($parameters) - 1]; |
| return true; | ||
| } | ||
|
|
||
| return false; |
…ver is assigned Assigning $obj->prop now also forgets $obj->m()->x, $obj->m()['k'] and the conditional expressions about or conditioned on them, not only $obj->m(). Closes phpstan/phpstan#15785 Amp-Thread-ID: https://ampcode.com/threads/T-01a1ffd7-32dc-7043-b3ac-2d8cf42d2441
…lyImpureFunctionValues is off Closes phpstan/phpstan#15734 Amp-Thread-ID: https://ampcode.com/threads/T-01a1ffd7-32dc-7043-b3ac-2d8cf42d2441
… assigned property's receiver
…AssignPropertyFetch Amp-Thread-ID: https://ampcode.com/threads/T-01a1ffd7-32dc-7043-b3ac-2d8cf42d2441
…ls on an assigned property's receiver" This reverts commit e16c914. It saved about 2 µs per assignment, which is not measurable in analysis time.
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.
An impure method call on
$thisused to forget every remembered property type of$this. Now it keeps the properties no code the call can run writes.How
PreservedPropertiesVisitor, a rich parser visitor, reads each class's own code once while the file is parsed. It attaches the preserved property names to every$this->m(),self::m()andstatic::m()call.MethodCallHandlerandStaticCallHandlerpass them toMutatingScope::invalidateExpression(). No other file is read, so there is no cross-file parsing, nothing in vendor/ is looked at, and nothing is added to the result cache.parent::, another object, a function,new) forgets every public and protected property. It also forgets private properties written by the followed code, by the class's closures, and by the methods the class hands out as first-class callables.$thisescapes ([$this, 'm'], passed on), and for dynamic names,__get/__set/__call, hooked properties and calls inside closures.Performance
Medians of interleaved runs, with the result cache cleared before every run, change in user CPU:
Two private projects, also checked, were within ±2.5%. Per-round deltas spread wider than every value above. RSS is unchanged everywhere, and error output on all projects is identical to the base commit.
Tally:
TypeCombinatorBench::benchIntersectBenevolentUnion@types-64moves because the boot allocations land differently in the heap. The patch's code does not run in it: removing the visitor's service registration restores the base numbers exactly, and natively the peak memory is byte-identical. The +64 KiB memory on some AnalysisBench cases has the same cause. The +1–1.7% instructions on the large AnalysisBench files are the visitor's parsing work.The visitor has no native Turbo mirror. It already measures neutral with Turbo, which is below the 0.5% gain turbo-ext/CLAUDE.md requires for a port.
Closes phpstan/phpstan#6603
Closes phpstan/phpstan#15734
Closes phpstan/phpstan#15785