diff --git a/rules-tests/DeadCode/Rector/Property/RemoveDefaultValueFromAssignedPropertyRector/Fixture/skip_assign_in_loop.php.inc b/rules-tests/DeadCode/Rector/Property/RemoveDefaultValueFromAssignedPropertyRector/Fixture/skip_assign_in_loop.php.inc new file mode 100644 index 00000000000..4540d611d3a --- /dev/null +++ b/rules-tests/DeadCode/Rector/Property/RemoveDefaultValueFromAssignedPropertyRector/Fixture/skip_assign_in_loop.php.inc @@ -0,0 +1,23 @@ + $items + */ + public function __construct(iterable $items) { + foreach ($items as $item) { + if ($item > 5) { + $this->x = 1; + } else { + $this->x = -1; + } + } + } +} diff --git a/rules/TypeDeclaration/AlreadyAssignDetector/ConstructorAssignDetector.php b/rules/TypeDeclaration/AlreadyAssignDetector/ConstructorAssignDetector.php index 2061e95a62b..52229f55273 100644 --- a/rules/TypeDeclaration/AlreadyAssignDetector/ConstructorAssignDetector.php +++ b/rules/TypeDeclaration/AlreadyAssignDetector/ConstructorAssignDetector.php @@ -62,7 +62,7 @@ public function isPropertyAssigned(ClassLike $classLike, string $propertyName, b Node $node ) use ($propertyName, &$isAssignedInConstructor, $allowConditional): ?int { if ($this->isIfElseAssign($node, $propertyName)) { - $isAssignedInConstructor = true; + $isAssignedInConstructor = ! (bool) $node->getAttribute(AttributeKey::IS_IN_LOOP_OR_SWITCH); return NodeVisitor::STOP_TRAVERSAL; }