forked from rectorphp/rector-phpunit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstructClassMethodToSetUpTestCaseRector.php
More file actions
171 lines (142 loc) · 4.92 KB
/
Copy pathConstructClassMethodToSetUpTestCaseRector.php
File metadata and controls
171 lines (142 loc) · 4.92 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
declare(strict_types=1);
namespace Rector\PHPUnit\CodeQuality\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use Rector\Core\NodeAnalyzer\ClassAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\MethodName;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PHPUnit\NodeAnalyzer\SetUpMethodDecorator;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://github.com/sebastianbergmann/phpunit/issues/3975#issuecomment-562584609
*
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\ConstructClassMethodToSetUpTestCaseRector\ConstructClassMethodToSetUpTestCaseRectorTest
*/
final class ConstructClassMethodToSetUpTestCaseRector extends AbstractRector
{
public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
private readonly ClassAnalyzer $classAnalyzer,
private readonly VisibilityManipulator $visibilityManipulator,
private readonly SetUpMethodDecorator $setUpMethodDecorator
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change __construct() method in tests of `PHPUnit\Framework\TestCase` to setUp(), to prevent dangerous override',
[
new CodeSample(
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
private $someValue;
public function __construct(?string $name = null, array $data = [], string $dataName = '')
{
$this->someValue = 1000;
parent::__construct($name, $data, $dataName);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
final class SomeTest extends TestCase
{
private $someValue;
protected function setUp()
{
parent::setUp();
$this->someValue = 1000;
}
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
return null;
}
$constructClassMethod = $node->getMethod(MethodName::CONSTRUCT);
if (! $constructClassMethod instanceof ClassMethod) {
return null;
}
if ($this->classAnalyzer->isAnonymousClass($node)) {
return null;
}
$addedStmts = $this->resolveStmtsToAddToSetUp($constructClassMethod);
$setUpClassMethod = $node->getMethod(MethodName::SET_UP);
if (! $setUpClassMethod instanceof ClassMethod) {
// no setUp() method yet, rename it to setUp :)
$constructClassMethod->name = new Identifier(MethodName::SET_UP);
$constructClassMethod->params = [];
$constructClassMethod->stmts = $addedStmts;
$this->setUpMethodDecorator->decorate($constructClassMethod);
$this->visibilityManipulator->makeProtected($constructClassMethod);
} else {
$stmtKey = $constructClassMethod->getAttribute(AttributeKey::STMT_KEY);
unset($node->stmts[$stmtKey]);
$setUpClassMethod->stmts = array_merge((array) $setUpClassMethod->stmts, $addedStmts);
}
return $node;
}
/**
* @return Stmt[]
*/
private function resolveStmtsToAddToSetUp(ClassMethod $constructClassMethod): array
{
$constructorStmts = (array) $constructClassMethod->stmts;
// remove parent call
foreach ($constructorStmts as $key => $constructorStmt) {
if ($constructorStmt instanceof Expression) {
$constructorStmt = clone $constructorStmt->expr;
}
if (! $this->isParentCallNamed($constructorStmt, MethodName::CONSTRUCT)) {
continue;
}
unset($constructorStmts[$key]);
}
return $constructorStmts;
}
private function isParentCallNamed(Node $node, string $desiredMethodName): bool
{
if (! $node instanceof StaticCall) {
return false;
}
if ($node->class instanceof Expr) {
return false;
}
if (! $this->nodeNameResolver->isName($node->class, 'parent')) {
return false;
}
if ($node->name instanceof Expr) {
return false;
}
return $this->nodeNameResolver->isName($node->name, $desiredMethodName);
}
}