Skip to content

Keep remembered property types across calls on $this that cannot write them - #7119

Open
ondrejmirtes wants to merge 9 commits into
2.3.xfrom
preserve-properties-across-method-calls
Open

ondrejmirtes wants to merge 9 commits into
2.3.xfrom
preserve-properties-across-method-calls

Conversation

@ondrejmirtes

@ondrejmirtes ondrejmirtes commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

An impure method call on $this used to forget every remembered property type of $this. Now it keeps the properties no code the call can run writes.

final class Foo
{
	private bool $foo = false;
	private bool $bar = false;

	public function run(): void
	{
		$this->foo = true;
		$this->writeBar();
		\PHPStan\dumpType($this->foo); // true, was bool
	}

	private function writeBar(): void
	{
		echo 'x';
		$this->bar = true;
	}
}

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() and static::m() call. MethodCallHandler and StaticCallHandler pass them to MutatingScope::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.

  • A call that can leave the class (an inherited or overridable method, 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.
  • Nothing is kept when the class uses a trait or a call reaches a trait method, when $this escapes ([$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:

Project Without Turbo With Turbo
PHPStan itself +0.1% −0.1%
WordPress +0.1% +0.8%
Dolibarr −0.2% +0.3%
mautic +1.3% 0.0%
TYPO3 +1.1% +2.1%

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-64 moves 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

…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
@staabm

staabm commented Nov 22, 2026

Copy link
Copy Markdown
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
if ($this->isFunctionArgumentByValue($functionNames, $position)) {
continue;
}
$propertyNames[$propertyName] = true;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not covered by tests

return true;
}

return false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not covered by tests

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

Labels

None yet

Projects

None yet

2 participants