-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathSessionVariableToSessionFacadeRector.php
230 lines (194 loc) · 6.2 KB
/
SessionVariableToSessionFacadeRector.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
namespace RectorLaravel\Rector\ArrayDimFetch;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Isset_;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Unset_;
use PhpParser\NodeVisitor;
use RectorLaravel\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \RectorLaravel\Tests\Rector\ArrayDimFetch\SessionVariableToSessionFacadeRector\SessionVariableToSessionFacadeRectorTest
*/
class SessionVariableToSessionFacadeRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change PHP session usage to Session Facade methods',
[new CodeSample(
<<<'CODE_SAMPLE'
$_SESSION['key'];
$_SESSION['key'] = 'value';
$_SESSION;
session_regenerate_id();
session_unset();
session_destroy();
session_start();
unset($_SESSION['key']);
isset($_SESSION['key'])
CODE_SAMPLE,
<<<'CODE_SAMPLE'
\Illuminate\Support\Facades\Session::get('key');
\Illuminate\Support\Facades\Session::put('key', 'value');
\Illuminate\Support\Facades\Session::all();
\Illuminate\Support\Facades\Session::regenerate();
\Illuminate\Support\Facades\Session::flush();
\Illuminate\Support\Facades\Session::destroy();
\Illuminate\Support\Facades\Session::start();
\Illuminate\Support\Facades\Session::forget('key');
\Illuminate\Support\Facades\Session::has('key');
CODE_SAMPLE
)]
);
}
public function getNodeTypes(): array
{
return [
Isset_::class,
Unset_::class,
ArrayDimFetch::class,
Assign::class,
FuncCall::class,
Variable::class,
];
}
/**
* @param ArrayDimFetch|Assign|FuncCall|Isset_|Unset_|Variable $node
* @return StaticCall|Expression|1|null
*/
public function refactor(Node $node): StaticCall|Expression|int|null
{
if ($node instanceof ArrayDimFetch) {
return $this->processDimFetch($node);
}
if ($node instanceof FuncCall) {
return $this->processFunction($node);
}
if ($node instanceof Isset_) {
return $this->processIsset($node);
}
if ($node instanceof Unset_) {
$return = $this->processUnset($node);
if ($return instanceof StaticCall) {
return new Expression($return);
}
return $return;
}
if ($node instanceof Variable) {
return $this->processVariable($node);
}
return $this->processAssign($node);
}
/**
* @return StaticCall|1|null
*/
public function processDimFetch(ArrayDimFetch $arrayDimFetch): StaticCall|int|null
{
if (! $this->isName($arrayDimFetch->var, '_SESSION')) {
return null;
}
if (! $arrayDimFetch->dim instanceof Expr) {
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
}
return $this->nodeFactory->createStaticCall('Illuminate\Support\Facades\Session', 'get', [
new Arg($arrayDimFetch->dim),
]);
}
private function processAssign(Assign $assign): ?StaticCall
{
$dimFetch = $assign->var;
if (! $dimFetch instanceof ArrayDimFetch || ! $this->isName($dimFetch->var, '_SESSION')) {
return null;
}
if (! $dimFetch->dim instanceof Expr) {
return null;
}
return $this->nodeFactory->createStaticCall('Illuminate\Support\Facades\Session', 'put', [
new Arg($dimFetch->dim),
new Arg($assign->expr),
]);
}
private function processFunction(FuncCall $funcCall): ?StaticCall
{
if (! $this->isNames($funcCall, [
'session_regenerate_id',
'session_unset',
'session_destroy',
'session_start',
])) {
return null;
}
$method = $this->getName($funcCall);
$replacementMethod = match ($method) {
'session_regenerate_id' => 'regenerate',
'session_unset' => 'flush',
'session_destroy' => 'destroy',
'session_start' => 'start',
default => null,
};
if ($replacementMethod === null) {
return null;
}
return $this->nodeFactory->createStaticCall('Illuminate\Support\Facades\Session', $replacementMethod);
}
/**
* @return StaticCall|1|null
*/
private function processIsset(Isset_ $isset): StaticCall|int|null
{
if (count($isset->vars) < 1) {
return null;
}
$var = $isset->vars[0];
if (! $var instanceof ArrayDimFetch) {
return null;
}
if (! $this->isName($var->var, '_SESSION')) {
return null;
}
if (! $var->dim instanceof Expr) {
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
}
return $this->nodeFactory->createStaticCall('Illuminate\Support\Facades\Session', 'has', [
new Arg($var->dim),
]);
}
/**
* @return StaticCall|1|null
*/
private function processUnset(Unset_ $unset): StaticCall|int|null
{
if (count($unset->vars) < 1) {
return null;
}
$var = $unset->vars[0];
if (! $var instanceof ArrayDimFetch) {
return null;
}
if (! $this->isName($var->var, '_SESSION')) {
return null;
}
if (! $var->dim instanceof Expr) {
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
}
return $this->nodeFactory->createStaticCall('Illuminate\Support\Facades\Session', 'forget', [
new Arg($var->dim),
]);
}
private function processVariable(Variable $variable): ?StaticCall
{
if (! $this->isName($variable, '_SESSION')) {
return null;
}
return $this->nodeFactory->createStaticCall('Illuminate\Support\Facades\Session', 'all');
}
}