From 279126145e995c4d0b39a60bd6fc5fff5799199e Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 24 May 2023 11:38:51 +0200 Subject: [PATCH] Refactor PARENT_NODE away from UnsetCastRector (#3945) --- rules/Php72/Rector/Unset_/UnsetCastRector.php | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/rules/Php72/Rector/Unset_/UnsetCastRector.php b/rules/Php72/Rector/Unset_/UnsetCastRector.php index 842848d461a..e72deb3af2a 100644 --- a/rules/Php72/Rector/Unset_/UnsetCastRector.php +++ b/rules/Php72/Rector/Unset_/UnsetCastRector.php @@ -7,10 +7,10 @@ use PhpParser\Node; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\Cast\Unset_; +use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Stmt\Expression; use Rector\Core\Rector\AbstractRector; use Rector\Core\ValueObject\PhpVersionFeature; -use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\VersionBonding\Contract\MinPhpVersionInterface; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -49,34 +49,41 @@ public function getRuleDefinition(): RuleDefinition */ public function getNodeTypes(): array { - return [Unset_::class, Assign::class]; + return [Unset_::class, Assign::class, Expression::class]; } /** - * @param Unset_|Assign $node + * @param Unset_|Assign|Expression $node */ public function refactor(Node $node): ?Node { if ($node instanceof Assign) { - if ($node->expr instanceof Unset_) { - $unset = $node->expr; + return $this->refactorAssign($node); + } - if ($this->nodeComparator->areNodesEqual($node->var, $unset->expr)) { - return $this->nodeFactory->createFuncCall('unset', [$node->var]); - } + if ($node instanceof Expression) { + if (! $node->expr instanceof Unset_) { + return null; } + $this->removeNode($node); return null; } - $parentNode = $node->getAttribute(AttributeKey::PARENT_NODE); + return $this->nodeFactory->createNull(); + } - if ($parentNode instanceof Expression) { - $this->removeNode($node); + private function refactorAssign(Assign $assign): ?FuncCall + { + if (! $assign->expr instanceof Unset_) { + return null; + } + $unset = $assign->expr; + if (! $this->nodeComparator->areNodesEqual($assign->var, $unset->expr)) { return null; } - return $this->nodeFactory->createNull(); + return $this->nodeFactory->createFuncCall('unset', [$assign->var]); } }