From 7479c15a3bcf32c90a232c64a6046f20b3ef2018 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 27 Jul 2026 20:34:33 +0700 Subject: [PATCH] [DeadCode] Skip if else in loop on RemoveDefaultValueFromAssignedPropertyRector --- .../Fixture/skip_assign_in_loop.php.inc | 23 +++++++++++++++++++ .../ConstructorAssignDetector.php | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 rules-tests/DeadCode/Rector/Property/RemoveDefaultValueFromAssignedPropertyRector/Fixture/skip_assign_in_loop.php.inc 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; }