Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DeadCode] Skip append += assignment on RemoveUnusedPrivatePropertyRector #4506

Merged
merged 4 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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