forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYieldClassMethodToArrayClassMethodRector.php
154 lines (132 loc) · 4.13 KB
/
YieldClassMethodToArrayClassMethodRector.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
<?php
declare(strict_types=1);
namespace Rector\CodingStyle\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Expr\Yield_;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Type\ObjectType;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\PhpParser\NodeTransformer;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see https://medium.com/tech-tajawal/use-memory-gently-with-yield-in-php-7e62e2480b8d
* @see https://3v4l.org/5PJid
*
* @see \Rector\Tests\CodingStyle\Rector\ClassMethod\YieldClassMethodToArrayClassMethodRector\YieldClassMethodToArrayClassMethodRectorTest
*/
final class YieldClassMethodToArrayClassMethodRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var string
*/
public const METHODS_BY_TYPE = 'methods_by_type';
/**
* @var array<class-string, string[]>
*/
private $methodsByType = [];
/**
* @var NodeTransformer
*/
private $nodeTransformer;
/**
* @param array<class-string, string[]> $methodsByType
*/
public function __construct(NodeTransformer $nodeTransformer, array $methodsByType = [])
{
$this->methodsByType = $methodsByType;
$this->nodeTransformer = $nodeTransformer;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Turns yield return to array return in specific type and method', [
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
class SomeEventSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
yield 'event' => 'callback';
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeEventSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return ['event' => 'callback'];
}
}
CODE_SAMPLE
,
[
self::METHODS_BY_TYPE => [
'EventSubscriberInterface' => ['getSubscribedEvents'],
],
]
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [ClassMethod::class];
}
/**
* @param ClassMethod $node
*/
public function refactor(Node $node): ?Node
{
foreach ($this->methodsByType as $type => $methods) {
if (! $this->isObjectType($node, new ObjectType($type))) {
continue;
}
foreach ($methods as $method) {
if (! $this->isName($node, $method)) {
continue;
}
$yieldNodes = $this->collectYieldNodesFromClassMethod($node);
if ($yieldNodes === []) {
continue;
}
$arrayNode = $this->nodeTransformer->transformYieldsToArray($yieldNodes);
$this->removeNodes($yieldNodes);
$node->returnType = new Identifier('array');
$returnExpression = new Return_($arrayNode);
$node->stmts = array_merge((array) $node->stmts, [$returnExpression]);
}
}
return $node;
}
public function configure(array $configuration): void
{
$this->methodsByType = $configuration[self::METHODS_BY_TYPE] ?? [];
}
/**
* @return Yield_[]
*/
private function collectYieldNodesFromClassMethod(ClassMethod $classMethod): array
{
$yieldNodes = [];
if ($classMethod->stmts === null) {
return [];
}
foreach ($classMethod->stmts as $statement) {
if (! $statement instanceof Expression) {
continue;
}
if ($statement->expr instanceof Yield_) {
$yieldNodes[] = $statement->expr;
}
}
return $yieldNodes;
}
}