forked from rectorphp/rector-phpunit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYieldDataProviderRector.php
More file actions
208 lines (175 loc) · 6.37 KB
/
YieldDataProviderRector.php
File metadata and controls
208 lines (175 loc) · 6.37 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
declare(strict_types=1);
namespace Rector\PHPUnit\CodeQuality\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\YieldFrom;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\Type\Generic\GenericObjectType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\DeadCode\NodeAnalyzer\IsClassMethodUsedAnalyzer;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\NodeTransformer;
use Rector\PHPStan\ScopeFetcher;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\PHPUnit\NodeFinder\DataProviderClassMethodFinder;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://medium.com/tech-tajawal/use-memory-gently-with-yield-in-php-7e62e2480b8d
* @changelog https://3v4l.org/5PJid
*
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\YieldDataProviderRector\YieldDataProviderRectorTest
*/
final class YieldDataProviderRector extends AbstractRector
{
public function __construct(
private readonly NodeTransformer $nodeTransformer,
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
private readonly DataProviderClassMethodFinder $dataProviderClassMethodFinder,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly IsClassMethodUsedAnalyzer $isClassMethodUsedAnalyzer,
private readonly PhpDocTypeChanger $phpDocTypeChanger,
private readonly DocBlockUpdater $docBlockUpdater
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Turns array return to yield in data providers', [
new CodeSample(
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
final class SomeTest implements TestCase
{
public static function provideData()
{
return [
['some text']
];
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use PHPUnit\Framework\TestCase;
final class SomeTest implements TestCase
{
public static function provideData()
{
yield ['some text'];
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Class_
{
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
return null;
}
$hasChanged = false;
$dataProviderClassMethods = $this->dataProviderClassMethodFinder->find($node);
foreach ($dataProviderClassMethods as $dataProviderClassMethod) {
$array = $this->collectReturnArrayNodesFromClassMethod($dataProviderClassMethod);
if (! $array instanceof Array_) {
continue;
}
$scope = ScopeFetcher::fetch($node);
if ($this->isClassMethodUsedAnalyzer->isClassMethodUsed($node, $dataProviderClassMethod, $scope)) {
continue;
}
$this->transformArrayToYieldsOnMethodNode($dataProviderClassMethod, $array);
$hasChanged = true;
}
if ($hasChanged) {
return $node;
}
return null;
}
private function collectReturnArrayNodesFromClassMethod(ClassMethod $classMethod): ?Array_
{
if ($classMethod->stmts === null) {
return null;
}
foreach ($classMethod->stmts as $statement) {
if ($statement instanceof Expression) {
$statement = $statement->expr;
}
if ($statement instanceof Return_ || $statement instanceof YieldFrom) {
$returnedExpr = $statement->expr;
if (! $returnedExpr instanceof Array_) {
return null;
}
return $returnedExpr;
}
}
return null;
}
private function transformArrayToYieldsOnMethodNode(ClassMethod $classMethod, Array_ $array): void
{
$yields = $this->nodeTransformer->transformArrayToYields($array);
$this->removeReturnTag($classMethod);
// change return typehint
$classMethod->returnType = new FullyQualified('Iterator');
$commentReturn = [];
foreach ((array) $classMethod->stmts as $key => $classMethodStmt) {
if ($classMethodStmt instanceof Expression) {
$classMethodStmt = $classMethodStmt->expr;
}
if (
! $classMethodStmt instanceof Return_
&& ! $classMethodStmt instanceof YieldFrom
) {
continue;
}
$commentReturn = $classMethodStmt->getAttribute(AttributeKey::COMMENTS) ?? [];
unset($classMethod->stmts[$key]);
}
if (isset($yields[0])) {
$yields[0]->setAttribute(
AttributeKey::COMMENTS,
array_merge($commentReturn, $yields[0]->getAttribute(AttributeKey::COMMENTS) ?? [])
);
}
$classMethod->stmts = [...(array) $classMethod->stmts, ...$yields];
}
private function removeReturnTag(ClassMethod $classMethod): void
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classMethod);
if (! $phpDocInfo->getReturnTagValue() instanceof ReturnTagValueNode) {
return;
}
if ($phpDocInfo->getReturnType()->isArray()->yes()) {
$keyType = $phpDocInfo->getReturnType()
->getIterableKeyType();
$itemType = $phpDocInfo->getReturnType()
->getIterableValueType();
$this->phpDocTypeChanger->changeReturnType(
$classMethod,
$phpDocInfo,
new GenericObjectType('Iterator', [$keyType, $itemType])
);
} else {
$phpDocInfo->removeByType(ReturnTagValueNode::class);
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($classMethod);
}
}
}