|
5 | 5 | namespace Rector\PHPUnit\CodeQuality\Rector\Class_; |
6 | 6 |
|
7 | 7 | use PhpParser\Node; |
8 | | -use PhpParser\Node\Expr; |
9 | | -use PhpParser\Node\Expr\Array_; |
10 | | -use PhpParser\Node\Expr\Yield_; |
11 | 8 | use PhpParser\Node\Stmt\Class_; |
12 | | -use PhpParser\Node\Stmt\Expression; |
13 | | -use PhpParser\Node\Stmt\Return_; |
14 | | -use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; |
15 | | -use Rector\PHPUnit\NodeFinder\DataProviderClassMethodFinder; |
| 9 | +use Rector\Configuration\Deprecation\Contract\DeprecatedInterface; |
| 10 | +use Rector\Exception\ShouldNotHappenException; |
16 | 11 | use Rector\Rector\AbstractRector; |
17 | 12 | use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
18 | 13 | use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
19 | 14 |
|
20 | 15 | /** |
21 | | - * @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\RemoveDataProviderParamKeysRector\RemoveDataProviderParamKeysRectorTest |
| 16 | + * @deprecated as the keys can be useful to improve test readability |
| 17 | + * |
| 18 | + * @see https://github.com/rectorphp/rector-phpunit/pull/469#issuecomment-3533161836 |
22 | 19 | */ |
23 | | -final class RemoveDataProviderParamKeysRector extends AbstractRector |
| 20 | +final class RemoveDataProviderParamKeysRector extends AbstractRector implements DeprecatedInterface |
24 | 21 | { |
25 | | - public function __construct( |
26 | | - private readonly TestsNodeAnalyzer $testsNodeAnalyzer, |
27 | | - private readonly DataProviderClassMethodFinder $dataProviderClassMethodFinder, |
28 | | - ) { |
29 | | - } |
30 | | - |
31 | 22 | public function getRuleDefinition(): RuleDefinition |
32 | 23 | { |
33 | 24 | return new RuleDefinition('Remove data provider keys, that should match param names, as order is good enough', [ |
@@ -89,77 +80,9 @@ public function getNodeTypes(): array |
89 | 80 | */ |
90 | 81 | public function refactor(Node $node): ?Node |
91 | 82 | { |
92 | | - if (! $this->testsNodeAnalyzer->isInTestClass($node)) { |
93 | | - return null; |
94 | | - } |
95 | | - |
96 | | - $dataProviderClassMethods = $this->dataProviderClassMethodFinder->find($node); |
97 | | - $hasChanged = false; |
98 | | - |
99 | | - foreach ($dataProviderClassMethods as $dataProviderClassMethod) { |
100 | | - // 1. remove array keys |
101 | | - foreach ((array) $dataProviderClassMethod->stmts as $classMethodStmt) { |
102 | | - if (! $classMethodStmt instanceof Return_) { |
103 | | - continue; |
104 | | - } |
105 | | - |
106 | | - if (! $classMethodStmt->expr instanceof Array_) { |
107 | | - continue; |
108 | | - } |
109 | | - |
110 | | - $returnedArray = $classMethodStmt->expr; |
111 | | - foreach ($returnedArray->items as $arrayItem) { |
112 | | - if (! $arrayItem->value instanceof Array_) { |
113 | | - continue; |
114 | | - } |
115 | | - |
116 | | - $nestedArray = $arrayItem->value; |
117 | | - foreach ($nestedArray->items as $nestedArrayItem) { |
118 | | - if (! $nestedArrayItem->key instanceof Expr) { |
119 | | - continue; |
120 | | - } |
121 | | - |
122 | | - // remove key |
123 | | - $nestedArrayItem->key = null; |
124 | | - $hasChanged = true; |
125 | | - } |
126 | | - |
127 | | - } |
128 | | - } |
129 | | - |
130 | | - // 2. remove yield keys |
131 | | - foreach ((array) $dataProviderClassMethod->stmts as $classMethodStmt) { |
132 | | - if (! $classMethodStmt instanceof Expression) { |
133 | | - continue; |
134 | | - } |
135 | | - |
136 | | - if (! $classMethodStmt->expr instanceof Yield_) { |
137 | | - continue; |
138 | | - } |
139 | | - |
140 | | - $yield = $classMethodStmt->expr; |
141 | | - if (! $yield->value instanceof Array_) { |
142 | | - continue; |
143 | | - } |
144 | | - |
145 | | - $yieldArray = $yield->value; |
146 | | - |
147 | | - foreach ($yieldArray->items as $yieldArrayItem) { |
148 | | - if (! $yieldArrayItem->key instanceof Expr) { |
149 | | - continue; |
150 | | - } |
151 | | - |
152 | | - // remove key |
153 | | - $yieldArrayItem->key = null; |
154 | | - $hasChanged = true; |
155 | | - } |
156 | | - } |
157 | | - } |
158 | | - |
159 | | - if ($hasChanged === false) { |
160 | | - return null; |
161 | | - } |
162 | | - |
163 | | - return $node; |
| 83 | + throw new ShouldNotHappenException(sprintf( |
| 84 | + '"%s" is deprecated and should not be used anymore. Remove it from your config files.', |
| 85 | + self::class, |
| 86 | + )); |
164 | 87 | } |
165 | 88 | } |
0 commit comments