forked from rectorphp/rector-phpunit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYieldDataProviderRector.php
161 lines (134 loc) · 4.52 KB
/
YieldDataProviderRector.php
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
<?php
declare(strict_types=1);
namespace Rector\PHPUnit\CodeQuality\Rector\Class_;
use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Return_;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use Rector\Core\PhpParser\NodeTransformer;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\PHPUnit\NodeFinder\DataProviderClassMethodFinder;
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,
) {
}
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): ?Node
{
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;
}
$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 Return_) {
$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 Return_) {
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_merge((array) $classMethod->stmts, $yields);
}
private function removeReturnTag(ClassMethod $classMethod): void
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classMethod);
$phpDocInfo->removeByType(ReturnTagValueNode::class);
}
}