Skip to content

Initialize properties through a constructor callee that an override may replace - #7148

Merged
ondrejmirtes merged 1 commit into
2.3.xfrom
initialize-in-overridable-constructor-callees
Nov 25, 2026
Merged

ondrejmirtes merged 1 commit into
2.3.xfrom
initialize-in-overridable-constructor-callees

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

Since #7119, a method a subclass can override no longer initialized the properties it assigns when the constructor called it. This reported code that is not always wrong:

class Fee
{
	private float $amount;

	public function __construct(float $amount)
	{
		$this->setAmount($amount);
	}

	protected function setAmount(float $amount): void
	{
		$this->amount = $amount; // "Class Fee has an uninitialized property $amount."
	}
}

An override of setAmount() can call parent::setAmount(), so PHPStan cannot be certain the property stays uninitialized. A call on $this now takes the initialized properties from the walk of the class's method even when an override may run instead. The types of the properties it writes are still taken only when the call runs exactly that method (private, final, in a final class, or through self::).

This restores the pre-#7119 expectations for InitializedInPublicSetterNonFinalClass, TestAdditionalConstructor::$four and bug-9577's SpecializedException (a constructor reached through a trait alias), and adds the reported shape as InitializedInProtectedSetterThatMayBeOverridden.

…ay replace

A method of the class the constructor calls on $this initialized properties
only when the call ran exactly that method. When a subclass could override it,
the properties it assigns were reported as uninitialized, and reads after the
call as accesses to an uninitialized property. An override can call the
method through parent::, so the report is not always right. The call now takes
the initialized properties from the method's walk either way; the types of the
properties it writes are still taken only when it runs exactly that method.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1ffd7-32dc-7043-b3ac-2d8cf42d2441
@ondrejmirtes
ondrejmirtes merged commit a0defb9 into 2.3.x Nov 25, 2026
558 of 564 checks passed
@ondrejmirtes
ondrejmirtes deleted the initialize-in-overridable-constructor-callees branch November 25, 2026 10:16

}

class InitializedInProtectedSetterThatMayBeOverridden

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.

does/should it make a difference when the class is final (or the setter method is final), so cannot be overridden?

https://phpstan.org/r/4959e937-b6e0-4f46-84c4-5f6ebdf6c2bb

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I relaxed this a bit here. Yeah, in proper code you should rely on $this->amount being initialized only if setAmount is private or final. But this led to annoying behaviour (as many other edge cases around properties). I found several cases on real-world projects that overridden setAmount in a subclass but there wasn't an actual bug because they called parent::setAmount().

So: in ideal world, this case should report an error, and final case should not, but for now neither of them report an error.

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.

2 participants