-
Notifications
You must be signed in to change notification settings - Fork 568
Expand file tree
/
Copy pathReadOnlyPropertyAssignRule.php
More file actions
130 lines (111 loc) · 4.13 KB
/
ReadOnlyPropertyAssignRule.php
File metadata and controls
130 lines (111 loc) · 4.13 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
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Properties;
use ArrayAccess;
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Node\Expr\SetOffsetValueTypeExpr;
use PHPStan\Node\Expr\UnsetOffsetExpr;
use PHPStan\Node\PropertyAssignNode;
use PHPStan\Reflection\ConstructorsHelper;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\ObjectType;
use PHPStan\Type\TypeUtils;
use function in_array;
use function sprintf;
use function strtolower;
/**
* @implements Rule<PropertyAssignNode>
*/
#[RegisteredRule(level: 3)]
final class ReadOnlyPropertyAssignRule implements Rule
{
public function __construct(
private PropertyReflectionFinder $propertyReflectionFinder,
private ConstructorsHelper $constructorsHelper,
)
{
}
public function getNodeType(): string
{
return PropertyAssignNode::class;
}
public function processNode(Node $node, Scope $scope): array
{
$propertyFetch = $node->getPropertyFetch();
if (!$propertyFetch instanceof Node\Expr\PropertyFetch) {
return [];
}
$inCloneWith = (bool) $propertyFetch->getAttribute('inCloneWith', false);
$errors = [];
$reflections = $this->propertyReflectionFinder->findPropertyReflectionsFromNode($propertyFetch, $scope);
foreach ($reflections as $propertyReflection) {
$nativeReflection = $propertyReflection->getNativeReflection();
if ($nativeReflection === null) {
continue;
}
if (!$scope->canWriteProperty($propertyReflection)) {
continue;
}
if (!$nativeReflection->isReadOnly()) {
continue;
}
$declaringClass = $nativeReflection->getDeclaringClass();
$scopeClassReflection = $scope->isInClass() ? $scope->getClassReflection() : null;
$isOutsideDeclaringClass = $scopeClassReflection === null
|| $scopeClassReflection->getName() !== $declaringClass->getName();
if ($inCloneWith) {
if (
$isOutsideDeclaringClass
&& $declaringClass->isReadOnly()
&& $nativeReflection->isPublic()
&& !$nativeReflection->isPrivateSet()
) {
$errors[] = RuleErrorBuilder::message(sprintf('Readonly property %s::$%s is assigned outside of its declaring class.', $declaringClass->getDisplayName(), $propertyReflection->getName()))
->line($propertyFetch->name->getStartLine())
->identifier('property.readOnlyAssignOutOfClass')
->build();
}
continue;
}
if ($isOutsideDeclaringClass) {
$errors[] = RuleErrorBuilder::message(sprintf('Readonly property %s::$%s is assigned outside of its declaring class.', $declaringClass->getDisplayName(), $propertyReflection->getName()))
->line($propertyFetch->name->getStartLine())
->identifier('property.readOnlyAssignOutOfClass')
->build();
continue;
}
$scopeMethod = $scope->getFunction();
if (!$scopeMethod instanceof MethodReflection) {
throw new ShouldNotHappenException();
}
if (
in_array($scopeMethod->getName(), $this->constructorsHelper->getConstructors($scopeClassReflection), true)
|| strtolower($scopeMethod->getName()) === '__unserialize'
) {
if (TypeUtils::findThisType($scope->getType($propertyFetch->var)) === null) {
$errors[] = RuleErrorBuilder::message(sprintf('Readonly property %s::$%s is not assigned on $this.', $declaringClass->getDisplayName(), $propertyReflection->getName()))
->line($propertyFetch->name->getStartLine())
->identifier('property.readOnlyAssignNotOnThis')
->build();
}
continue;
}
$assignedExpr = $node->getAssignedExpr();
if (
($assignedExpr instanceof SetOffsetValueTypeExpr || $assignedExpr instanceof UnsetOffsetExpr)
&& (new ObjectType(ArrayAccess::class))->isSuperTypeOf($scope->getType($assignedExpr->getVar()))->yes()
) {
continue;
}
$errors[] = RuleErrorBuilder::message(sprintf('Readonly property %s::$%s is assigned outside of the constructor.', $declaringClass->getDisplayName(), $propertyReflection->getName()))
->line($propertyFetch->name->getStartLine())
->identifier('property.readOnlyAssignNotInConstructor')
->build();
}
return $errors;
}
}