-
-
Notifications
You must be signed in to change notification settings - Fork 440
Expand file tree
/
Copy pathPhp4ConstructorRector.php
More file actions
210 lines (175 loc) · 6.21 KB
/
Php4ConstructorRector.php
File metadata and controls
210 lines (175 loc) · 6.21 KB
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
<?php
declare(strict_types=1);
namespace Rector\Php70\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use Rector\Enum\ObjectReference;
use Rector\NodeCollector\ScopeResolver\ParentClassScopeResolver;
use Rector\Php70\NodeAnalyzer\MethodCallNameAnalyzer;
use Rector\Php70\NodeAnalyzer\Php4ConstructorClassMethodAnalyzer;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\MethodName;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\Php70\Rector\ClassMethod\Php4ConstructorRector\Php4ConstructorRectorTest
*/
final class Php4ConstructorRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly Php4ConstructorClassMethodAnalyzer $php4ConstructorClassMethodAnalyzer,
private readonly ParentClassScopeResolver $parentClassScopeResolver,
private readonly MethodCallNameAnalyzer $methodCallNameAnalyzer,
) {
}
public function provideMinPhpVersion(): int
{
return PhpVersionFeature::NO_PHP4_CONSTRUCTOR;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change PHP 4 style constructor to `__construct`',
[
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function SomeClass()
{
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function __construct()
{
}
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactor(Node $node): Class_|null
{
$scope = ScopeFetcher::fetch($node);
// catch only classes without namespace
if ($scope->getNamespace() !== null) {
return null;
}
$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return null;
}
$className = $this->getName($node);
if (! is_string($className)) {
return null;
}
foreach ($node->stmts as $classStmtKey => $classStmt) {
if (! $classStmt instanceof ClassMethod) {
continue;
}
if (! $this->php4ConstructorClassMethodAnalyzer->detect($classStmt, $classReflection)) {
continue;
}
$psr4ConstructorMethod = $classStmt;
// process parent call references first
$this->processClassMethodStatementsForParentConstructorCalls($psr4ConstructorMethod, $scope);
// does it already have a __construct method?
if (! $node->getMethod(MethodName::CONSTRUCT) instanceof ClassMethod) {
$psr4ConstructorMethod->name = new Identifier(MethodName::CONSTRUCT);
}
foreach ((array) $psr4ConstructorMethod->stmts as $classMethodStmt) {
if (! $classMethodStmt instanceof Expression) {
continue;
}
// remove delegating method
if ($this->methodCallNameAnalyzer->isLocalMethodCallNamed(
$classMethodStmt->expr,
MethodName::CONSTRUCT
)) {
unset($node->stmts[$classStmtKey]);
}
if ($this->methodCallNameAnalyzer->isParentMethodCall($node, $classMethodStmt->expr)) {
/** @var MethodCall $expr */
$expr = $classMethodStmt->expr;
/** @var string $parentClassName */
$parentClassName = $this->getParentClassName($node);
$classMethodStmt->expr = new StaticCall(new FullyQualified($parentClassName), new Identifier(
MethodName::CONSTRUCT
), $expr->args);
}
}
return $node;
}
return null;
}
private function processClassMethodStatementsForParentConstructorCalls(ClassMethod $classMethod, Scope $scope): void
{
foreach ((array) $classMethod->stmts as $methodStmt) {
if (! $methodStmt instanceof Expression) {
continue;
}
$methodStmt = $methodStmt->expr;
if (! $methodStmt instanceof StaticCall) {
continue;
}
$this->processParentPhp4ConstructCall($methodStmt, $scope);
}
}
private function processParentPhp4ConstructCall(StaticCall $staticCall, Scope $scope): void
{
$parentClassReflection = $this->parentClassScopeResolver->resolveParentClassReflection($scope);
// no parent class
if (! $parentClassReflection instanceof ClassReflection) {
return;
}
if (! $staticCall->class instanceof Name) {
return;
}
// rename ParentClass
if ($this->isName($staticCall->class, $parentClassReflection->getName())) {
$staticCall->class = new Name(ObjectReference::PARENT);
}
if (! $this->isName($staticCall->class, ObjectReference::PARENT)) {
return;
}
// it's not a parent PHP 4 constructor call
if (! $this->isName($staticCall->name, $parentClassReflection->getName())) {
return;
}
$staticCall->name = new Identifier(MethodName::CONSTRUCT);
}
private function getParentClassName(Class_ $class): ?string
{
if (! $class->extends instanceof Node) {
return null;
}
return $class->extends->toString();
}
}