Skip to content

Commit

Permalink
[DeadCode] Skip append += assignment on RemoveUnusedPrivatePropertyRe…
Browse files Browse the repository at this point in the history
…ctor (#4506)

* [DeadCode] Skip append += assignment on RemoveUnusedPrivatePropertyRector

* skip appender

* add test appender on AddDefaultValueForUndefinedVariableRector to avoid regression

* Fix test
  • Loading branch information
samsonasik committed Jul 13, 2023
1 parent 398baa2 commit 8537bbd
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
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 @@ -19,12 +18,13 @@ final class AssignedToNodeVisitor extends NodeVisitorAbstract implements ScopeRe
{
public function enterNode(Node $node): ?Node
{
if (! $node instanceof Assign && ! $node instanceof AssignOp) {
if ($node instanceof AssignOp) {
$node->var->setAttribute(AttributeKey::IS_ASSIGNED_TO, true);
return null;
}

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

$node->var->setAttribute(AttributeKey::IS_BEING_ASSIGNED, 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 SkipAppendAssignment {
private int $bar = 2;

public function bar(): int {
return $this->bar += 3;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

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

class SkipAppenderAssignment {
private int $bar = 2;

public function bar(): int {
$a = 1;
$a += $this->bar;

return $a;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Rector\Tests\Php56\Rector\FunctionLike\AddDefaultValueForUndefinedVariableRector\Fixture;

class Appender
{
public function run($a = 1)
{
return $a += $b;
}
}

?>
-----
<?php

namespace Rector\Tests\Php56\Rector\FunctionLike\AddDefaultValueForUndefinedVariableRector\Fixture;

class Appender
{
public function run($a = 1)
{
$b = null;
return $a += $b;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Doctrine\Rector\Property\DoctrineTargetEntityStringToClassConstantRector;
use Rector\Doctrine\CodeQuality\Rector\Property\DoctrineTargetEntityStringToClassConstantRector;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector;

return static function (RectorConfig $rectorConfig): void {
Expand Down

0 comments on commit 8537bbd

Please sign in to comment.