|
4 | 4 |
|
5 | 5 | namespace Rector\PHPUnit\CodeQuality\Rector\Class_; |
6 | 6 |
|
7 | | -use PhpParser\Modifiers; |
8 | 7 | use PhpParser\Node; |
9 | | -use PhpParser\Node\Expr\Assign; |
10 | | -use PhpParser\Node\Expr\PropertyFetch; |
11 | | -use PhpParser\Node\Expr\StaticPropertyFetch; |
12 | | -use PhpParser\Node\Expr\Variable; |
13 | | -use PhpParser\Node\Identifier; |
14 | 8 | use PhpParser\Node\Stmt\Class_; |
15 | | -use PhpParser\Node\Stmt\ClassMethod; |
16 | | -use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; |
| 9 | +use Rector\Configuration\Deprecation\Contract\DeprecatedInterface; |
| 10 | +use Rector\Exception\ShouldNotHappenException; |
17 | 11 | use Rector\Rector\AbstractRector; |
18 | | -use Rector\ValueObject\MethodName; |
19 | 12 | use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
20 | 13 | use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
21 | 14 |
|
22 | 15 | /** |
23 | | - * @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\SetUpBeforeClassToSetUpRector\SetUpBeforeClassToSetUpRectorTest |
| 16 | + * @deprecated as can break code easily, e.g. if static is required in tear down as well |
24 | 17 | */ |
25 | | -final class SetUpBeforeClassToSetUpRector extends AbstractRector |
| 18 | +final class SetUpBeforeClassToSetUpRector extends AbstractRector implements DeprecatedInterface |
26 | 19 | { |
27 | | - public function __construct( |
28 | | - private readonly TestsNodeAnalyzer $testsNodeAnalyzer, |
29 | | - ) { |
30 | | - } |
31 | | - |
32 | 20 | public function getRuleDefinition(): RuleDefinition |
33 | 21 | { |
34 | 22 | return new RuleDefinition('Change setUpBeforeClass() to setUp() if not needed', [ |
@@ -88,88 +76,9 @@ public function getNodeTypes(): array |
88 | 76 | */ |
89 | 77 | public function refactor(Node $node): ?Node |
90 | 78 | { |
91 | | - $className = $this->getName($node); |
92 | | - if ($className === null) { |
93 | | - return null; |
94 | | - } |
95 | | - |
96 | | - if (! $this->testsNodeAnalyzer->isInTestClass($node)) { |
97 | | - return null; |
98 | | - } |
99 | | - |
100 | | - $setUpBeforeClassMethod = $node->getMethod(MethodName::SET_UP_BEFORE_CLASS); |
101 | | - if (! $setUpBeforeClassMethod instanceof ClassMethod) { |
102 | | - return null; |
103 | | - } |
104 | | - |
105 | | - $changedPropertyNames = []; |
106 | | - |
107 | | - // replace static property fetches |
108 | | - $this->traverseNodesWithCallable($setUpBeforeClassMethod, function (Node $node) use ( |
109 | | - &$changedPropertyNames |
110 | | - ) { |
111 | | - if (! $node instanceof Assign) { |
112 | | - return null; |
113 | | - } |
114 | | - |
115 | | - if (! $node->var instanceof StaticPropertyFetch) { |
116 | | - return null; |
117 | | - } |
118 | | - |
119 | | - $staticPropertyFetch = $node->var; |
120 | | - if (! $this->isName($staticPropertyFetch->class, 'self')) { |
121 | | - return null; |
122 | | - } |
123 | | - |
124 | | - $node->var = new PropertyFetch(new Variable('this'), $staticPropertyFetch->name); |
125 | | - $propertyName = $this->getName($staticPropertyFetch->name); |
126 | | - if (! is_string($propertyName)) { |
127 | | - return null; |
128 | | - } |
129 | | - |
130 | | - $changedPropertyNames[] = $propertyName; |
131 | | - }); |
132 | | - |
133 | | - if ($changedPropertyNames === []) { |
134 | | - return null; |
135 | | - } |
136 | | - |
137 | | - // remove static flag |
138 | | - $setUpBeforeClassMethod->flags -= Modifiers::STATIC; |
139 | | - |
140 | | - // remove public flag |
141 | | - $setUpBeforeClassMethod->flags -= Modifiers::PUBLIC; |
142 | | - |
143 | | - // make protected |
144 | | - $setUpBeforeClassMethod->flags += Modifiers::PROTECTED; |
145 | | - |
146 | | - $setUpBeforeClassMethod->name = new Identifier('setUp'); |
147 | | - |
148 | | - foreach ($node->getProperties() as $property) { |
149 | | - if (! $property->isStatic()) { |
150 | | - continue; |
151 | | - } |
152 | | - |
153 | | - if ($this->isNames($property, $changedPropertyNames)) { |
154 | | - $property->flags -= Modifiers::STATIC; |
155 | | - } |
156 | | - } |
157 | | - |
158 | | - // replace same property access in the class |
159 | | - $this->traverseNodesWithCallable($node->getMethods(), function (Node $node) use ( |
160 | | - $changedPropertyNames |
161 | | - ): ?PropertyFetch { |
162 | | - if (! $node instanceof StaticPropertyFetch) { |
163 | | - return null; |
164 | | - } |
165 | | - |
166 | | - if (! $this->isNames($node, $changedPropertyNames)) { |
167 | | - return null; |
168 | | - } |
169 | | - |
170 | | - return new PropertyFetch(new Variable('this'), $node->name); |
171 | | - }); |
172 | | - |
173 | | - return $node; |
| 79 | + throw new ShouldNotHappenException(sprintf( |
| 80 | + '"%s" is deprecated and should not be used anymore. Remove it from your config files.', |
| 81 | + self::class, |
| 82 | + )); |
174 | 83 | } |
175 | 84 | } |
0 commit comments