Repository navigation
Initialize properties through a constructor callee that an override may replace - #7148
Merged
ondrejmirtes merged 1 commit intoNov 25, 2026
Merged
Conversation
…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
deleted the
initialize-in-overridable-constructor-callees
branch
November 25, 2026 10:16
staabm
reviewed
Nov 25, 2026
|
|
||
| } | ||
|
|
||
| class InitializedInProtectedSetterThatMayBeOverridden |
Contributor
There was a problem hiding this comment.
does/should it make a difference when the class is final (or the setter method is final), so cannot be overridden?
Member
Author
There was a problem hiding this comment.
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.
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.
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:
An override of
setAmount()can callparent::setAmount(), so PHPStan cannot be certain the property stays uninitialized. A call on$thisnow 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 throughself::).This restores the pre-#7119 expectations for
InitializedInPublicSetterNonFinalClass,TestAdditionalConstructor::$fourand bug-9577'sSpecializedException(a constructor reached through a trait alias), and adds the reported shape asInitializedInProtectedSetterThatMayBeOverridden.