Skip to content

Commit 6960a96

Browse files
authored
[depre] Deprecate SetUpBeforeClassToSetUpRector as can break code easily (#567)
1 parent 9a50a43 commit 6960a96

File tree

8 files changed

+8
-231
lines changed

8 files changed

+8
-231
lines changed

config/sets/phpunit-code-quality.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Rector\PHPUnit\CodeQuality\Rector\Class_\NarrowUnusedSetUpDefinedPropertyRector;
1010
use Rector\PHPUnit\CodeQuality\Rector\Class_\PreferPHPUnitThisCallRector;
1111
use Rector\PHPUnit\CodeQuality\Rector\Class_\RemoveDataProviderParamKeysRector;
12-
use Rector\PHPUnit\CodeQuality\Rector\Class_\SetUpBeforeClassToSetUpRector;
1312
use Rector\PHPUnit\CodeQuality\Rector\Class_\SingleMockPropertyTypeRector;
1413
use Rector\PHPUnit\CodeQuality\Rector\Class_\TestWithToDataProviderRector;
1514
use Rector\PHPUnit\CodeQuality\Rector\Class_\TypeWillReturnCallableArrowFunctionRector;
@@ -120,7 +119,6 @@
120119
RemoveExpectAnyFromMockRector::class,
121120
SingleMockPropertyTypeRector::class,
122121

123-
SetUpBeforeClassToSetUpRector::class,
124122
FinalizeTestCaseClassRector::class,
125123
DeclareStrictTypesTestsRector::class,
126124

rules-tests/CodeQuality/Rector/Class_/SetUpBeforeClassToSetUpRector/Fixture/keep_unrelated_method.php.inc

Lines changed: 0 additions & 21 deletions
This file was deleted.

rules-tests/CodeQuality/Rector/Class_/SetUpBeforeClassToSetUpRector/Fixture/skip_other_property.php.inc

Lines changed: 0 additions & 16 deletions
This file was deleted.

rules-tests/CodeQuality/Rector/Class_/SetUpBeforeClassToSetUpRector/Fixture/some_setup_before_class.php.inc

Lines changed: 0 additions & 47 deletions
This file was deleted.

rules-tests/CodeQuality/Rector/Class_/SetUpBeforeClassToSetUpRector/SetUpBeforeClassToSetUpRectorTest.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

rules-tests/CodeQuality/Rector/Class_/SetUpBeforeClassToSetUpRector/Source/SomeService.php

Lines changed: 0 additions & 8 deletions
This file was deleted.

rules-tests/CodeQuality/Rector/Class_/SetUpBeforeClassToSetUpRector/config/configured_rule.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

rules/CodeQuality/Rector/Class_/SetUpBeforeClassToSetUpRector.php

Lines changed: 8 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,19 @@
44

55
namespace Rector\PHPUnit\CodeQuality\Rector\Class_;
66

7-
use PhpParser\Modifiers;
87
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;
148
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;
1711
use Rector\Rector\AbstractRector;
18-
use Rector\ValueObject\MethodName;
1912
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2013
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
2114

2215
/**
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
2417
*/
25-
final class SetUpBeforeClassToSetUpRector extends AbstractRector
18+
final class SetUpBeforeClassToSetUpRector extends AbstractRector implements DeprecatedInterface
2619
{
27-
public function __construct(
28-
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
29-
) {
30-
}
31-
3220
public function getRuleDefinition(): RuleDefinition
3321
{
3422
return new RuleDefinition('Change setUpBeforeClass() to setUp() if not needed', [
@@ -88,88 +76,9 @@ public function getNodeTypes(): array
8876
*/
8977
public function refactor(Node $node): ?Node
9078
{
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+
));
17483
}
17584
}

0 commit comments

Comments
 (0)