diff --git a/rules-tests/DeadCode/Rector/Property/RemoveDefaultValueFromAssignedPropertyRector/Fixture/skip_nested_conditional_assign.php.inc b/rules-tests/DeadCode/Rector/Property/RemoveDefaultValueFromAssignedPropertyRector/Fixture/skip_nested_conditional_assign.php.inc new file mode 100644 index 00000000000..c01cc6597c4 --- /dev/null +++ b/rules-tests/DeadCode/Rector/Property/RemoveDefaultValueFromAssignedPropertyRector/Fixture/skip_nested_conditional_assign.php.inc @@ -0,0 +1,21 @@ +a = 'a'; + } else { + $this->a = 'b'; + } + } + + echo $this->a; + } +} diff --git a/rules/TypeDeclaration/AlreadyAssignDetector/ConstructorAssignDetector.php b/rules/TypeDeclaration/AlreadyAssignDetector/ConstructorAssignDetector.php index 52229f55273..66da7da3146 100644 --- a/rules/TypeDeclaration/AlreadyAssignDetector/ConstructorAssignDetector.php +++ b/rules/TypeDeclaration/AlreadyAssignDetector/ConstructorAssignDetector.php @@ -132,6 +132,11 @@ private function isIfElseAssign(Node $node, string $propertyName): bool return false; } + // nested if/else does not guarantee the assign, the outer condition can skip it + if ($node->getAttribute(AttributeKey::IS_FIRST_LEVEL_STATEMENT) !== true) { + return false; + } + return $this->isAssignedInStmts($node->stmts, $propertyName) && $this->isAssignedInStmts( $node->else->stmts, $propertyName