2023-04-13 10:40:34 +00:00
< ? php
declare ( strict_types = 1 );
2023-08-05 23:50:25 +00:00
namespace Rector\Instanceof_\Rector\Ternary ;
2023-04-13 10:40:34 +00:00
use PhpParser\Node ;
use PhpParser\Node\Expr ;
use PhpParser\Node\Expr\BooleanNot ;
use PhpParser\Node\Expr\Instanceof_ ;
use PhpParser\Node\Expr\Ternary ;
2024-01-02 02:40:38 +00:00
use Rector\Rector\AbstractRector ;
2023-04-13 10:40:34 +00:00
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition ;
/**
2023-08-05 23:50:25 +00:00
* @ see \Rector\Tests\Instanceof_\Rector\Ternary\FlipNegatedTernaryInstanceofRector\FlipNegatedTernaryInstanceofRectorTest
2023-04-13 10:40:34 +00:00
*/
final class FlipNegatedTernaryInstanceofRector extends AbstractRector
{
public function getRuleDefinition () : RuleDefinition
{
return new RuleDefinition ( 'Flip negated ternary of instanceof to direct use of object' , [ new CodeSample ( 'echo ! $object instanceof Product ? null : $object->getPrice();' , 'echo $object instanceof Product ? $object->getPrice() : null;' )]);
}
/**
* @ return array < class - string < Node >>
*/
public function getNodeTypes () : array
{
return [ Ternary :: class ];
}
/**
* @ param Ternary $node
*/
public function refactor ( Node $node ) : ? Node
{
if ( ! $node -> if instanceof Expr ) {
return null ;
}
if ( ! $node -> cond instanceof BooleanNot ) {
return null ;
}
$booleanNot = $node -> cond ;
if ( ! $booleanNot -> expr instanceof Instanceof_ ) {
return null ;
}
$node -> cond = $booleanNot -> expr ;
// flip if and else
[ $node -> if , $node -> else ] = [ $node -> else , $node -> if ];
return $node ;
}
}