-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathLumenRoutesStringMiddlewareToArrayRector.php
154 lines (128 loc) · 4.44 KB
/
LumenRoutesStringMiddlewareToArrayRector.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 RectorLaravel\Rector\MethodCall;
use PhpParser\Node;
use PhpParser\Node\ArrayItem;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Scalar\String_;
use RectorLaravel\AbstractRector;
use RectorLaravel\NodeAnalyzer\LumenRouteRegisteringMethodAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \RectorLaravel\Tests\Rector\MethodCall\LumenRoutesStringMiddlewareToArrayRector\LumenRoutesStringMiddlewareToArrayRectorTest
*/
final class LumenRoutesStringMiddlewareToArrayRector extends AbstractRector
{
public function __construct(
private readonly LumenRouteRegisteringMethodAnalyzer $lumenRouteRegisteringMethodAnalyzer
) {}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Changes middlewares from rule definitions from string to array notation.',
[new CodeSample(<<<'CODE_SAMPLE'
$router->get('/user', ['middleware => 'test']);
$router->post('/user', ['middleware => 'test|authentication']);
CODE_SAMPLE
, <<<'CODE_SAMPLE'
$router->get('/user', ['middleware => ['test']]);
$router->post('/user', ['middleware => ['test', 'authentication']]);
CODE_SAMPLE)]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
}
public function refactor(Node $node): ?Node
{
if (! $node instanceof MethodCall) {
return null;
}
if (! $this->lumenRouteRegisteringMethodAnalyzer->isLumenRoutingClass($node)) {
return null;
}
$array = $this->getRouterMethodAttributes($node);
if (! $array instanceof Array_) {
return null;
}
$arrayItem = $this->findItemInArrayByKey($array, 'middleware');
if (! $arrayItem instanceof ArrayItem) {
return null;
}
$middlewareValue = $arrayItem->value;
if (! $middlewareValue instanceof String_) {
return null;
}
/** @var string $middlewareString */
$middlewareString = $middlewareValue->value;
$splitMiddleware = explode('|', $middlewareString);
$newMiddlewareArray = new Array_([]);
foreach ($splitMiddleware as $singleSplitMiddleware) {
$newMiddlewareArray->items[] = new ArrayItem(new String_($singleSplitMiddleware));
}
$this->replaceItemInArrayByKey(
$array,
new ArrayItem($newMiddlewareArray, new String_('middleware')),
'middleware'
);
return $node;
}
private function getRouterMethodAttributes(MethodCall $methodCall): ?Array_
{
$attributes = null;
if ($this->lumenRouteRegisteringMethodAnalyzer->isRoutesRegisterGroup($methodCall->name)) {
$attributes = $methodCall->getArgs()[0]
->value;
}
if ($this->lumenRouteRegisteringMethodAnalyzer->isRoutesRegisterRoute($methodCall->name)) {
$attributes = $methodCall->getArgs()[1]
->value;
}
if (! $attributes instanceof Array_) {
return null;
}
return $attributes;
}
private function findItemInArrayByKey(Array_ $array, string $keyName): ?ArrayItem
{
foreach ($array->items as $i => $item) {
if (! $item instanceof ArrayItem) {
continue;
}
if (! $this->hasKeyName($item, $keyName)) {
continue;
}
$foundArrayItem = $array->items[$i];
if (! $foundArrayItem instanceof ArrayItem) {
continue;
}
return $item;
}
return null;
}
private function replaceItemInArrayByKey(Array_ $array, ArrayItem $arrayItem, string $keyName): void
{
foreach ($array->items as $i => $item) {
if (! $item instanceof ArrayItem) {
continue;
}
if (! $this->hasKeyName($item, $keyName)) {
continue;
}
$array->items[$i] = $arrayItem;
}
}
private function hasKeyName(ArrayItem $arrayItem, string $name): bool
{
if (! $arrayItem->key instanceof String_) {
return false;
}
return $arrayItem->key->value === $name;
}
}