2018-10-16 06:38:03 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
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_;
|
|
|
|
use Rector\NodeTypeResolver\Node\Attribute;
|
2018-11-04 01:17:02 +00:00
|
|
|
use Rector\PhpParser\Node\Maintainer\ForeachMaintainer;
|
2018-10-16 06:38:03 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
|
|
|
use Rector\RectorDefinition\CodeSample;
|
|
|
|
use Rector\RectorDefinition\RectorDefinition;
|
|
|
|
|
|
|
|
final class SimplifyForeachToCoalescingRector extends AbstractRector
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var Return_|null
|
|
|
|
*/
|
2018-10-17 12:41:28 +00:00
|
|
|
private $returnNode;
|
2018-10-16 06:38:03 +00:00
|
|
|
|
2018-10-21 10:32:51 +00:00
|
|
|
/**
|
2018-11-04 01:17:02 +00:00
|
|
|
* @var ForeachMaintainer
|
2018-10-21 10:32:51 +00:00
|
|
|
*/
|
2018-11-04 01:17:02 +00:00
|
|
|
private $foreachMaintainer;
|
2018-10-21 10:32:51 +00:00
|
|
|
|
2018-11-04 01:17:02 +00:00
|
|
|
public function __construct(ForeachMaintainer $foreachMaintainer)
|
2018-10-21 10:32:51 +00:00
|
|
|
{
|
2018-11-04 01:17:02 +00:00
|
|
|
$this->foreachMaintainer = $foreachMaintainer;
|
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(
|
|
|
|
<<<'CODE_SAMPLE'
|
|
|
|
foreach ($this->oldToNewFunctions as $oldFunction => $newFunction) {
|
|
|
|
if ($currentFunction === $oldFunction) {
|
|
|
|
return $newFunction;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
CODE_SAMPLE
|
|
|
|
,
|
|
|
|
<<<'CODE_SAMPLE'
|
|
|
|
return $this->oldToNewFunctions[$currentFunction] ?? null;
|
|
|
|
CODE_SAMPLE
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
{
|
2018-12-15 20:28:41 +00:00
|
|
|
$this->returnNode = 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);
|
|
|
|
if (! $returnOrAssignNode) {
|
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
|
|
|
|
*/
|
|
|
|
private function matchReturnOrAssignNode(Foreach_ $foreachNode): ?Node
|
|
|
|
{
|
2018-11-04 01:17:02 +00:00
|
|
|
return $this->foreachMaintainer->matchOnlyStmt($foreachNode, 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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-16 06:38:03 +00:00
|
|
|
private function processForeachNodeWithReturnInside(Foreach_ $foreachNode, Return_ $returnNode): ?Node
|
|
|
|
{
|
|
|
|
if (! $this->areNodesEqual($foreachNode->valueVar, $returnNode->expr)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var If_ $ifNode */
|
|
|
|
$ifNode = $foreachNode->stmts[0];
|
|
|
|
|
|
|
|
/** @var Identical $identicalNode */
|
|
|
|
$identicalNode = $ifNode->cond;
|
|
|
|
|
|
|
|
if ($this->areNodesEqual($identicalNode->left, $foreachNode->keyVar)) {
|
|
|
|
$checkedNode = $identicalNode->right;
|
|
|
|
} elseif ($this->areNodesEqual($identicalNode->right, $foreachNode->keyVar)) {
|
|
|
|
$checkedNode = $identicalNode->left;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// is next node Return?
|
|
|
|
if ($foreachNode->getAttribute(Attribute::NEXT_NODE) instanceof Return_) {
|
2018-10-17 12:41:28 +00:00
|
|
|
$this->returnNode = $foreachNode->getAttribute(Attribute::NEXT_NODE);
|
|
|
|
$this->removeNode($this->returnNode);
|
2018-10-16 06:38:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$coalesceNode = new Coalesce(new ArrayDimFetch(
|
|
|
|
$foreachNode->expr,
|
|
|
|
$checkedNode
|
2019-01-14 18:21:23 +00:00
|
|
|
), $this->returnNode && $this->returnNode->expr !== null ? $this->returnNode->expr : $checkedNode);
|
2018-10-16 06:38:03 +00:00
|
|
|
|
2019-02-17 14:12:47 +00:00
|
|
|
if ($this->returnNode !== null) {
|
2018-10-16 06:38:03 +00:00
|
|
|
return new Return_($coalesceNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function processForeachNodeWithAssignInside(Foreach_ $foreachNode, Assign $assignNode): ?Node
|
|
|
|
{
|
|
|
|
/** @var If_ $ifNode */
|
|
|
|
$ifNode = $foreachNode->stmts[0];
|
|
|
|
|
|
|
|
/** @var Identical $identicalNode */
|
|
|
|
$identicalNode = $ifNode->cond;
|
|
|
|
|
|
|
|
if ($this->areNodesEqual($identicalNode->left, $foreachNode->keyVar)) {
|
|
|
|
$checkedNode = $assignNode->var;
|
2018-12-15 20:28:41 +00:00
|
|
|
$keyNode = $identicalNode->right;
|
2018-10-16 06:38:03 +00:00
|
|
|
} elseif ($this->areNodesEqual($identicalNode->right, $foreachNode->keyVar)) {
|
|
|
|
$checkedNode = $assignNode->var;
|
2018-12-15 20:28:41 +00:00
|
|
|
$keyNode = $identicalNode->left;
|
2018-10-16 06:38:03 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-12-15 20:28:41 +00:00
|
|
|
$arrayDimFetchNode = new ArrayDimFetch($foreachNode->expr, $keyNode);
|
|
|
|
|
|
|
|
return new Assign($checkedNode, new Coalesce($arrayDimFetchNode, $checkedNode));
|
2018-10-16 06:38:03 +00:00
|
|
|
}
|
|
|
|
}
|