Skip to content

Commit 3ecc536

Browse files
authored
[DeadCode] Skip array dim fetch assign in RemoveDefaultValueFromAssignedPropertyRector (#8212)
1 parent fafa915 commit 3ecc536

3 files changed

Lines changed: 62 additions & 1 deletion

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Property\RemoveDefaultValueFromAssignedPropertyRector\Fixture;
4+
5+
final class SkipArrayDimFetchAssign
6+
{
7+
private array $default = [
8+
'name' => 'some name',
9+
'tplset' => null,
10+
];
11+
12+
public function __construct()
13+
{
14+
$this->default['tplset'] = 'dotty';
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Property\RemoveDefaultValueFromAssignedPropertyRector\Fixture;
4+
5+
final class SkipNestedArrayDimFetchAssign
6+
{
7+
private array $config = [
8+
'first' => ['second' => null],
9+
];
10+
11+
public function __construct()
12+
{
13+
$this->config['first']['second'] = 'value';
14+
}
15+
}

‎rules/DeadCode/Rector/Property/RemoveDefaultValueFromAssignedPropertyRector.php‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Expr;
9+
use PhpParser\Node\Expr\ArrayDimFetch;
10+
use PhpParser\Node\Expr\Assign;
911
use PhpParser\Node\Stmt\Class_;
1012
use PhpParser\Node\Stmt\ClassMethod;
1113
use PhpParser\Node\Stmt\Return_;
14+
use Rector\NodeAnalyzer\PropertyFetchAnalyzer;
1215
use Rector\PhpParser\Node\BetterNodeFinder;
1316
use Rector\Rector\AbstractRector;
1417
use Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector;
@@ -23,7 +26,8 @@ final class RemoveDefaultValueFromAssignedPropertyRector extends AbstractRector
2326
{
2427
public function __construct(
2528
private readonly ConstructorAssignDetector $constructorAssignDetector,
26-
private readonly BetterNodeFinder $betterNodeFinder
29+
private readonly BetterNodeFinder $betterNodeFinder,
30+
private readonly PropertyFetchAnalyzer $propertyFetchAnalyzer
2731
) {
2832
}
2933

@@ -106,6 +110,11 @@ public function refactor(Node $node): ?Node
106110
continue;
107111
}
108112

113+
// partial assign, e.g. $this->items['key'] = ...; keeps the default value required
114+
if ($this->isAssignedViaArrayDimFetch($node, $propertyName)) {
115+
continue;
116+
}
117+
109118
$propertyProperty->default = null;
110119
$hasChanged = true;
111120
}
@@ -117,4 +126,25 @@ public function refactor(Node $node): ?Node
117126

118127
return null;
119128
}
129+
130+
private function isAssignedViaArrayDimFetch(Class_ $class, string $propertyName): bool
131+
{
132+
return $this->betterNodeFinder->findFirst($class, function (Node $subNode) use ($propertyName): bool {
133+
if (! $subNode instanceof Assign) {
134+
return false;
135+
}
136+
137+
$assignedExpr = $subNode->var;
138+
if (! $assignedExpr instanceof ArrayDimFetch) {
139+
return false;
140+
}
141+
142+
// unwrap nested dims, e.g. $this->items['first']['second'] = ...
143+
while ($assignedExpr instanceof ArrayDimFetch) {
144+
$assignedExpr = $assignedExpr->var;
145+
}
146+
147+
return $this->propertyFetchAnalyzer->isLocalPropertyFetchName($assignedExpr, $propertyName);
148+
}) instanceof Assign;
149+
}
120150
}

0 commit comments

Comments
 (0)