-
-
Notifications
You must be signed in to change notification settings - Fork 336
/
PropertyManipulator.php
330 lines (277 loc) · 11.5 KB
/
PropertyManipulator.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
declare(strict_types=1);
namespace Rector\Core\NodeManipulator;
use Doctrine\ORM\Mapping\ManyToMany;
use Doctrine\ORM\Mapping\Table;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PostDec;
use PhpParser\Node\Expr\PostInc;
use PhpParser\Node\Expr\PreDec;
use PhpParser\Node\Expr\PreInc;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Trait_;
use PhpParser\Node\Stmt\Unset_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Core\PhpParser\AstResolver;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\PhpParser\NodeFinder\PropertyFetchFinder;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\Core\ValueObject\MethodName;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper;
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
use Rector\Php80\NodeAnalyzer\PromotedPropertyResolver;
use Rector\ReadWrite\Guard\VariableToConstantGuard;
use Rector\ReadWrite\NodeAnalyzer\ReadWritePropertyAnalyzer;
use Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector;
use Symplify\PackageBuilder\Php\TypeChecker;
/**
* For inspiration to improve this service,
* @see examples of variable modifications in https://wiki.php.net/rfc/readonly_properties_v2#proposal
*/
final class PropertyManipulator
{
/**
* @var string[]|class-string<Table>[]
*/
private const ALLOWED_NOT_READONLY_ANNOTATION_CLASS_OR_ATTRIBUTES = [
'Doctrine\ORM\Mapping\Entity',
'Doctrine\ORM\Mapping\Table',
'Doctrine\ORM\Mapping\MappedSuperclass',
];
/**
* @var string[]|class-string<ManyToMany>[]
*/
private const ALLOWED_READONLY_ANNOTATION_CLASS_OR_ATTRIBUTES = [
'Doctrine\ORM\Mapping\Id',
'Doctrine\ORM\Mapping\Column',
'Doctrine\ORM\Mapping\OneToMany',
'Doctrine\ORM\Mapping\ManyToMany',
'Doctrine\ORM\Mapping\ManyToOne',
'Doctrine\ORM\Mapping\OneToOne',
'JMS\Serializer\Annotation\Type',
];
public function __construct(
private readonly AssignManipulator $assignManipulator,
private readonly BetterNodeFinder $betterNodeFinder,
private readonly VariableToConstantGuard $variableToConstantGuard,
private readonly ReadWritePropertyAnalyzer $readWritePropertyAnalyzer,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly TypeChecker $typeChecker,
private readonly PropertyFetchFinder $propertyFetchFinder,
private readonly ReflectionResolver $reflectionResolver,
private readonly NodeNameResolver $nodeNameResolver,
private readonly PhpAttributeAnalyzer $phpAttributeAnalyzer,
private readonly NodeTypeResolver $nodeTypeResolver,
private readonly PromotedPropertyResolver $promotedPropertyResolver,
private readonly ConstructorAssignDetector $constructorAssignDetector,
private readonly AstResolver $astResolver,
private readonly PropertyFetchAnalyzer $propertyFetchAnalyzer
) {
}
public function isAllowedReadOnly(Property | Param $propertyOrPromotedParam, PhpDocInfo $phpDocInfo): bool
{
if ($phpDocInfo->hasByAnnotationClasses(self::ALLOWED_READONLY_ANNOTATION_CLASS_OR_ATTRIBUTES)) {
return true;
}
return $this->phpAttributeAnalyzer->hasPhpAttributes(
$propertyOrPromotedParam,
self::ALLOWED_READONLY_ANNOTATION_CLASS_OR_ATTRIBUTES
);
}
public function isPropertyUsedInReadContext(Class_ $class, Property | Param $propertyOrPromotedParam): bool
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($propertyOrPromotedParam);
if ($this->isAllowedReadOnly($propertyOrPromotedParam, $phpDocInfo)) {
return true;
}
$privatePropertyFetches = $this->propertyFetchFinder->findPrivatePropertyFetches(
$class,
$propertyOrPromotedParam
);
foreach ($privatePropertyFetches as $privatePropertyFetch) {
if ($this->readWritePropertyAnalyzer->isRead($privatePropertyFetch)) {
return true;
}
}
// has classLike $this->$variable call?
$classLike = $this->betterNodeFinder->findParentType($propertyOrPromotedParam, ClassLike::class);
if (! $classLike instanceof ClassLike) {
return false;
}
return (bool) $this->betterNodeFinder->findFirst($classLike->stmts, function (Node $node): bool {
if (! $node instanceof PropertyFetch) {
return false;
}
if (! $this->readWritePropertyAnalyzer->isRead($node)) {
return false;
}
return $node->name instanceof Expr;
});
}
public function isPropertyChangeableExceptConstructor(Property | Param $propertyOrParam): bool
{
$class = $this->betterNodeFinder->findParentType($propertyOrParam, Class_::class);
// does not has parent type ClassLike? Possibly parent is changed by other rule
if (! $class instanceof Class_) {
return true;
}
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($class);
if ($phpDocInfo->hasByAnnotationClasses(self::ALLOWED_NOT_READONLY_ANNOTATION_CLASS_OR_ATTRIBUTES)) {
return true;
}
if ($this->phpAttributeAnalyzer->hasPhpAttributes(
$class,
self::ALLOWED_NOT_READONLY_ANNOTATION_CLASS_OR_ATTRIBUTES
)) {
return true;
}
$propertyFetches = $this->propertyFetchFinder->findPrivatePropertyFetches($class, $propertyOrParam);
foreach ($propertyFetches as $propertyFetch) {
if ($this->isChangeableContext($propertyFetch)) {
return true;
}
// skip for constructor? it is allowed to set value in constructor method
$propertyName = (string) $this->nodeNameResolver->getName($propertyFetch);
$classMethod = $this->betterNodeFinder->findParentType($propertyFetch, ClassMethod::class);
if ($this->isPropertyAssignedOnlyInConstructor($class, $propertyName, $classMethod)) {
continue;
}
if ($this->assignManipulator->isLeftPartOfAssign($propertyFetch)) {
return true;
}
$isInUnset = (bool) $this->betterNodeFinder->findParentType($propertyFetch, Unset_::class);
if ($isInUnset) {
return true;
}
}
return false;
}
public function isPropertyChangeable(Class_ $class, Property $property): bool
{
$propertyFetches = $this->propertyFetchFinder->findPrivatePropertyFetches($class, $property);
foreach ($propertyFetches as $propertyFetch) {
if ($this->isChangeableContext($propertyFetch)) {
return true;
}
if ($this->assignManipulator->isLeftPartOfAssign($propertyFetch)) {
return true;
}
}
return false;
}
public function resolveExistingClassPropertyNameByType(Class_ $class, ObjectType $objectType): ?string
{
foreach ($class->getProperties() as $property) {
$propertyType = $this->nodeTypeResolver->getType($property);
if (! $propertyType->equals($objectType)) {
continue;
}
return $this->nodeNameResolver->getName($property);
}
$promotedPropertyParams = $this->promotedPropertyResolver->resolveFromClass($class);
foreach ($promotedPropertyParams as $promotedPropertyParam) {
$paramType = $this->nodeTypeResolver->getType($promotedPropertyParam);
if (! $paramType->equals($objectType)) {
continue;
}
return $this->nodeNameResolver->getName($promotedPropertyParam);
}
return null;
}
public function isUsedByTrait(ClassReflection $classReflection, string $propertyName): bool
{
foreach ($classReflection->getTraits() as $traitUse) {
$trait = $this->astResolver->resolveClassFromName($traitUse->getName());
if (! $trait instanceof Trait_) {
continue;
}
if ($this->propertyFetchAnalyzer->containsLocalPropertyFetchName($trait, $propertyName)) {
return true;
}
}
return false;
}
private function isPropertyAssignedOnlyInConstructor(
Class_ $class,
string $propertyName,
?ClassMethod $classMethod
): bool {
if (! $classMethod instanceof ClassMethod) {
return false;
}
// there is property unset in Test class, so only check on __construct
if (! $this->nodeNameResolver->isName($classMethod->name, MethodName::CONSTRUCT)) {
return false;
}
return $this->constructorAssignDetector->isPropertyAssigned($class, $propertyName);
}
private function isChangeableContext(PropertyFetch | StaticPropertyFetch $propertyFetch): bool
{
$parent = $propertyFetch->getAttribute(AttributeKey::PARENT_NODE);
if (! $parent instanceof Node) {
return false;
}
if ($this->typeChecker->isInstanceOf($parent, [PreInc::class, PreDec::class, PostInc::class, PostDec::class])) {
$parent = $parent->getAttribute(AttributeKey::PARENT_NODE);
}
if (! $parent instanceof Node) {
return false;
}
if ($parent instanceof Arg) {
$readArg = $this->variableToConstantGuard->isReadArg($parent);
if (! $readArg) {
return true;
}
$caller = $parent->getAttribute(AttributeKey::PARENT_NODE);
if ($caller instanceof MethodCall || $caller instanceof StaticCall) {
return $this->isFoundByRefParam($caller);
}
}
if ($parent instanceof ArrayDimFetch) {
return ! $this->readWritePropertyAnalyzer->isRead($propertyFetch);
}
return false;
}
private function isFoundByRefParam(MethodCall | StaticCall $node): bool
{
$functionLikeReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($node);
if ($functionLikeReflection === null) {
return false;
}
$scope = $node->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
return false;
}
$parametersAcceptor = ParametersAcceptorSelectorVariantsWrapper::select(
$functionLikeReflection,
$node,
$scope
);
foreach ($parametersAcceptor->getParameters() as $parameterReflection) {
if ($parameterReflection->passedByReference()->yes()) {
return true;
}
}
return false;
}
}