rector/packages/NodeRemoval/BreakingRemovalGuard.php
Tomas Votruba 46dd47dd4e Updated Rector to commit ac94ddd5bd269b17afc339f39af711c02d56b34a
ac94ddd5bd [PHPStan] Reduce PHPStan errors for narrow public - take 1 (#2672)
2022-07-16 14:24:54 +00:00

64 lines
2.0 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\NodeRemoval;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\While_;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\NodeTypeResolver\Node\AttributeKey;
final class BreakingRemovalGuard
{
public function ensureNodeCanBeRemove(Node $node) : void
{
if ($this->isLegalNodeRemoval($node)) {
return;
}
// validate the node can be removed
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
if (!$parentNode instanceof Node) {
throw new ShouldNotHappenException();
}
throw new ShouldNotHappenException(\sprintf('Node "%s" on line %d is child of "%s", so it cannot be removed as it would break PHP code. Change or remove the parent node instead.', \get_class($node), $node->getLine(), \get_class($parentNode)));
}
/**
* @api
*/
public function isLegalNodeRemoval(Node $node) : bool
{
$parent = $node->getAttribute(AttributeKey::PARENT_NODE);
if ($parent instanceof If_ && $parent->cond === $node) {
return \false;
}
if ($parent instanceof BooleanNot) {
$parent = $parent->getAttribute(AttributeKey::PARENT_NODE);
}
if ($parent instanceof Assign) {
return \false;
}
if ($this->isIfCondition($node)) {
return \false;
}
return !$this->isWhileCondition($node);
}
private function isIfCondition(Node $node) : bool
{
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
if (!$parentNode instanceof If_) {
return \false;
}
return $parentNode->cond === $node;
}
private function isWhileCondition(Node $node) : bool
{
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
if (!$parentNode instanceof While_) {
return \false;
}
return $parentNode->cond === $node;
}
}