-
-
Notifications
You must be signed in to change notification settings - Fork 688
/
NewInInitializerRector.php
191 lines (188 loc) · 6.43 KB
/
NewInInitializerRector.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
<?php
declare (strict_types=1);
namespace Rector\Php81\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Coalesce;
use PhpParser\Node\NullableType;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PHPStan\Reflection\ClassReflection;
use Rector\FamilyTree\NodeAnalyzer\ClassChildAnalyzer;
use Rector\NodeManipulator\StmtsManipulator;
use Rector\Php81\NodeAnalyzer\CoalesePropertyAssignMatcher;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
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\Php81\Rector\ClassMethod\NewInInitializerRector\NewInInitializerRectorTest
*/
final class NewInInitializerRector extends AbstractRector implements MinPhpVersionInterface
{
/**
* @readonly
*/
private ReflectionResolver $reflectionResolver;
/**
* @readonly
*/
private ClassChildAnalyzer $classChildAnalyzer;
/**
* @readonly
*/
private CoalesePropertyAssignMatcher $coalesePropertyAssignMatcher;
/**
* @readonly
*/
private StmtsManipulator $stmtsManipulator;
public function __construct(ReflectionResolver $reflectionResolver, ClassChildAnalyzer $classChildAnalyzer, CoalesePropertyAssignMatcher $coalesePropertyAssignMatcher, StmtsManipulator $stmtsManipulator)
{
$this->reflectionResolver = $reflectionResolver;
$this->classChildAnalyzer = $classChildAnalyzer;
$this->coalesePropertyAssignMatcher = $coalesePropertyAssignMatcher;
$this->stmtsManipulator = $stmtsManipulator;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Replace property declaration of new state with direct new', [new CodeSample(<<<'CODE_SAMPLE'
class SomeClass
{
private Logger $logger;
public function __construct(
?Logger $logger = null,
) {
$this->logger = $logger ?? new NullLogger;
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeClass
{
public function __construct(
private Logger $logger = new NullLogger,
) {
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactor(Node $node) : ?Node
{
if ($node->stmts === null || $node->stmts === []) {
return null;
}
if ($node->isAbstract() || $node->isAnonymous()) {
return null;
}
$constructClassMethod = $node->getMethod(MethodName::CONSTRUCT);
if (!$constructClassMethod instanceof ClassMethod) {
return null;
}
$params = $this->resolveParams($constructClassMethod);
if ($params === []) {
return null;
}
$hasChanged = \false;
foreach ((array) $constructClassMethod->stmts as $key => $stmt) {
foreach ($params as $param) {
$paramName = $this->getName($param);
$coalesce = $this->coalesePropertyAssignMatcher->matchCoalesceAssignsToLocalPropertyNamed($stmt, $paramName);
if (!$coalesce instanceof Coalesce) {
continue;
}
if ($this->stmtsManipulator->isVariableUsedInNextStmt($constructClassMethod, $key + 1, $paramName)) {
continue;
}
/** @var NullableType $currentParamType */
$currentParamType = $param->type;
$param->type = $currentParamType->type;
$param->default = $coalesce->right;
unset($constructClassMethod->stmts[$key]);
$this->processPropertyPromotion($node, $param, $paramName);
$hasChanged = \true;
}
}
if ($hasChanged) {
return $node;
}
return null;
}
public function provideMinPhpVersion() : int
{
return PhpVersionFeature::NEW_INITIALIZERS;
}
/**
* @return Param[]
*/
private function resolveParams(ClassMethod $classMethod) : array
{
$params = $this->matchConstructorParams($classMethod);
if ($params === []) {
return [];
}
if ($this->isOverrideAbstractMethod($classMethod)) {
return [];
}
return $params;
}
private function isOverrideAbstractMethod(ClassMethod $classMethod) : bool
{
$classReflection = $this->reflectionResolver->resolveClassReflection($classMethod);
$methodName = $this->nodeNameResolver->getName($classMethod);
return $classReflection instanceof ClassReflection && $this->classChildAnalyzer->hasAbstractParentClassMethod($classReflection, $methodName);
}
private function processPropertyPromotion(Class_ $class, Param $param, string $paramName) : void
{
foreach ($class->stmts as $key => $stmt) {
if (!$stmt instanceof Property) {
continue;
}
$property = $stmt;
if (!$this->isName($stmt, $paramName)) {
continue;
}
$param->flags = $property->flags;
$param->attrGroups = \array_merge($property->attrGroups, $param->attrGroups);
unset($class->stmts[$key]);
}
}
/**
* @return Param[]
*/
private function matchConstructorParams(ClassMethod $classMethod) : array
{
// skip empty constructor assigns, as we need those here
if ($classMethod->stmts === null || $classMethod->stmts === []) {
return [];
}
$params = \array_filter($classMethod->params, static fn(Param $param): bool => $param->type instanceof NullableType);
if ($params === []) {
return $params;
}
$totalParams = \count($classMethod->params);
foreach (\array_keys($params) as $key) {
for ($iteration = $key + 1; $iteration < $totalParams; ++$iteration) {
if (isset($classMethod->params[$iteration]) && !$classMethod->params[$iteration]->default instanceof Expr) {
return [];
}
}
}
return $params;
}
}