From 63ca8727cbf38fdbcc1a68c429ce3dc2340f268e Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 28 Nov 2022 16:54:30 +0100 Subject: [PATCH] [DeadCode] Fix RemoveJustPropertyFetchRector to skip concat assigns (#3123) * [DeadCode] Fix RemoveJustPropertyFetchRector to skip concat assigns * [ci-review] Rector Rectify Co-authored-by: GitHub Action --- .../Fixture/skip_concat_assign.php.inc | 16 ++++++++++++ .../RemoveJustPropertyFetchRector.php | 25 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 rules-tests/DeadCode/Rector/StmtsAwareInterface/RemoveJustPropertyFetchRector/Fixture/skip_concat_assign.php.inc diff --git a/rules-tests/DeadCode/Rector/StmtsAwareInterface/RemoveJustPropertyFetchRector/Fixture/skip_concat_assign.php.inc b/rules-tests/DeadCode/Rector/StmtsAwareInterface/RemoveJustPropertyFetchRector/Fixture/skip_concat_assign.php.inc new file mode 100644 index 00000000000..2a5a6e82e99 --- /dev/null +++ b/rules-tests/DeadCode/Rector/StmtsAwareInterface/RemoveJustPropertyFetchRector/Fixture/skip_concat_assign.php.inc @@ -0,0 +1,16 @@ +message; + $error .= "\n"; + + return $error; + } +} diff --git a/rules/DeadCode/Rector/StmtsAwareInterface/RemoveJustPropertyFetchRector.php b/rules/DeadCode/Rector/StmtsAwareInterface/RemoveJustPropertyFetchRector.php index 00c1c3500a1..30b78715224 100644 --- a/rules/DeadCode/Rector/StmtsAwareInterface/RemoveJustPropertyFetchRector.php +++ b/rules/DeadCode/Rector/StmtsAwareInterface/RemoveJustPropertyFetchRector.php @@ -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; @@ -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; } @@ -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); } @@ -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 $variable) { + $parentNode = $variable->getAttribute(AttributeKey::PARENT_NODE); + if (! $parentNode instanceof Concat) { + continue; + } + + if ($parentNode->var === $variable) { + return true; + } + } + + return false; + } }