-
-
Notifications
You must be signed in to change notification settings - Fork 336
/
RemoveUnusedNonEmptyArrayBeforeForeachRector.php
133 lines (115 loc) · 3.65 KB
/
RemoveUnusedNonEmptyArrayBeforeForeachRector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
declare(strict_types=1);
namespace Rector\DeadCode\Rector\If_;
use PhpParser\Node;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\If_;
use PHPStan\Type\ArrayType;
use Rector\Core\NodeManipulator\IfManipulator;
use Rector\Core\Php\ReservedKeywordAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Rector\DeadCode\NodeManipulator\CountManipulator;
use Rector\DeadCode\UselessIfCondBeforeForeachDetector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\DeadCode\Rector\If_\RemoveUnusedNonEmptyArrayBeforeForeachRector\RemoveUnusedNonEmptyArrayBeforeForeachRectorTest
*/
final class RemoveUnusedNonEmptyArrayBeforeForeachRector extends AbstractRector
{
public function __construct(
private readonly CountManipulator $countManipulator,
private readonly IfManipulator $ifManipulator,
private readonly UselessIfCondBeforeForeachDetector $uselessIfCondBeforeForeachDetector,
private readonly ReservedKeywordAnalyzer $reservedKeywordAnalyzer
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Remove unused if check to non-empty array before foreach of the array',
[
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$values = [];
if ($values !== []) {
foreach ($values as $value) {
echo $value;
}
}
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$values = [];
foreach ($values as $value) {
echo $value;
}
}
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [If_::class];
}
/**
* @param If_ $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isUselessBeforeForeachCheck($node)) {
return null;
}
$stmt = $node->stmts[0];
$ifComments = $node->getAttribute(AttributeKey::COMMENTS) ?? [];
$stmtComments = $stmt->getAttribute(AttributeKey::COMMENTS) ?? [];
$comments = array_merge($ifComments, $stmtComments);
$stmt->setAttribute(AttributeKey::COMMENTS, $comments);
return $stmt;
}
private function isUselessBeforeForeachCheck(If_ $if): bool
{
if (! $this->ifManipulator->isIfWithOnly($if, Foreach_::class)) {
return false;
}
/** @var Foreach_ $foreach */
$foreach = $if->stmts[0];
$foreachExpr = $foreach->expr;
if ($foreachExpr instanceof Variable) {
$variableName = $this->nodeNameResolver->getName($foreachExpr);
if (is_string($variableName) && $this->reservedKeywordAnalyzer->isNativeVariable($variableName)) {
return false;
}
}
if ($this->uselessIfCondBeforeForeachDetector->isMatchingNotIdenticalEmptyArray($if, $foreachExpr)) {
return true;
}
if ($this->uselessIfCondBeforeForeachDetector->isMatchingNotEmpty($if, $foreachExpr)) {
return true;
}
// we know it's an array
$condType = $this->getType($if->cond);
if ($condType instanceof ArrayType) {
return true;
}
return $this->countManipulator->isCounterHigherThanOne($if->cond, $foreachExpr);
}
}