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;
|
2023-03-23 23:21:34 +00:00
|
|
|
use PhpParser\Node\Expr;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Expr\ArrayDimFetch;
|
|
|
|
use PhpParser\Node\Expr\Assign;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\Coalesce;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\Identical;
|
2023-05-10 05:18:05 +00:00
|
|
|
use PhpParser\Node\Stmt;
|
2023-12-02 04:51:32 +00:00
|
|
|
use PhpParser\Node\Stmt\Else_;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Stmt\Expression;
|
|
|
|
use PhpParser\Node\Stmt\Foreach_;
|
|
|
|
use PhpParser\Node\Stmt\If_;
|
|
|
|
use PhpParser\Node\Stmt\Return_;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
|
|
|
|
use Rector\Rector\AbstractRector;
|
|
|
|
use Rector\ValueObject\PhpVersionFeature;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
2022-06-07 09:18:30 +00:00
|
|
|
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://3v4l.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-07 08:22:29 +00:00
|
|
|
final class SimplifyForeachToCoalescingRector extends AbstractRector implements MinPhpVersionInterface
|
2018-10-16 06:38:03 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-10-16 06:38:03 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Changes foreach that returns set value to ??', [new CodeSample(<<<'CODE_SAMPLE'
|
2018-10-16 06:38:03 +00:00
|
|
|
foreach ($this->oldToNewFunctions as $oldFunction => $newFunction) {
|
|
|
|
if ($currentFunction === $oldFunction) {
|
2023-12-02 05:05:22 +00:00
|
|
|
return $newFunction;
|
2018-10-16 06:38:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-02 05:05:22 +00:00
|
|
|
return null;
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2023-12-02 05:05:22 +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
|
|
|
}
|
|
|
|
/**
|
2023-05-10 05:18:05 +00:00
|
|
|
* @innerForeachReturn 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
|
|
|
{
|
2023-05-10 05:18:05 +00:00
|
|
|
return [StmtsAwareInterface::class];
|
2018-10-16 06:38:03 +00:00
|
|
|
}
|
|
|
|
/**
|
2023-05-10 05:18:05 +00:00
|
|
|
* @param StmtsAwareInterface $node
|
2018-10-16 06:38:03 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2018-10-16 06:38:03 +00:00
|
|
|
{
|
2023-05-10 05:18:05 +00:00
|
|
|
if ($node->stmts === null) {
|
2018-10-16 06:38:03 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-05-10 05:18:05 +00:00
|
|
|
$hasChanged = \false;
|
|
|
|
foreach ($node->stmts as $key => $stmt) {
|
|
|
|
if (!$stmt instanceof Foreach_) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$foreach = $stmt;
|
|
|
|
if (!$foreach->keyVar instanceof Expr) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$foreachReturnOrAssign = $this->matchForeachReturnOrAssign($foreach);
|
|
|
|
if ($foreachReturnOrAssign instanceof Expression) {
|
|
|
|
/** @var Assign $innerAssign */
|
|
|
|
$innerAssign = $foreachReturnOrAssign->expr;
|
|
|
|
if (!$this->nodeComparator->areNodesEqual($foreach->valueVar, $innerAssign->expr)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$assign = $this->processForeachNodeWithAssignInside($foreach, $innerAssign);
|
|
|
|
if (!$assign instanceof Assign) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$node->stmts[$key] = new Expression($assign);
|
|
|
|
$hasChanged = \true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($foreachReturnOrAssign instanceof Return_) {
|
|
|
|
if (!$this->nodeComparator->areNodesEqual($foreach->valueVar, $foreachReturnOrAssign->expr)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$nextStmt = $node->stmts[$key + 1] ?? null;
|
|
|
|
$return = $this->processForeachNodeWithReturnInside($foreach, $foreachReturnOrAssign, $nextStmt);
|
|
|
|
$node->stmts[$key] = $return;
|
|
|
|
// cleanup next return
|
|
|
|
if ($nextStmt instanceof Return_) {
|
|
|
|
unset($node->stmts[$key + 1]);
|
|
|
|
}
|
|
|
|
$hasChanged = \true;
|
|
|
|
}
|
2018-10-16 06:38:03 +00:00
|
|
|
}
|
2023-05-10 05:18:05 +00:00
|
|
|
if ($hasChanged) {
|
|
|
|
return $node;
|
2018-10-16 06:38:03 +00:00
|
|
|
}
|
2023-05-10 05:18:05 +00:00
|
|
|
return null;
|
2018-10-16 06:38:03 +00:00
|
|
|
}
|
2021-07-21 09:35:57 +00:00
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return PhpVersionFeature::NULL_COALESCE;
|
2021-07-21 09:35:57 +00:00
|
|
|
}
|
2018-10-31 15:34:37 +00:00
|
|
|
/**
|
2023-05-10 05:18:05 +00:00
|
|
|
* @todo make assign expr generic
|
|
|
|
* @return Expression<Assign>|Return_|null
|
2018-10-31 15:34:37 +00:00
|
|
|
*/
|
2023-05-10 05:18:05 +00:00
|
|
|
private function matchForeachReturnOrAssign(Foreach_ $foreach)
|
2018-10-31 15:34:37 +00:00
|
|
|
{
|
2023-05-10 05:18:05 +00:00
|
|
|
if (\count($foreach->stmts) !== 1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$onlyForeachStmt = $foreach->stmts[0];
|
|
|
|
if (!$onlyForeachStmt instanceof If_) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$if = $onlyForeachStmt;
|
|
|
|
if (!$if->cond instanceof Identical) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (\count($if->stmts) !== 1) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-12-02 04:51:32 +00:00
|
|
|
if ($if->else instanceof Else_ || $if->elseifs !== []) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-05-10 05:18:05 +00:00
|
|
|
$innerStmt = $if->stmts[0];
|
|
|
|
if ($innerStmt instanceof Return_) {
|
|
|
|
return $innerStmt;
|
|
|
|
}
|
|
|
|
if (!$innerStmt instanceof Expression) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$innerNode = $innerStmt->expr;
|
|
|
|
if ($innerNode instanceof Assign) {
|
|
|
|
if ($innerNode->var instanceof ArrayDimFetch) {
|
2018-10-31 15:34:37 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-05-10 05:18:05 +00:00
|
|
|
return $innerStmt;
|
|
|
|
}
|
|
|
|
return null;
|
2018-10-31 15:34:37 +00:00
|
|
|
}
|
2023-05-10 05:18:05 +00:00
|
|
|
private function processForeachNodeWithReturnInside(Foreach_ $foreach, Return_ $innerForeachReturn, ?Stmt $nextStmt) : ?\PhpParser\Node\Stmt\Return_
|
2018-10-16 06:38:03 +00:00
|
|
|
{
|
2023-05-10 05:18:05 +00:00
|
|
|
if (!$this->nodeComparator->areNodesEqual($foreach->valueVar, $innerForeachReturn->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;
|
|
|
|
}
|
2023-05-10 05:18:05 +00:00
|
|
|
$coalesce = new Coalesce(new ArrayDimFetch($foreach->expr, $checkedNode), $nextStmt instanceof Return_ && $nextStmt->expr instanceof Expr ? $nextStmt->expr : $checkedNode);
|
|
|
|
return new Return_($coalesce);
|
2018-10-16 06:38:03 +00:00
|
|
|
}
|
2023-05-10 05:18:05 +00:00
|
|
|
private function processForeachNodeWithAssignInside(Foreach_ $foreach, Assign $assign) : ?Assign
|
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-07 08:22:29 +00:00
|
|
|
$arrayDimFetch = new ArrayDimFetch($foreach->expr, $keyNode);
|
|
|
|
return new Assign($checkedNode, new Coalesce($arrayDimFetch, $checkedNode));
|
2018-10-16 06:38:03 +00:00
|
|
|
}
|
|
|
|
}
|