forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChangeGlobalVariablesToPropertiesRector.php
161 lines (136 loc) · 3.93 KB
/
ChangeGlobalVariablesToPropertiesRector.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\Privatization\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Global_;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PostRector\Collector\PropertyToAddCollector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see https://3v4l.org/DWC4P
*
* @see https://stackoverflow.com/a/12446305/1348344
* @see \Rector\Tests\Privatization\Rector\ClassMethod\ChangeGlobalVariablesToPropertiesRector\ChangeGlobalVariablesToPropertiesRectorTest
*/
final class ChangeGlobalVariablesToPropertiesRector extends AbstractRector
{
/**
* @var string[]
*/
private $globalVariableNames = [];
/**
* @var PropertyToAddCollector
*/
private $propertyToAddCollector;
public function __construct(PropertyToAddCollector $propertyToAddCollector)
{
$this->propertyToAddCollector = $propertyToAddCollector;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change global $variables to private properties',
[
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function go()
{
global $variable;
$variable = 5;
}
public function run()
{
global $variable;
var_dump($variable);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
private $variable;
public function go()
{
$this->variable = 5;
}
public function run()
{
var_dump($this->variable);
}
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [ClassMethod::class];
}
/**
* @param ClassMethod $node
*/
public function refactor(Node $node): ?Node
{
$classLike = $node->getAttribute(AttributeKey::CLASS_NODE);
if (! $classLike instanceof Class_) {
return null;
}
$this->collectGlobalVariableNamesAndRefactorToPropertyFetch($node);
if ($this->globalVariableNames === []) {
return null;
}
foreach ($this->globalVariableNames as $globalVariableName) {
$this->propertyToAddCollector->addPropertyWithoutConstructorToClass($globalVariableName, null, $classLike);
}
return $node;
}
private function collectGlobalVariableNamesAndRefactorToPropertyFetch(ClassMethod $classMethod): void
{
$this->globalVariableNames = [];
$this->traverseNodesWithCallable($classMethod, function (Node $node): ?PropertyFetch {
if ($node instanceof Global_) {
$this->refactorGlobal($node);
return null;
}
if ($node instanceof Variable) {
return $this->refactorGlobalVariable($node);
}
return null;
});
}
private function refactorGlobal(Global_ $global): void
{
foreach ($global->vars as $var) {
$varName = $this->getName($var);
if ($varName === null) {
return;
}
$this->globalVariableNames[] = $varName;
}
$this->removeNode($global);
}
private function refactorGlobalVariable(Variable $variable): ?PropertyFetch
{
if (! $this->isNames($variable, $this->globalVariableNames)) {
return null;
}
// replace with property fetch
$variableName = $this->getName($variable);
if ($variableName === null) {
return null;
}
return $this->nodeFactory->createPropertyFetch('this', $variableName);
}
}