Skip to content

Commit

Permalink
Skip coalsce assign in RemoveUnusedPrivatePropertyRector (#4491)
Browse files Browse the repository at this point in the history
Co-authored-by: Benedikt Franke <benedikt@franke.tech>
  • Loading branch information
TomasVotruba and spawnia authored Jul 12, 2023
1 parent 84afeaf commit c0983f1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\AssignOp;
use PhpParser\Node\Expr\AssignOp\Coalesce;
use PhpParser\NodeVisitorAbstract;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\Scope\Contract\NodeVisitor\ScopeResolverNodeVisitorInterface;
Expand All @@ -22,6 +23,10 @@ public function enterNode(Node $node): ?Node
return null;
}

if ($node instanceof Coalesce) {
$node->var->setAttribute(AttributeKey::IS_ASSIGNED_TO, true);
}

$node->var->setAttribute(AttributeKey::IS_BEING_ASSIGNED, true);
$node->expr->setAttribute(AttributeKey::IS_ASSIGNED_TO, true);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector\Fixture;

class Foo {
private int $bar;

public function bar(): int {
return $this->bar ??= 3;
}
}
4 changes: 4 additions & 0 deletions rules/DeadCode/NodeAnalyzer/PropertyWriteonlyAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public function arePropertyFetchesExclusivelyBeingAssignedTo(array $propertyFetc
return false;
}

if ((bool) $propertyFetch->getAttribute(AttributeKey::IS_ASSIGNED_TO, false)) {
return false;
}

if ((bool) $propertyFetch->getAttribute(AttributeKey::IS_BEING_ASSIGNED, false)) {
continue;
}
Expand Down

0 comments on commit c0983f1

Please sign in to comment.