2019-10-13 05:59:52 +00:00
< ? php
2021-05-09 20:15:43 +00:00
declare ( strict_types = 1 );
2022-06-06 17:12:56 +00:00
namespace Rector\CodeQuality\Rector\BooleanAnd ;
2018-10-23 20:13:13 +00:00
2022-06-06 17:12:56 +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_ ;
use PhpParser\Node\Expr\FuncCall ;
2024-01-02 02:40:38 +00:00
use Rector\NodeManipulator\BinaryOpManipulator ;
2022-06-06 17:12:56 +00:00
use Rector\Php71\ValueObject\TwoNodeMatch ;
2024-01-02 02:40:38 +00:00
use Rector\Rector\AbstractRector ;
2022-06-07 09:18:30 +00:00
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition ;
2018-11-09 22:22:42 +00:00
/**
2022-05-12 19:17:07 +00:00
* @ changelog https :// 3 v4l . org / EZ2P4
* @ changelog 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
*/
2022-06-07 08:22:29 +00:00
final class SimplifyEmptyArrayCheckRector extends AbstractRector
2018-10-23 20:13:13 +00:00
{
2018-11-06 23:33:10 +00:00
/**
2023-06-11 23:01:39 +00:00
* @ readonly
2024-01-02 02:40:38 +00:00
* @ var \Rector\NodeManipulator\BinaryOpManipulator
2018-11-06 23:33:10 +00:00
*/
2019-02-27 21:54:39 +00:00
private $binaryOpManipulator ;
2022-06-07 08:22:29 +00:00
public function __construct ( 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
}
2022-06-07 08:22:29 +00:00
public function getRuleDefinition () : RuleDefinition
2018-10-23 20:13:13 +00:00
{
2022-06-07 08:22:29 +00:00
return new RuleDefinition ( 'Simplify `is_array` and `empty` functions combination into a simple identical check for an empty array' , [ new 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
{
2022-06-07 08:22:29 +00:00
return [ BooleanAnd :: class ];
2018-10-23 20:13:13 +00:00
}
/**
2021-12-10 10:22:23 +00:00
* @ param BooleanAnd $node
2018-10-23 20:13:13 +00:00
*/
2022-06-07 08:22:29 +00:00
public function refactor ( Node $node ) : ? Node
2018-10-23 20:13:13 +00:00
{
2022-10-17 09:13:38 +00:00
$twoNodeMatch = $this -> resolvetwoNodeMatch ( $node );
if ( ! $twoNodeMatch instanceof TwoNodeMatch ) {
return null ;
}
/** @var FuncCall $isArrayExpr */
$isArrayExpr = $twoNodeMatch -> getFirstExpr ();
2023-05-08 12:54:32 +00:00
$firstArgValue = $isArrayExpr -> getArgs ()[ 0 ] -> value ;
2022-10-17 09:13:38 +00:00
/** @var Empty_ $emptyOrNotIdenticalNode */
$emptyOrNotIdenticalNode = $twoNodeMatch -> getSecondExpr ();
2023-05-08 12:54:32 +00:00
if ( $emptyOrNotIdenticalNode -> expr instanceof FuncCall && $this -> nodeComparator -> areNodesEqual ( $emptyOrNotIdenticalNode -> expr -> getArgs ()[ 0 ] -> value , $firstArgValue )) {
2022-10-17 09:13:38 +00:00
return new Identical ( $emptyOrNotIdenticalNode -> expr , new Array_ ());
}
if ( ! $this -> nodeComparator -> areNodesEqual ( $emptyOrNotIdenticalNode -> expr , $firstArgValue )) {
return null ;
}
return new Identical ( $emptyOrNotIdenticalNode -> expr , new Array_ ());
}
private function resolvetwoNodeMatch ( BooleanAnd $booleanAnd ) : ? TwoNodeMatch
{
return $this -> binaryOpManipulator -> matchFirstAndSecondConditionNode (
$booleanAnd ,
2018-11-06 23:33:10 +00:00
// is_array(...)
2022-06-07 08:22:29 +00:00
function ( Node $node ) : bool {
if ( ! $node instanceof FuncCall ) {
2021-05-09 20:15:43 +00:00
return \false ;
2021-03-19 23:11:36 +00:00
}
2022-10-17 09:13:38 +00:00
if ( $node -> isFirstClassCallable ()) {
return \false ;
}
if ( ! $this -> isName ( $node , 'is_array' )) {
return \false ;
}
2023-05-08 07:49:33 +00:00
return isset ( $node -> getArgs ()[ 0 ]);
2018-11-06 23:33:10 +00:00
},
2022-10-17 09:13:38 +00:00
// empty(...)
function ( Node $node ) : bool {
if ( ! $node instanceof Empty_ ) {
return \false ;
}
if ( $node -> expr instanceof FuncCall ) {
if ( $node -> expr -> isFirstClassCallable ()) {
return \false ;
}
if ( ! $this -> isName ( $node -> expr , 'array_filter' )) {
return \false ;
}
2023-05-08 07:49:33 +00:00
return isset ( $node -> expr -> getArgs ()[ 0 ]);
2022-10-17 09:13:38 +00:00
}
return \true ;
}
2018-11-06 23:33:10 +00:00
);
2018-10-23 20:13:13 +00:00
}
}