Skip to content

Commit

Permalink
WritingToReadOnlyPropertiesRule - hook on better node
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Apr 6, 2023
1 parent 112540b commit bddf573
Showing 1 changed file with 6 additions and 21 deletions.
27 changes: 6 additions & 21 deletions src/Rules/Properties/WritingToReadOnlyPropertiesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\PropertyAssignNode;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
use function sprintf;

/**
* @implements Rule<Node\Expr>
* @implements Rule<PropertyAssignNode>
*/
class WritingToReadOnlyPropertiesRule implements Rule
{
Expand All @@ -26,36 +27,20 @@ public function __construct(

public function getNodeType(): string
{
return Node\Expr::class;
return PropertyAssignNode::class;
}

public function processNode(Node $node, Scope $scope): array
{
$propertyFetch = $node->getPropertyFetch();
if (
!$node instanceof Node\Expr\Assign
&& !$node instanceof Node\Expr\AssignOp
&& !$node instanceof Node\Expr\AssignRef
) {
return [];
}

if (
!($node->var instanceof Node\Expr\PropertyFetch)
&& !($node->var instanceof Node\Expr\StaticPropertyFetch)
) {
return [];
}

if (
$node->var instanceof Node\Expr\PropertyFetch
$propertyFetch instanceof Node\Expr\PropertyFetch
&& $this->checkThisOnly
&& !$this->ruleLevelHelper->isThis($node->var->var)
&& !$this->ruleLevelHelper->isThis($propertyFetch->var)
) {
return [];
}

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

0 comments on commit bddf573

Please sign in to comment.