2019-10-13 05:59:52 +00:00
< ? php
2021-05-09 20:15:43 +00:00
declare ( strict_types = 1 );
2018-11-09 22:50:00 +00:00
namespace Rector\CodeQuality\Rector\BooleanAnd ;
2018-10-23 20:13:13 +00:00
use PhpParser\Node ;
use PhpParser\Node\Expr\Array_ ;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd ;
use PhpParser\Node\Expr\BinaryOp\Identical ;
use PhpParser\Node\Expr\Empty_ ;
2021-03-19 23:11:36 +00:00
use PhpParser\Node\Expr\FuncCall ;
2021-02-08 12:33:17 +00:00
use Rector\Core\NodeManipulator\BinaryOpManipulator ;
2020-02-06 21:48:18 +00:00
use Rector\Core\Rector\AbstractRector ;
2021-01-20 11:41:35 +00:00
use Rector\Php71\ValueObject\TwoNodeMatch ;
2020-11-16 17:50:38 +00:00
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition ;
2018-11-09 22:22:42 +00:00
/**
* @ see https :// 3 v4l . org / EZ2P4
* @ see https :// 3 v4l . org / egtb5
2021-03-12 22:20:25 +00:00
* @ see \Rector\Tests\CodeQuality\Rector\BooleanAnd\SimplifyEmptyArrayCheckRector\SimplifyEmptyArrayCheckRectorTest
2018-11-09 22:22:42 +00:00
*/
2021-05-10 22:23:08 +00:00
final class SimplifyEmptyArrayCheckRector extends \Rector\Core\Rector\AbstractRector
2018-10-23 20:13:13 +00:00
{
2018-11-06 23:33:10 +00:00
/**
2021-12-04 12:47:17 +00:00
* @ readonly
2021-05-10 23:39:21 +00:00
* @ var \Rector\Core\NodeManipulator\BinaryOpManipulator
2018-11-06 23:33:10 +00:00
*/
2019-02-27 21:54:39 +00:00
private $binaryOpManipulator ;
2021-05-10 22:23:08 +00:00
public function __construct ( \Rector\Core\NodeManipulator\BinaryOpManipulator $binaryOpManipulator )
2018-11-06 23:33:10 +00:00
{
2019-02-27 21:54:39 +00:00
$this -> binaryOpManipulator = $binaryOpManipulator ;
2018-11-06 23:33:10 +00:00
}
2021-05-10 22:23:08 +00:00
public function getRuleDefinition () : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2018-10-23 20:13:13 +00:00
{
2021-05-10 22:23:08 +00:00
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition ( 'Simplify `is_array` and `empty` functions combination into a simple identical check for an empty array' , [ new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ( 'is_array($values) && empty($values)' , '$values === []' )]);
2018-10-23 20:13:13 +00:00
}
/**
2021-02-27 00:06:15 +00:00
* @ return array < class - string < Node >>
2018-10-23 20:13:13 +00:00
*/
2021-05-09 20:15:43 +00:00
public function getNodeTypes () : array
2018-10-23 20:13:13 +00:00
{
2021-05-10 22:23:08 +00:00
return [ \PhpParser\Node\Expr\BinaryOp\BooleanAnd :: class ];
2018-10-23 20:13:13 +00:00
}
/**
2021-12-10 09:57:54 +00:00
* @ param \PhpParser\Node $node
2018-10-23 20:13:13 +00:00
*/
2021-12-10 09:57:54 +00:00
public function refactor ( $node ) : ? \PhpParser\Node
2018-10-23 20:13:13 +00:00
{
2020-08-23 09:39:09 +00:00
$twoNodeMatch = $this -> binaryOpManipulator -> matchFirstAndSecondConditionNode (
2018-11-06 23:33:10 +00:00
$node ,
// is_array(...)
2021-05-10 22:23:08 +00:00
function ( \PhpParser\Node $node ) : bool {
if ( ! $node instanceof \PhpParser\Node\Expr\FuncCall ) {
2021-05-09 20:15:43 +00:00
return \false ;
2021-03-19 23:11:36 +00:00
}
return $this -> isName ( $node , 'is_array' );
2018-11-06 23:33:10 +00:00
},
2021-05-10 22:23:08 +00:00
\PhpParser\Node\Expr\Empty_ :: class
2018-11-06 23:33:10 +00:00
);
2021-05-10 22:23:08 +00:00
if ( ! $twoNodeMatch instanceof \Rector\Php71\ValueObject\TwoNodeMatch ) {
2018-10-23 20:13:13 +00:00
return null ;
}
2018-11-09 22:22:42 +00:00
/** @var Empty_ $emptyOrNotIdenticalNode */
2020-08-23 09:39:09 +00:00
$emptyOrNotIdenticalNode = $twoNodeMatch -> getSecondExpr ();
2021-05-10 22:23:08 +00:00
return new \PhpParser\Node\Expr\BinaryOp\Identical ( $emptyOrNotIdenticalNode -> expr , new \PhpParser\Node\Expr\Array_ ());
2018-10-23 20:13:13 +00:00
}
}