Skip to content

Commit

Permalink
[DeadCode] Fix RemoveJustPropertyFetchRector to skip concat assigns
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Nov 28, 2022
1 parent 67ec259 commit 2a37774
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\DeadCode\Rector\StmtsAwareInterface\RemoveJustPropertyFetchRector\Fixture;

final class SkipConcatAssign
{
public function run()
{
$assignedObject = json_decode('some...');

$error = $assignedObject->message;
$error .= "\n";

return $error;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\AssignOp\Concat;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\ClosureUse;
use PhpParser\Node\Expr\FuncCall;
Expand Down Expand Up @@ -124,6 +125,10 @@ public function refactor(Node $node): ?Node
// filter out variable usages that are part of nested property fetch, or change variable
$variableUsages = $this->filterOutReferencedVariableUsages($variableUsages);

if ($this->isOverridenWithConcatAssign($variableUsages)) {
return null;
}

if (! $variableToPropertyAssign instanceof PropertyFetchToVariableAssign) {
return null;
}
Expand Down Expand Up @@ -197,6 +202,7 @@ private function filterOutReferencedVariableUsages(array $variableUsages): array
{
return array_filter($variableUsages, function (Variable $variable): bool {
$variableUsageParent = $variable->getAttribute(AttributeKey::PARENT_NODE);

if ($variableUsageParent instanceof Arg) {
$variableUsageParent = $variableUsageParent->getAttribute(AttributeKey::PARENT_NODE);
}
Expand Down Expand Up @@ -264,4 +270,23 @@ private function isPropertyFetchCallerNode(PropertyFetch $propertyFetch): bool
return $nodeObjectType->isSuperTypeOf($propertyFetchCallerType)
->yes();
}

/**
* @param Variable[] $variables
*/
private function isOverridenWithConcatAssign(array $variables): bool
{
foreach ($variables as $variableUsage) {
$parentNode = $variableUsage->getAttribute(AttributeKey::PARENT_NODE);
if (! $parentNode instanceof Concat) {
continue;
}

if ($parentNode->var === $variableUsage) {
return true;
}
}

return false;
}
}

0 comments on commit 2a37774

Please sign in to comment.