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\Foreach_ ;
2018-10-16 06:38:03 +00:00
2022-06-06 17:12:56 +00:00
use PhpParser\Node ;
use PhpParser\Node\Expr\ArrayDimFetch ;
use PhpParser\Node\Expr\Assign ;
use PhpParser\Node\Expr\BinaryOp\Coalesce ;
use PhpParser\Node\Expr\BinaryOp\Identical ;
use PhpParser\Node\Stmt\Expression ;
use PhpParser\Node\Stmt\Foreach_ ;
use PhpParser\Node\Stmt\If_ ;
use PhpParser\Node\Stmt\Return_ ;
use Rector\Core\NodeManipulator\ForeachManipulator ;
use Rector\Core\Rector\AbstractRector ;
use Rector\Core\ValueObject\PhpVersionFeature ;
use Rector\NodeTypeResolver\Node\AttributeKey ;
use Rector\VersionBonding\Contract\MinPhpVersionInterface ;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition ;
2019-09-03 09:11:45 +00:00
/**
2022-05-12 19:17:07 +00:00
* @ changelog https :// 3 v4l . org / bfsdY
2019-12-29 15:38:02 +00:00
*
2021-03-12 22:20:25 +00:00
* @ see \Rector\Tests\CodeQuality\Rector\Foreach_\SimplifyForeachToCoalescingRector\SimplifyForeachToCoalescingRectorTest
2019-09-03 09:11:45 +00:00
*/
2022-06-06 17:12:56 +00:00
final class SimplifyForeachToCoalescingRector extends \Rector\Core\Rector\AbstractRector implements \Rector\VersionBonding\Contract\MinPhpVersionInterface
2018-10-16 06:38:03 +00:00
{
2018-10-21 10:32:51 +00:00
/**
2021-08-23 00:20:32 +00:00
* @ var \PhpParser\Node\Stmt\Return_ | null
2018-10-21 10:32:51 +00:00
*/
2020-07-31 20:20:45 +00:00
private $return ;
2021-05-10 23:39:21 +00:00
/**
2021-12-04 12:47:17 +00:00
* @ readonly
2021-05-10 23:39:21 +00:00
* @ var \Rector\Core\NodeManipulator\ForeachManipulator
*/
private $foreachManipulator ;
2022-06-06 17:12:56 +00:00
public function __construct ( \Rector\Core\NodeManipulator\ForeachManipulator $foreachManipulator )
2018-10-21 10:32:51 +00:00
{
2019-02-27 21:54:39 +00:00
$this -> foreachManipulator = $foreachManipulator ;
2018-10-21 10:32:51 +00:00
}
2022-06-06 17:12:56 +00:00
public function getRuleDefinition () : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2018-10-16 06:38:03 +00:00
{
2022-06-06 17:12:56 +00:00
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition ( 'Changes foreach that returns set value to ??' , [ new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ( <<< 'CODE_SAMPLE'
2018-10-16 06:38:03 +00:00
foreach ( $this -> oldToNewFunctions as $oldFunction => $newFunction ) {
if ( $currentFunction === $oldFunction ) {
return $newFunction ;
}
}
return null ;
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
, <<< 'CODE_SAMPLE'
2018-10-16 06:38:03 +00:00
return $this -> oldToNewFunctions [ $currentFunction ] ? ? null ;
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
)]);
2018-10-16 06:38:03 +00:00
}
/**
2021-02-27 00:06:15 +00:00
* @ return array < class - string < Node >>
2018-10-16 06:38:03 +00:00
*/
2021-05-09 20:15:43 +00:00
public function getNodeTypes () : array
2018-10-16 06:38:03 +00:00
{
2022-06-06 17:12:56 +00:00
return [ \PhpParser\Node\Stmt\Foreach_ :: class ];
2018-10-16 06:38:03 +00:00
}
/**
2021-12-10 10:22:23 +00:00
* @ param Foreach_ $node
2018-10-16 06:38:03 +00:00
*/
2022-06-06 17:12:56 +00:00
public function refactor ( \PhpParser\Node $node ) : ? \PhpParser\Node
2018-10-16 06:38:03 +00:00
{
2020-06-29 21:19:37 +00:00
$this -> return = null ;
2019-02-17 14:12:47 +00:00
if ( $node -> keyVar === null ) {
2018-10-16 06:38:03 +00:00
return null ;
}
2018-10-21 10:32:51 +00:00
/** @var Return_|Assign|null $returnOrAssignNode */
$returnOrAssignNode = $this -> matchReturnOrAssignNode ( $node );
2019-02-27 13:07:25 +00:00
if ( $returnOrAssignNode === null ) {
2018-10-16 06:38:03 +00:00
return null ;
}
// return $newValue;
// we don't return the node value
2021-05-09 20:15:43 +00:00
if ( ! $this -> nodeComparator -> areNodesEqual ( $node -> valueVar , $returnOrAssignNode -> expr )) {
2018-10-16 06:38:03 +00:00
return null ;
}
2022-06-06 17:12:56 +00:00
if ( $returnOrAssignNode instanceof \PhpParser\Node\Stmt\Return_ ) {
2018-10-21 10:32:51 +00:00
return $this -> processForeachNodeWithReturnInside ( $node , $returnOrAssignNode );
2018-10-16 06:38:03 +00:00
}
2018-10-21 10:32:51 +00:00
return $this -> processForeachNodeWithAssignInside ( $node , $returnOrAssignNode );
2018-10-16 06:38:03 +00:00
}
2021-07-21 09:35:57 +00:00
public function provideMinPhpVersion () : int
{
2022-06-06 17:12:56 +00:00
return \Rector\Core\ValueObject\PhpVersionFeature :: NULL_COALESCE ;
2021-07-21 09:35:57 +00:00
}
2018-10-31 15:34:37 +00:00
/**
* @ return Assign | Return_ | null
*/
2022-06-06 17:12:56 +00:00
private function matchReturnOrAssignNode ( \PhpParser\Node\Stmt\Foreach_ $foreach ) : ? \PhpParser\Node
2018-10-31 15:34:37 +00:00
{
2022-06-06 17:12:56 +00:00
return $this -> foreachManipulator -> matchOnlyStmt ( $foreach , function ( \PhpParser\Node $node ) : ? Node {
if ( ! $node instanceof \PhpParser\Node\Stmt\If_ ) {
2018-10-31 15:34:37 +00:00
return null ;
}
2022-06-06 17:12:56 +00:00
if ( ! $node -> cond instanceof \PhpParser\Node\Expr\BinaryOp\Identical ) {
2018-10-31 15:34:37 +00:00
return null ;
}
2021-05-09 20:15:43 +00:00
if ( \count ( $node -> stmts ) !== 1 ) {
2018-10-31 15:34:37 +00:00
return null ;
}
2022-06-06 17:12:56 +00:00
$innerNode = $node -> stmts [ 0 ] instanceof \PhpParser\Node\Stmt\Expression ? $node -> stmts [ 0 ] -> expr : $node -> stmts [ 0 ];
if ( $innerNode instanceof \PhpParser\Node\Expr\Assign || $innerNode instanceof \PhpParser\Node\Stmt\Return_ ) {
2018-10-31 15:34:37 +00:00
return $innerNode ;
}
return null ;
});
}
2021-11-28 17:35:55 +00:00
/**
* @ return \PhpParser\Node\Stmt\Return_ | null
*/
2022-06-06 17:12:56 +00:00
private function processForeachNodeWithReturnInside ( \PhpParser\Node\Stmt\Foreach_ $foreach , \PhpParser\Node\Stmt\Return_ $return )
2018-10-16 06:38:03 +00:00
{
2021-05-09 20:15:43 +00:00
if ( ! $this -> nodeComparator -> areNodesEqual ( $foreach -> valueVar , $return -> expr )) {
2018-10-16 06:38:03 +00:00
return null ;
}
/** @var If_ $ifNode */
2020-06-29 21:19:37 +00:00
$ifNode = $foreach -> stmts [ 0 ];
2018-10-16 06:38:03 +00:00
/** @var Identical $identicalNode */
$identicalNode = $ifNode -> cond ;
2021-02-19 12:01:23 +00:00
if ( $this -> nodeComparator -> areNodesEqual ( $identicalNode -> left , $foreach -> keyVar )) {
2018-10-16 06:38:03 +00:00
$checkedNode = $identicalNode -> right ;
2021-02-19 12:01:23 +00:00
} elseif ( $this -> nodeComparator -> areNodesEqual ( $identicalNode -> right , $foreach -> keyVar )) {
2018-10-16 06:38:03 +00:00
$checkedNode = $identicalNode -> left ;
} else {
return null ;
}
2022-06-06 17:12:56 +00:00
$nextNode = $foreach -> getAttribute ( \Rector\NodeTypeResolver\Node\AttributeKey :: NEXT_NODE );
2018-10-16 06:38:03 +00:00
// is next node Return?
2022-06-06 17:12:56 +00:00
if ( $nextNode instanceof \PhpParser\Node\Stmt\Return_ ) {
2020-10-26 14:03:26 +00:00
$this -> return = $nextNode ;
2020-06-29 21:19:37 +00:00
$this -> removeNode ( $this -> return );
2018-10-16 06:38:03 +00:00
}
2022-06-06 17:12:56 +00:00
$coalesce = new \PhpParser\Node\Expr\BinaryOp\Coalesce ( new \PhpParser\Node\Expr\ArrayDimFetch ( $foreach -> expr , $checkedNode ), $this -> return instanceof \PhpParser\Node\Stmt\Return_ && $this -> return -> expr !== null ? $this -> return -> expr : $checkedNode );
2020-06-29 21:19:37 +00:00
if ( $this -> return !== null ) {
2022-06-06 17:12:56 +00:00
return new \PhpParser\Node\Stmt\Return_ ( $coalesce );
2018-10-16 06:38:03 +00:00
}
return null ;
}
2022-06-06 17:12:56 +00:00
private function processForeachNodeWithAssignInside ( \PhpParser\Node\Stmt\Foreach_ $foreach , \PhpParser\Node\Expr\Assign $assign ) : ? \PhpParser\Node
2018-10-16 06:38:03 +00:00
{
/** @var If_ $ifNode */
2020-06-29 21:19:37 +00:00
$ifNode = $foreach -> stmts [ 0 ];
2018-10-16 06:38:03 +00:00
/** @var Identical $identicalNode */
$identicalNode = $ifNode -> cond ;
2021-02-19 12:01:23 +00:00
if ( $this -> nodeComparator -> areNodesEqual ( $identicalNode -> left , $foreach -> keyVar )) {
2019-02-22 17:25:31 +00:00
$checkedNode = $assign -> var ;
2018-12-15 20:28:41 +00:00
$keyNode = $identicalNode -> right ;
2021-02-19 12:01:23 +00:00
} elseif ( $this -> nodeComparator -> areNodesEqual ( $identicalNode -> right , $foreach -> keyVar )) {
2019-02-22 17:25:31 +00:00
$checkedNode = $assign -> var ;
2018-12-15 20:28:41 +00:00
$keyNode = $identicalNode -> left ;
2018-10-16 06:38:03 +00:00
} else {
return null ;
}
2022-06-06 17:12:56 +00:00
$arrayDimFetch = new \PhpParser\Node\Expr\ArrayDimFetch ( $foreach -> expr , $keyNode );
return new \PhpParser\Node\Expr\Assign ( $checkedNode , new \PhpParser\Node\Expr\BinaryOp\Coalesce ( $arrayDimFetch , $checkedNode ));
2018-10-16 06:38:03 +00:00
}
}