2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2018-10-16 06:38:03 +00:00
|
|
|
|
|
|
|
namespace Rector\CodeQuality\Rector\Foreach_;
|
|
|
|
|
|
|
|
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_;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\PhpParser\Node\Manipulator\ForeachManipulator;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
|
|
|
use Rector\Core\RectorDefinition\CodeSample;
|
|
|
|
use Rector\Core\RectorDefinition\RectorDefinition;
|
|
|
|
use Rector\Core\ValueObject\PhpVersionFeature;
|
2019-04-13 09:20:27 +00:00
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2018-10-16 06:38:03 +00:00
|
|
|
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2019-12-29 15:38:02 +00:00
|
|
|
* @see https://3v4l.org/bfsdY
|
|
|
|
*
|
2019-09-03 09:11:45 +00:00
|
|
|
* @see \Rector\CodeQuality\Tests\Rector\Foreach_\SimplifyForeachToCoalescingRector\SimplifyForeachToCoalescingRectorTest
|
|
|
|
*/
|
2018-10-16 06:38:03 +00:00
|
|
|
final class SimplifyForeachToCoalescingRector extends AbstractRector
|
|
|
|
{
|
|
|
|
/**
|
2020-07-31 20:20:45 +00:00
|
|
|
* @var ForeachManipulator
|
2018-10-16 06:38:03 +00:00
|
|
|
*/
|
2020-07-31 20:20:45 +00:00
|
|
|
private $foreachManipulator;
|
2018-10-16 06:38:03 +00:00
|
|
|
|
2018-10-21 10:32:51 +00:00
|
|
|
/**
|
2020-07-31 20:20:45 +00:00
|
|
|
* @var Return_|null
|
2018-10-21 10:32:51 +00:00
|
|
|
*/
|
2020-07-31 20:20:45 +00:00
|
|
|
private $return;
|
2018-10-21 10:32:51 +00:00
|
|
|
|
2019-02-27 21:54:39 +00:00
|
|
|
public function __construct(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
|
|
|
}
|
|
|
|
|
2018-10-16 06:38:03 +00:00
|
|
|
public function getDefinition(): RectorDefinition
|
|
|
|
{
|
|
|
|
return new RectorDefinition('Changes foreach that returns set value to ??', [
|
|
|
|
new CodeSample(
|
2020-09-15 08:23:13 +00:00
|
|
|
<<<'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
|
2018-10-16 06:38:03 +00:00
|
|
|
,
|
2020-09-15 08:23:13 +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
|
2018-10-16 06:38:03 +00:00
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
2018-10-17 12:41:28 +00:00
|
|
|
return [Foreach_::class];
|
2018-10-16 06:38:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-17 12:41:28 +00:00
|
|
|
* @param Foreach_ $node
|
2018-10-16 06:38:03 +00:00
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
2019-09-15 14:07:29 +00:00
|
|
|
if (! $this->isAtLeastPhpVersion(PhpVersionFeature::NULL_COALESCE)) {
|
2019-02-23 13:03:07 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-06-29 21:19:37 +00:00
|
|
|
$this->return = null;
|
2018-12-15 20:28:41 +00:00
|
|
|
|
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
|
2018-10-21 10:32:51 +00:00
|
|
|
if (! $this->areNodesEqual($node->valueVar, $returnOrAssignNode->expr)) {
|
2018-10-16 06:38:03 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-10-21 10:32:51 +00:00
|
|
|
if ($returnOrAssignNode instanceof Return_) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-10-31 15:34:37 +00:00
|
|
|
/**
|
|
|
|
* @return Assign|Return_|null
|
|
|
|
*/
|
2020-06-29 21:19:37 +00:00
|
|
|
private function matchReturnOrAssignNode(Foreach_ $foreach): ?Node
|
2018-10-31 15:34:37 +00:00
|
|
|
{
|
2020-06-29 21:19:37 +00:00
|
|
|
return $this->foreachManipulator->matchOnlyStmt($foreach, function (Node $node): ?Node {
|
2018-10-31 15:34:37 +00:00
|
|
|
if (! $node instanceof If_) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $node->cond instanceof Identical) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($node->stmts) !== 1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-02-09 19:08:08 +00:00
|
|
|
$innerNode = $node->stmts[0] instanceof Expression ? $node->stmts[0]->expr : $node->stmts[0];
|
2018-10-31 15:34:37 +00:00
|
|
|
|
|
|
|
if ($innerNode instanceof Assign || $innerNode instanceof Return_) {
|
|
|
|
return $innerNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-29 21:19:37 +00:00
|
|
|
private function processForeachNodeWithReturnInside(Foreach_ $foreach, Return_ $return): ?Node
|
2018-10-16 06:38:03 +00:00
|
|
|
{
|
2020-06-29 21:19:37 +00:00
|
|
|
if (! $this->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;
|
|
|
|
|
2020-06-29 21:19:37 +00:00
|
|
|
if ($this->areNodesEqual($identicalNode->left, $foreach->keyVar)) {
|
2018-10-16 06:38:03 +00:00
|
|
|
$checkedNode = $identicalNode->right;
|
2020-06-29 21:19:37 +00:00
|
|
|
} elseif ($this->areNodesEqual($identicalNode->right, $foreach->keyVar)) {
|
2018-10-16 06:38:03 +00:00
|
|
|
$checkedNode = $identicalNode->left;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// is next node Return?
|
2020-06-29 21:19:37 +00:00
|
|
|
if ($foreach->getAttribute(AttributeKey::NEXT_NODE) instanceof Return_) {
|
|
|
|
$this->return = $foreach->getAttribute(AttributeKey::NEXT_NODE);
|
|
|
|
$this->removeNode($this->return);
|
2018-10-16 06:38:03 +00:00
|
|
|
}
|
|
|
|
|
2020-06-29 21:19:37 +00:00
|
|
|
$coalesce = new Coalesce(new ArrayDimFetch(
|
|
|
|
$foreach->expr,
|
2018-10-16 06:38:03 +00:00
|
|
|
$checkedNode
|
2020-06-29 21:19:37 +00:00
|
|
|
), $this->return && $this->return->expr !== null ? $this->return->expr : $checkedNode);
|
2018-10-16 06:38:03 +00:00
|
|
|
|
2020-06-29 21:19:37 +00:00
|
|
|
if ($this->return !== null) {
|
|
|
|
return new Return_($coalesce);
|
2018-10-16 06:38:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-06-29 21:19:37 +00:00
|
|
|
private function processForeachNodeWithAssignInside(Foreach_ $foreach, Assign $assign): ?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;
|
|
|
|
|
2020-06-29 21:19:37 +00:00
|
|
|
if ($this->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;
|
2020-06-29 21:19:37 +00:00
|
|
|
} elseif ($this->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;
|
|
|
|
}
|
|
|
|
|
2020-06-29 21:19:37 +00:00
|
|
|
$arrayDimFetch = new ArrayDimFetch($foreach->expr, $keyNode);
|
2018-12-15 20:28:41 +00:00
|
|
|
|
2020-06-29 21:19:37 +00:00
|
|
|
return new Assign($checkedNode, new Coalesce($arrayDimFetch, $checkedNode));
|
2018-10-16 06:38:03 +00:00
|
|
|
}
|
|
|
|
}
|