2020-06-18 11:49:37 +02:00
< ? php
2021-05-09 20:15:43 +00:00
declare ( strict_types = 1 );
2020-06-18 11:49:37 +02:00
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
{
2021-05-10 22:23:08 +00:00
public function ensureNodeCanBeRemove ( \PhpParser\Node $node ) : void
2020-06-18 11:49:37 +02:00
{
if ( $this -> isLegalNodeRemoval ( $node )) {
return ;
}
2020-12-09 23:25:53 +01:00
// validate the node can be removed
2021-05-10 22:23:08 +00:00
$parentNode = $node -> getAttribute ( \Rector\NodeTypeResolver\Node\AttributeKey :: PARENT_NODE );
if ( ! $parentNode instanceof \PhpParser\Node ) {
throw new \Rector\Core\Exception\ShouldNotHappenException ();
2020-12-09 23:25:53 +01:00
}
2021-05-10 22:23:08 +00:00
throw new \Rector\Core\Exception\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 )));
2020-06-18 11:49:37 +02:00
}
2021-05-10 22:23:08 +00:00
public function isLegalNodeRemoval ( \PhpParser\Node $node ) : bool
2020-06-18 11:49:37 +02:00
{
2021-05-10 22:23:08 +00:00
$parent = $node -> getAttribute ( \Rector\NodeTypeResolver\Node\AttributeKey :: PARENT_NODE );
if ( $parent instanceof \PhpParser\Node\Stmt\If_ && $parent -> cond === $node ) {
2021-05-09 20:15:43 +00:00
return \false ;
2020-10-19 16:38:02 +02:00
}
2021-05-10 22:23:08 +00:00
if ( $parent instanceof \PhpParser\Node\Expr\BooleanNot ) {
$parent = $parent -> getAttribute ( \Rector\NodeTypeResolver\Node\AttributeKey :: PARENT_NODE );
2020-06-18 11:49:37 +02:00
}
2021-05-10 22:23:08 +00:00
if ( $parent instanceof \PhpParser\Node\Expr\Assign ) {
2021-05-09 20:15:43 +00:00
return \false ;
2020-12-19 22:24:53 +07:00
}
if ( $this -> isIfCondition ( $node )) {
2021-05-09 20:15:43 +00:00
return \false ;
2020-12-19 22:24:53 +07:00
}
2021-05-09 20:15:43 +00:00
return ! $this -> isWhileCondition ( $node );
2020-06-18 11:49:37 +02:00
}
2021-05-10 22:23:08 +00:00
private function isIfCondition ( \PhpParser\Node $node ) : bool
2020-06-18 11:49:37 +02:00
{
2021-05-10 22:23:08 +00:00
$parentNode = $node -> getAttribute ( \Rector\NodeTypeResolver\Node\AttributeKey :: PARENT_NODE );
if ( ! $parentNode instanceof \PhpParser\Node\Stmt\If_ ) {
2021-05-09 20:15:43 +00:00
return \false ;
2020-06-18 11:49:37 +02:00
}
return $parentNode -> cond === $node ;
}
2021-05-10 22:23:08 +00:00
private function isWhileCondition ( \PhpParser\Node $node ) : bool
2020-06-18 11:49:37 +02:00
{
2021-05-10 22:23:08 +00:00
$parentNode = $node -> getAttribute ( \Rector\NodeTypeResolver\Node\AttributeKey :: PARENT_NODE );
if ( ! $parentNode instanceof \PhpParser\Node\Stmt\While_ ) {
2021-05-09 20:15:43 +00:00
return \false ;
2020-06-18 11:49:37 +02:00
}
return $parentNode -> cond === $node ;
}
}