2019-10-13 05:59:52 +00:00
< ? php
2021-05-09 20:15:43 +00:00
declare ( strict_types = 1 );
2018-10-12 20:19:34 +00:00
namespace Rector\CodeQuality\Rector\Foreach_ ;
use PhpParser\Node ;
2019-01-14 18:21:23 +00:00
use PhpParser\Node\Expr ;
2018-10-12 20:19:34 +00:00
use PhpParser\Node\Expr\BinaryOp\Equal ;
use PhpParser\Node\Expr\BinaryOp\Identical ;
use PhpParser\Node\Expr\BooleanNot ;
use PhpParser\Node\Expr\FuncCall ;
use PhpParser\Node\Expr\Variable ;
use PhpParser\Node\Stmt\Foreach_ ;
use PhpParser\Node\Stmt\If_ ;
use PhpParser\Node\Stmt\Return_ ;
2019-06-04 11:58:55 +00:00
use PHPStan\Type\ObjectType ;
2020-12-05 11:41:55 +00:00
use Rector\BetterPhpDocParser\Comment\CommentsMerger ;
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 ;
2019-04-13 09:20:27 +00:00
use Rector\NodeTypeResolver\Node\AttributeKey ;
2020-08-23 09:39:09 +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 ;
2019-09-03 09:11:45 +00:00
/**
2021-03-12 22:20:25 +00:00
* @ see \Rector\Tests\CodeQuality\Rector\Foreach_\ForeachToInArrayRector\ForeachToInArrayRectorTest
2019-09-03 09:11:45 +00:00
*/
2021-05-10 22:23:08 +00:00
final class ForeachToInArrayRector extends \Rector\Core\Rector\AbstractRector
2018-10-12 20:19:34 +00:00
{
2018-11-05 08:08:27 +00:00
/**
2021-05-10 23:39:21 +00:00
* @ var \Rector\Core\NodeManipulator\BinaryOpManipulator
2018-11-05 08:08:27 +00:00
*/
2020-05-04 22:23:41 +00:00
private $binaryOpManipulator ;
2018-11-07 01:28:52 +00:00
/**
2021-05-10 23:39:21 +00:00
* @ var \Rector\BetterPhpDocParser\Comment\CommentsMerger
2018-11-07 01:28:52 +00:00
*/
2020-12-05 11:41:55 +00:00
private $commentsMerger ;
2021-05-10 22:23:08 +00:00
public function __construct ( \Rector\Core\NodeManipulator\BinaryOpManipulator $binaryOpManipulator , \Rector\BetterPhpDocParser\Comment\CommentsMerger $commentsMerger )
2018-11-05 08:08:27 +00:00
{
2019-02-27 21:54:39 +00:00
$this -> binaryOpManipulator = $binaryOpManipulator ;
2020-12-05 11:41:55 +00:00
$this -> commentsMerger = $commentsMerger ;
2018-11-05 08:08:27 +00:00
}
2021-05-10 22:23:08 +00:00
public function getRuleDefinition () : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2018-10-12 20:19:34 +00:00
{
2021-05-10 22:23:08 +00:00
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition ( 'Simplify `foreach` loops into `in_array` when possible' , [ new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ( <<< 'CODE_SAMPLE'
2018-10-12 20:19:34 +00:00
foreach ( $items as $item ) {
2020-09-08 13:27:17 +00:00
if ( $item === 'something' ) {
2018-10-12 20:19:34 +00:00
return true ;
}
}
return false ;
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
, <<< 'CODE_SAMPLE'
2021-02-18 21:35:10 +00:00
return in_array ( 'something' , $items , true );
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
)]);
2018-10-12 20:19:34 +00:00
}
/**
2021-02-27 00:06:15 +00:00
* @ return array < class - string < Node >>
2018-10-12 20:19:34 +00:00
*/
2021-05-09 20:15:43 +00:00
public function getNodeTypes () : array
2018-10-12 20:19:34 +00:00
{
2021-05-10 22:23:08 +00:00
return [ \PhpParser\Node\Stmt\Foreach_ :: class ];
2018-10-12 20:19:34 +00:00
}
/**
2018-11-05 08:08:27 +00:00
* @ param Foreach_ $node
2018-10-12 20:19:34 +00:00
*/
2021-05-10 22:23:08 +00:00
public function refactor ( \PhpParser\Node $node ) : ? \PhpParser\Node
2018-10-12 20:19:34 +00:00
{
2018-11-07 01:37:39 +00:00
if ( $this -> shouldSkipForeach ( $node )) {
2018-10-12 20:19:34 +00:00
return null ;
}
2018-11-07 01:28:52 +00:00
/** @var If_ $firstNodeInsideForeach */
2018-11-05 08:08:27 +00:00
$firstNodeInsideForeach = $node -> stmts [ 0 ];
2018-11-07 01:28:52 +00:00
if ( $this -> shouldSkipIf ( $firstNodeInsideForeach )) {
2018-10-12 20:19:34 +00:00
return null ;
}
2019-01-14 18:21:23 +00:00
/** @var Identical|Equal $ifCondition */
2018-10-12 20:19:34 +00:00
$ifCondition = $firstNodeInsideForeach -> cond ;
2019-02-22 17:25:31 +00:00
$foreachValueVar = $node -> valueVar ;
2020-08-23 09:39:09 +00:00
$twoNodeMatch = $this -> matchNodes ( $ifCondition , $foreachValueVar );
2021-05-10 22:23:08 +00:00
if ( ! $twoNodeMatch instanceof \Rector\Php71\ValueObject\TwoNodeMatch ) {
2018-10-12 20:19:34 +00:00
return null ;
}
2020-08-23 09:39:09 +00:00
$comparedNode = $twoNodeMatch -> getSecondExpr ();
2021-05-09 20:15:43 +00:00
if ( ! $this -> isIfBodyABoolReturnNode ( $firstNodeInsideForeach )) {
2018-10-12 20:19:34 +00:00
return null ;
}
2020-07-19 18:52:42 +00:00
$funcCall = $this -> createInArrayFunction ( $comparedNode , $ifCondition , $node );
2019-02-22 17:25:31 +00:00
/** @var Return_ $returnToRemove */
2021-05-10 22:23:08 +00:00
$returnToRemove = $node -> getAttribute ( \Rector\NodeTypeResolver\Node\AttributeKey :: NEXT_NODE );
2019-02-22 17:25:31 +00:00
/** @var Return_ $return */
$return = $firstNodeInsideForeach -> stmts [ 0 ];
if ( $returnToRemove -> expr === null ) {
2019-01-14 18:21:23 +00:00
return null ;
}
2021-05-09 20:15:43 +00:00
if ( ! $this -> valueResolver -> isTrueOrFalse ( $returnToRemove -> expr )) {
2018-11-07 01:28:52 +00:00
return null ;
}
2021-03-07 21:07:50 +00:00
$returnedExpr = $return -> expr ;
2021-05-10 22:23:08 +00:00
if ( ! $returnedExpr instanceof \PhpParser\Node\Expr ) {
2019-01-14 18:21:23 +00:00
return null ;
}
2018-11-07 01:37:39 +00:00
// cannot be "return true;" + "return true;"
2021-02-19 12:01:23 +00:00
if ( $this -> nodeComparator -> areNodesEqual ( $return , $returnToRemove )) {
2018-11-07 01:28:52 +00:00
return null ;
}
2019-02-22 17:25:31 +00:00
$this -> removeNode ( $returnToRemove );
2021-03-07 21:07:50 +00:00
$return = $this -> createReturn ( $returnedExpr , $funcCall );
2020-12-05 11:41:55 +00:00
$this -> commentsMerger -> keepChildren ( $return , $node );
2019-02-22 17:25:31 +00:00
return $return ;
2018-10-12 20:19:34 +00:00
}
2021-05-10 22:23:08 +00:00
private function shouldSkipForeach ( \PhpParser\Node\Stmt\Foreach_ $foreach ) : bool
2018-10-31 15:34:37 +00:00
{
2020-10-18 18:42:49 +00:00
if ( $foreach -> keyVar !== null ) {
2021-05-09 20:15:43 +00:00
return \true ;
2018-10-31 15:34:37 +00:00
}
2021-05-09 20:15:43 +00:00
if ( \count ( $foreach -> stmts ) > 1 ) {
return \true ;
2018-11-05 08:08:27 +00:00
}
2021-05-10 22:23:08 +00:00
$nextNode = $foreach -> getAttribute ( \Rector\NodeTypeResolver\Node\AttributeKey :: NEXT_NODE );
if ( ! $nextNode instanceof \PhpParser\Node ) {
2021-05-09 20:15:43 +00:00
return \true ;
2020-12-24 16:28:56 +00:00
}
2021-05-10 22:23:08 +00:00
if ( ! $nextNode instanceof \PhpParser\Node\Stmt\Return_ ) {
2021-05-09 20:15:43 +00:00
return \true ;
2018-10-31 15:34:37 +00:00
}
$returnExpression = $nextNode -> expr ;
2021-05-10 22:23:08 +00:00
if ( ! $returnExpression instanceof \PhpParser\Node\Expr ) {
2021-05-09 20:15:43 +00:00
return \true ;
2018-11-07 01:37:39 +00:00
}
2021-05-09 20:15:43 +00:00
if ( ! $this -> valueResolver -> isTrueOrFalse ( $returnExpression )) {
return \true ;
2018-10-12 20:19:34 +00:00
}
2021-10-07 17:46:41 +00:00
$foreachValueStaticType = $this -> getType ( $foreach -> expr );
2021-05-10 22:23:08 +00:00
if ( $foreachValueStaticType instanceof \PHPStan\Type\ObjectType ) {
2021-05-09 20:15:43 +00:00
return \true ;
2019-06-04 12:01:50 +00:00
}
2021-05-10 22:23:08 +00:00
return ! $foreach -> stmts [ 0 ] instanceof \PhpParser\Node\Stmt\If_ ;
2018-10-12 20:19:34 +00:00
}
2021-05-10 22:23:08 +00:00
private function shouldSkipIf ( \PhpParser\Node\Stmt\If_ $if ) : bool
2018-12-16 13:24:31 +00:00
{
2020-06-29 21:19:37 +00:00
$ifCondition = $if -> cond ;
2021-05-10 22:23:08 +00:00
if ( $ifCondition instanceof \PhpParser\Node\Expr\BinaryOp\Identical ) {
2021-05-09 20:15:43 +00:00
return \false ;
2020-12-19 15:24:53 +00:00
}
2021-05-10 22:23:08 +00:00
return ! $ifCondition instanceof \PhpParser\Node\Expr\BinaryOp\Equal ;
2018-12-16 13:24:31 +00:00
}
2021-10-23 21:09:26 +00:00
/**
* @ param \PhpParser\Node\Expr\BinaryOp\Equal | \PhpParser\Node\Expr\BinaryOp\Identical $binaryOp
*/
private function matchNodes ( $binaryOp , \PhpParser\Node\Expr $expr ) : ? \Rector\Php71\ValueObject\TwoNodeMatch
2019-01-22 19:40:27 +00:00
{
2021-05-10 22:23:08 +00:00
return $this -> binaryOpManipulator -> matchFirstAndSecondConditionNode ( $binaryOp , \PhpParser\Node\Expr\Variable :: class , function ( \PhpParser\Node $node , \PhpParser\Node $otherNode ) use ( $expr ) : bool {
2021-05-09 20:15:43 +00:00
return $this -> nodeComparator -> areNodesEqual ( $otherNode , $expr );
});
2019-01-22 19:40:27 +00:00
}
2021-05-10 22:23:08 +00:00
private function isIfBodyABoolReturnNode ( \PhpParser\Node\Stmt\If_ $if ) : bool
2018-10-12 20:19:34 +00:00
{
2020-06-29 21:19:37 +00:00
$ifStatment = $if -> stmts [ 0 ];
2021-05-10 22:23:08 +00:00
if ( ! $ifStatment instanceof \PhpParser\Node\Stmt\Return_ ) {
2021-05-09 20:15:43 +00:00
return \false ;
2018-10-12 20:19:34 +00:00
}
2019-01-14 18:21:23 +00:00
if ( $ifStatment -> expr === null ) {
2021-05-09 20:15:43 +00:00
return \false ;
2019-01-14 18:21:23 +00:00
}
2021-01-30 23:20:05 +00:00
return $this -> valueResolver -> isTrueOrFalse ( $ifStatment -> expr );
2018-10-12 20:19:34 +00:00
}
2018-10-31 15:34:37 +00:00
/**
2021-08-23 00:20:32 +00:00
* @ param \PhpParser\Node\Expr\BinaryOp\Identical | \PhpParser\Node\Expr\BinaryOp\Equal $binaryOp
2018-10-31 15:34:37 +00:00
*/
2021-06-29 14:24:45 +00:00
private function createInArrayFunction ( \PhpParser\Node\Expr $expr , $binaryOp , \PhpParser\Node\Stmt\Foreach_ $foreach ) : \PhpParser\Node\Expr\FuncCall
2018-10-12 20:19:34 +00:00
{
2021-01-30 21:41:25 +00:00
$arguments = $this -> nodeFactory -> createArgs ([ $expr , $foreach -> expr ]);
2021-05-10 22:23:08 +00:00
if ( $binaryOp instanceof \PhpParser\Node\Expr\BinaryOp\Identical ) {
2021-01-30 21:41:25 +00:00
$arguments [] = $this -> nodeFactory -> createArg ( $this -> nodeFactory -> createTrue ());
2018-10-12 20:19:34 +00:00
}
2021-01-30 21:41:25 +00:00
return $this -> nodeFactory -> createFuncCall ( 'in_array' , $arguments );
2018-10-12 20:19:34 +00:00
}
2021-05-10 22:23:08 +00:00
private function createReturn ( \PhpParser\Node\Expr $expr , \PhpParser\Node\Expr\FuncCall $funcCall ) : \PhpParser\Node\Stmt\Return_
2020-12-05 11:41:55 +00:00
{
2021-05-10 22:23:08 +00:00
$expr = $this -> valueResolver -> isFalse ( $expr ) ? new \PhpParser\Node\Expr\BooleanNot ( $funcCall ) : $funcCall ;
return new \PhpParser\Node\Stmt\Return_ ( $expr );
2020-12-05 11:41:55 +00:00
}
2018-10-12 20:19:34 +00:00
}