|
4 | 4 | namespace Rector\CodeQuality\Rector\Foreach_; |
5 | 5 |
|
6 | 6 | use PhpParser\Node; |
7 | | -use PhpParser\Node\ArrayItem; |
8 | | -use PhpParser\Node\Expr; |
9 | | -use PhpParser\Node\Expr\List_; |
10 | | -use PhpParser\Node\Expr\Variable; |
11 | | -use PhpParser\Node\Stmt\Foreach_; |
12 | | -use Rector\DeadCode\NodeAnalyzer\ExprUsedInNodeAnalyzer; |
13 | | -use Rector\NodeManipulator\StmtsManipulator; |
| 7 | +use Rector\Configuration\Deprecation\Contract\DeprecatedInterface; |
| 8 | +use Rector\Exception\ShouldNotHappenException; |
14 | 9 | use Rector\PhpParser\Enum\NodeGroup; |
15 | | -use Rector\PhpParser\Node\BetterNodeFinder; |
16 | 10 | use Rector\Rector\AbstractRector; |
17 | 11 | use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
18 | 12 | use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
19 | 13 | /** |
20 | | - * @see \Rector\Tests\CodeQuality\Rector\Foreach_\UnusedForeachValueToArrayKeysRector\UnusedForeachValueToArrayKeysRectorTest |
| 14 | + * @deprecated This rule is deprecated, as it is a personal preference. The array_keys() wrap makes the foreach harder to read and harder to extend, once the value is needed again. |
21 | 15 | */ |
22 | | -final class UnusedForeachValueToArrayKeysRector extends AbstractRector |
| 16 | +final class UnusedForeachValueToArrayKeysRector extends AbstractRector implements DeprecatedInterface |
23 | 17 | { |
24 | | - /** |
25 | | - * @readonly |
26 | | - */ |
27 | | - private ExprUsedInNodeAnalyzer $exprUsedInNodeAnalyzer; |
28 | | - /** |
29 | | - * @readonly |
30 | | - */ |
31 | | - private BetterNodeFinder $betterNodeFinder; |
32 | | - /** |
33 | | - * @readonly |
34 | | - */ |
35 | | - private StmtsManipulator $stmtsManipulator; |
36 | | - public function __construct(ExprUsedInNodeAnalyzer $exprUsedInNodeAnalyzer, BetterNodeFinder $betterNodeFinder, StmtsManipulator $stmtsManipulator) |
37 | | - { |
38 | | - $this->exprUsedInNodeAnalyzer = $exprUsedInNodeAnalyzer; |
39 | | - $this->betterNodeFinder = $betterNodeFinder; |
40 | | - $this->stmtsManipulator = $stmtsManipulator; |
41 | | - } |
42 | 18 | public function getRuleDefinition(): RuleDefinition |
43 | 19 | { |
44 | 20 | return new RuleDefinition('Change foreach with unused $value but only $key, to array_keys()', [new CodeSample(<<<'CODE_SAMPLE' |
@@ -79,106 +55,6 @@ public function getNodeTypes(): array |
79 | 55 | */ |
80 | 56 | public function refactor(Node $node): ?Node |
81 | 57 | { |
82 | | - $stmts = $node->stmts; |
83 | | - if ($stmts === null) { |
84 | | - return null; |
85 | | - } |
86 | | - $hasChanged = \false; |
87 | | - foreach ($stmts as $key => $stmt) { |
88 | | - if (!$stmt instanceof Foreach_) { |
89 | | - continue; |
90 | | - } |
91 | | - if (!$stmt->keyVar instanceof Expr) { |
92 | | - continue; |
93 | | - } |
94 | | - if (!$this->nodeTypeResolver->getNativeType($stmt->expr)->isArray()->yes()) { |
95 | | - continue; |
96 | | - } |
97 | | - // special case of nested array items |
98 | | - if ($stmt->valueVar instanceof List_) { |
99 | | - $valueArray = $this->refactorArrayForeachValue($stmt->valueVar, $stmt); |
100 | | - if (!$valueArray instanceof List_) { |
101 | | - continue; |
102 | | - } |
103 | | - $stmt->valueVar = $valueArray; |
104 | | - // not sure what does this mean :) |
105 | | - if ($valueArray->items !== []) { |
106 | | - continue; |
107 | | - } |
108 | | - $hasChanged = \true; |
109 | | - $this->removeForeachValueAndUseArrayKeys($stmt, $stmt->keyVar); |
110 | | - continue; |
111 | | - } |
112 | | - if (!$stmt->valueVar instanceof Variable) { |
113 | | - continue; |
114 | | - } |
115 | | - if ($this->isVariableUsedInForeach($stmt->valueVar, $stmt)) { |
116 | | - continue; |
117 | | - } |
118 | | - if ($this->stmtsManipulator->isVariableUsedInNextStmt($node, $key + 1, (string) $this->getName($stmt->valueVar))) { |
119 | | - continue; |
120 | | - } |
121 | | - $hasChanged = \true; |
122 | | - $this->removeForeachValueAndUseArrayKeys($stmt, $stmt->keyVar); |
123 | | - } |
124 | | - if (!$hasChanged) { |
125 | | - return null; |
126 | | - } |
127 | | - return $node; |
128 | | - } |
129 | | - /** |
130 | | - * @param int[] $removedKeys |
131 | | - */ |
132 | | - private function isArrayItemsRemovalWithoutChangingOrder(List_ $list, array $removedKeys): bool |
133 | | - { |
134 | | - $hasRemovingStarted = \false; |
135 | | - foreach (array_keys($list->items) as $key) { |
136 | | - if (in_array($key, $removedKeys, \true)) { |
137 | | - $hasRemovingStarted = \true; |
138 | | - } elseif ($hasRemovingStarted) { |
139 | | - // we cannot remove the previous item, and not remove the next one, because that would change the order |
140 | | - return \false; |
141 | | - } |
142 | | - } |
143 | | - return \true; |
144 | | - } |
145 | | - private function refactorArrayForeachValue(List_ $list, Foreach_ $foreach): ?List_ |
146 | | - { |
147 | | - // only last items can be removed, without changing the order |
148 | | - $removedKeys = []; |
149 | | - foreach ($list->items as $key => $arrayItem) { |
150 | | - if (!$arrayItem instanceof ArrayItem) { |
151 | | - // only known values can be processes |
152 | | - return null; |
153 | | - } |
154 | | - $value = $arrayItem->value; |
155 | | - if (!$value instanceof Variable) { |
156 | | - // only variables can be processed |
157 | | - return null; |
158 | | - } |
159 | | - if ($this->isVariableUsedInForeach($value, $foreach)) { |
160 | | - continue; |
161 | | - } |
162 | | - $removedKeys[] = $key; |
163 | | - } |
164 | | - if (!$this->isArrayItemsRemovalWithoutChangingOrder($list, $removedKeys)) { |
165 | | - return null; |
166 | | - } |
167 | | - // clear removed items |
168 | | - foreach ($removedKeys as $removedKey) { |
169 | | - unset($list->items[$removedKey]); |
170 | | - } |
171 | | - return $list; |
172 | | - } |
173 | | - private function isVariableUsedInForeach(Variable $variable, Foreach_ $foreach): bool |
174 | | - { |
175 | | - return (bool) $this->betterNodeFinder->findFirst($foreach->stmts, fn(Node $node): bool => $this->exprUsedInNodeAnalyzer->isUsed($node, $variable)); |
176 | | - } |
177 | | - private function removeForeachValueAndUseArrayKeys(Foreach_ $foreach, Expr $keyVarExpr): void |
178 | | - { |
179 | | - // remove key value |
180 | | - $foreach->valueVar = $keyVarExpr; |
181 | | - $foreach->keyVar = null; |
182 | | - $foreach->expr = $this->nodeFactory->createFuncCall('array_keys', [$foreach->expr]); |
| 58 | + throw new ShouldNotHappenException(sprintf('"%s" rule is deprecated, as it is a personal preference that makes foreach harder to read and extend', self::class)); |
183 | 59 | } |
184 | 60 | } |
0 commit comments