[DeadCode] Skip early return in constructor in RemoveDefaultValueFromAssignedPropertyRector#8209
Merged
Merged
Conversation
…AssignedPropertyRector
TomasVotruba
force-pushed
the
fix-early-return-default-value
branch
from
July 27, 2026 08:39
42b98e6 to
45e7ee3
Compare
Contributor
|
properties might also be initialized from methods which are considered 'additionalConstructors'. I think most robust would be to base this rule on the PHPStan builtin mechanics which already can identify uninitialized properties, see e.g. https://github.com/phpstan/phpstan-src/blob/d7e9e6b1a05d4c6bdefc771f4cf3b12122daa1ad/src/Rules/Properties/MissingReadOnlyPropertyAssignRule.php |
Member
Author
|
I know about the rule, but it's very complex. I wish there was single re-usable service. Asking agents if they can do something perf-wise about it. |
Contributor
|
there is |
Member
Author
|
That's not a printable node we can use, only a wrapper. |
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.
The rule removed the default value whenever a first-level
$this->prop = ...was found in the constructor. But an earlyreturncan skip that assign, leaving a typed property uninitialized:Before this fix, the rule produced:
class Foo { - public int $x = 0; + public int $x; public function __construct(bool $return = false) {which turns
(new Foo(true))->xintoError: Typed property Foo::$x must not be accessed before initialization.Now any
returnin the constructor body makes the rule bail out. Returns inside nested closures/arrow functions/anonymous classes are ignored, as they do not skip the constructor assign: