4
4
5
5
use PhpParser \Node ;
6
6
use PHPStan \Analyser \Scope ;
7
+ use PHPStan \Node \PropertyAssignNode ;
7
8
use PHPStan \Rules \Rule ;
8
9
use PHPStan \Rules \RuleErrorBuilder ;
9
10
use PHPStan \Rules \RuleLevelHelper ;
10
11
use function sprintf ;
11
12
12
13
/**
13
- * @implements Rule<Node\Expr >
14
+ * @implements Rule<PropertyAssignNode >
14
15
*/
15
16
class WritingToReadOnlyPropertiesRule implements Rule
16
17
{
@@ -26,36 +27,20 @@ public function __construct(
26
27
27
28
public function getNodeType (): string
28
29
{
29
- return Node \Expr ::class;
30
+ return PropertyAssignNode ::class;
30
31
}
31
32
32
33
public function processNode (Node $ node , Scope $ scope ): array
33
34
{
35
+ $ propertyFetch = $ node ->getPropertyFetch ();
34
36
if (
35
- !$ node instanceof Node \Expr \Assign
36
- && !$ node instanceof Node \Expr \AssignOp
37
- && !$ node instanceof Node \Expr \AssignRef
38
- ) {
39
- return [];
40
- }
41
-
42
- if (
43
- !($ node ->var instanceof Node \Expr \PropertyFetch)
44
- && !($ node ->var instanceof Node \Expr \StaticPropertyFetch)
45
- ) {
46
- return [];
47
- }
48
-
49
- if (
50
- $ node ->var instanceof Node \Expr \PropertyFetch
37
+ $ propertyFetch instanceof Node \Expr \PropertyFetch
51
38
&& $ this ->checkThisOnly
52
- && !$ this ->ruleLevelHelper ->isThis ($ node -> var ->var )
39
+ && !$ this ->ruleLevelHelper ->isThis ($ propertyFetch ->var )
53
40
) {
54
41
return [];
55
42
}
56
43
57
- /** @var Node\Expr\PropertyFetch|Node\Expr\StaticPropertyFetch $propertyFetch */
58
- $ propertyFetch = $ node ->var ;
59
44
$ propertyReflection = $ this ->propertyReflectionFinder ->findPropertyReflectionFromNode ($ propertyFetch , $ scope );
60
45
if ($ propertyReflection === null ) {
61
46
return [];
0 commit comments