Skip to content

Commit bddf573

Browse files
committed
WritingToReadOnlyPropertiesRule - hook on better node
1 parent 112540b commit bddf573

File tree

1 file changed

+6
-21
lines changed

1 file changed

+6
-21
lines changed

src/Rules/Properties/WritingToReadOnlyPropertiesRule.php

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
7+
use PHPStan\Node\PropertyAssignNode;
78
use PHPStan\Rules\Rule;
89
use PHPStan\Rules\RuleErrorBuilder;
910
use PHPStan\Rules\RuleLevelHelper;
1011
use function sprintf;
1112

1213
/**
13-
* @implements Rule<Node\Expr>
14+
* @implements Rule<PropertyAssignNode>
1415
*/
1516
class WritingToReadOnlyPropertiesRule implements Rule
1617
{
@@ -26,36 +27,20 @@ public function __construct(
2627

2728
public function getNodeType(): string
2829
{
29-
return Node\Expr::class;
30+
return PropertyAssignNode::class;
3031
}
3132

3233
public function processNode(Node $node, Scope $scope): array
3334
{
35+
$propertyFetch = $node->getPropertyFetch();
3436
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
5138
&& $this->checkThisOnly
52-
&& !$this->ruleLevelHelper->isThis($node->var->var)
39+
&& !$this->ruleLevelHelper->isThis($propertyFetch->var)
5340
) {
5441
return [];
5542
}
5643

57-
/** @var Node\Expr\PropertyFetch|Node\Expr\StaticPropertyFetch $propertyFetch */
58-
$propertyFetch = $node->var;
5944
$propertyReflection = $this->propertyReflectionFinder->findPropertyReflectionFromNode($propertyFetch, $scope);
6045
if ($propertyReflection === null) {
6146
return [];

0 commit comments

Comments
 (0)