-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathCallOnAppArrayAccessToStandaloneAssignRector.php
148 lines (127 loc) · 4.15 KB
/
CallOnAppArrayAccessToStandaloneAssignRector.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
<?php
declare(strict_types=1);
namespace RectorLaravel\Rector\Assign;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Nop;
use PHPStan\Type\ObjectType;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\PhpParser\Node\Value\ValueResolver;
use RectorLaravel\AbstractRector;
use RectorLaravel\NodeFactory\AppAssignFactory;
use RectorLaravel\ValueObject\ServiceNameTypeAndVariableName;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \RectorLaravel\Tests\Rector\Assign\CallOnAppArrayAccessToStandaloneAssignRector\CallOnAppArrayAccessToStandaloneAssignRectorTest
*/
final class CallOnAppArrayAccessToStandaloneAssignRector extends AbstractRector
{
/**
* @var ServiceNameTypeAndVariableName[]
*/
private array $serviceNameTypeAndVariableNames = [];
public function __construct(
private readonly AppAssignFactory $appAssignFactory,
private readonly DocBlockUpdater $docBlockUpdater,
private readonly ValueResolver $valueResolver,
) {
$this->serviceNameTypeAndVariableNames[] = new ServiceNameTypeAndVariableName(
'validator',
'Illuminate\Validation\Factory',
'validationFactory'
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Expression::class];
}
/**
* @param Expression $node
* @return array<int, Node>|null
*/
public function refactor(Node $node): ?array
{
if (! $node->expr instanceof Assign) {
return null;
}
if (! $node->expr->expr instanceof MethodCall) {
return null;
}
$methodCall = $node->expr->expr;
if (! $methodCall->var instanceof ArrayDimFetch) {
return null;
}
$arrayDimFetch = $methodCall->var;
if (! $this->isObjectType(
$arrayDimFetch->var,
new ObjectType('Illuminate\Contracts\Foundation\Application')
)) {
return null;
}
$arrayDimFetchDim = $methodCall->var->dim;
if (! $arrayDimFetchDim instanceof Expr) {
return null;
}
foreach ($this->serviceNameTypeAndVariableNames as $serviceNameTypeAndVariableName) {
if (! $this->valueResolver->isValue($arrayDimFetchDim, $serviceNameTypeAndVariableName->getServiceName())) {
continue;
}
$assignExpression = $this->appAssignFactory->createAssignExpression(
$serviceNameTypeAndVariableName,
$methodCall->var
);
$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($assignExpression);
$methodCall->var = new Variable($serviceNameTypeAndVariableName->getVariableName());
// the nop is a workaround because the docs of the first node are somehow stripped away
// this will add a newline but the docs will be preserved
return [new Nop, $assignExpression, $node];
}
return null;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Replace magical call on $this->app["something"] to standalone type assign variable',
[new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
/**
* @var \Illuminate\Contracts\Foundation\Application
*/
private $app;
public function run()
{
$validator = $this->app['validator']->make('...');
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
/**
* @var \Illuminate\Contracts\Foundation\Application
*/
private $app;
public function run()
{
/** @var \Illuminate\Validation\Factory $validationFactory */
$validationFactory = $this->app['validator'];
$validator = $validationFactory->make('...');
}
}
CODE_SAMPLE
)]
);
}
}