Skip to content

Commit fafa915

Browse files
authored
[DeadCode] Skip if else in loop on RemoveDefaultValueFromAssignedPropertyRector (#8211)
1 parent 4853fdb commit fafa915

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Property\RemoveDefaultValueFromAssignedPropertyRector\Fixture;
4+
5+
use stdClass;
6+
7+
final class SkipAssignInLoop
8+
{
9+
public int $x = 0;
10+
11+
/**
12+
* @param iterable<int> $items
13+
*/
14+
public function __construct(iterable $items) {
15+
foreach ($items as $item) {
16+
if ($item > 5) {
17+
$this->x = 1;
18+
} else {
19+
$this->x = -1;
20+
}
21+
}
22+
}
23+
}

‎rules/TypeDeclaration/AlreadyAssignDetector/ConstructorAssignDetector.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function isPropertyAssigned(ClassLike $classLike, string $propertyName, b
6262
Node $node
6363
) use ($propertyName, &$isAssignedInConstructor, $allowConditional): ?int {
6464
if ($this->isIfElseAssign($node, $propertyName)) {
65-
$isAssignedInConstructor = true;
65+
$isAssignedInConstructor = ! (bool) $node->getAttribute(AttributeKey::IS_IN_LOOP_OR_SWITCH);
6666
return NodeVisitor::STOP_TRAVERSAL;
6767
}
6868

0 commit comments

Comments
 (0)