Skip to content

Commit

Permalink
Refactor PARENT_NODE away from UnsetCastRector (#3945)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 24, 2023
1 parent c504b2f commit 2791261
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions rules/Php72/Rector/Unset_/UnsetCastRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
}
}

0 comments on commit 2791261

Please sign in to comment.