2020-12-19 08:56:24 +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\EarlyReturn\Rector\Return_;
|
2020-12-19 08:56:24 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
|
|
|
|
use PhpParser\Node\Stmt\If_;
|
|
|
|
use PhpParser\Node\Stmt\Return_;
|
|
|
|
use Rector\Core\NodeAnalyzer\CallAnalyzer;
|
|
|
|
use Rector\Core\NodeManipulator\IfManipulator;
|
|
|
|
use Rector\Core\PhpParser\Node\AssignAndBinaryMap;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2020-12-19 08:56:24 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\EarlyReturn\Rector\Return_\ReturnBinaryAndToEarlyReturnRector\ReturnBinaryAndToEarlyReturnRectorTest
|
2020-12-19 08:56:24 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class ReturnBinaryAndToEarlyReturnRector extends AbstractRector
|
2020-12-19 08:56:24 +00:00
|
|
|
{
|
2021-02-09 19:07:20 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Core\NodeManipulator\IfManipulator
|
2021-02-09 19:07:20 +00:00
|
|
|
*/
|
|
|
|
private $ifManipulator;
|
2021-02-25 08:41:43 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Core\PhpParser\Node\AssignAndBinaryMap
|
2021-02-25 08:41:43 +00:00
|
|
|
*/
|
|
|
|
private $assignAndBinaryMap;
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Core\NodeAnalyzer\CallAnalyzer
|
2021-02-25 08:41:43 +00:00
|
|
|
*/
|
|
|
|
private $callAnalyzer;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(IfManipulator $ifManipulator, AssignAndBinaryMap $assignAndBinaryMap, CallAnalyzer $callAnalyzer)
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2021-02-09 19:07:20 +00:00
|
|
|
$this->ifManipulator = $ifManipulator;
|
2021-02-25 08:41:43 +00:00
|
|
|
$this->assignAndBinaryMap = $assignAndBinaryMap;
|
|
|
|
$this->callAnalyzer = $callAnalyzer;
|
2021-02-09 19:07:20 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2020-12-19 08:56:24 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Changes Single return of && to early returns', [new CodeSample(<<<'CODE_SAMPLE'
|
2020-12-19 08:56:24 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
2021-02-25 08:41:43 +00:00
|
|
|
public function accept()
|
2020-12-19 08:56:24 +00:00
|
|
|
{
|
2021-02-25 08:41:43 +00:00
|
|
|
return $this->something() && $this->somethingelse();
|
2020-12-19 08:56:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2020-12-19 08:56:24 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
2021-02-25 08:41:43 +00:00
|
|
|
public function accept()
|
2020-12-19 08:56:24 +00:00
|
|
|
{
|
2022-05-12 09:11:03 +00:00
|
|
|
if (! $this->something()) {
|
2020-12-19 08:56:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-05-12 09:11:03 +00:00
|
|
|
|
2021-02-25 08:41:43 +00:00
|
|
|
return (bool) $this->somethingelse();
|
2020-12-19 08:56:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2020-12-19 08:56:24 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2020-12-19 08:56:24 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2020-12-19 08:56:24 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Return_::class];
|
2020-12-19 08:56:24 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Return_ $node
|
2021-09-09 02:08:49 +00:00
|
|
|
* @return null|Node[]
|
2020-12-19 08:56:24 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?array
|
2020-12-19 08:56:24 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$node->expr instanceof BooleanAnd) {
|
2020-12-19 08:56:24 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$left = $node->expr->left;
|
|
|
|
$ifNegations = $this->createMultipleIfsNegation($left, $node, []);
|
2021-09-09 02:08:49 +00:00
|
|
|
// ensure ifs not removed by other rules
|
|
|
|
if ($ifNegations === []) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-08-13 20:43:09 +00:00
|
|
|
if (!$this->callAnalyzer->doesIfHasObjectCall($ifNegations)) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-02-25 08:41:43 +00:00
|
|
|
$this->mirrorComments($ifNegations[0], $node);
|
2021-04-04 09:01:11 +00:00
|
|
|
/** @var BooleanAnd $booleanAnd */
|
|
|
|
$booleanAnd = $node->expr;
|
|
|
|
$lastReturnExpr = $this->assignAndBinaryMap->getTruthyExpr($booleanAnd->right);
|
2022-06-07 08:22:29 +00:00
|
|
|
return \array_merge($ifNegations, [new Return_($lastReturnExpr)]);
|
2020-12-19 08:56:24 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param If_[] $ifNegations
|
|
|
|
* @return If_[]
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function createMultipleIfsNegation(Expr $expr, Return_ $return, array $ifNegations) : array
|
2020-12-19 08:56:24 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
while ($expr instanceof BooleanAnd) {
|
2021-05-09 20:15:43 +00:00
|
|
|
$ifNegations = \array_merge($ifNegations, $this->collectLeftBooleanAndToIfs($expr, $return, $ifNegations));
|
2022-06-07 08:22:29 +00:00
|
|
|
$ifNegations[] = $this->ifManipulator->createIfNegation($expr->right, new Return_($this->nodeFactory->createFalse()));
|
2020-12-19 08:56:24 +00:00
|
|
|
$expr = $expr->right;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return $ifNegations + [$this->ifManipulator->createIfNegation($expr, new Return_($this->nodeFactory->createFalse()))];
|
2020-12-19 08:56:24 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param If_[] $ifNegations
|
|
|
|
* @return If_[]
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function collectLeftBooleanAndToIfs(BooleanAnd $booleanAnd, Return_ $return, array $ifNegations) : array
|
2020-12-19 08:56:24 +00:00
|
|
|
{
|
|
|
|
$left = $booleanAnd->left;
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$left instanceof BooleanAnd) {
|
|
|
|
return [$this->ifManipulator->createIfNegation($left, new Return_($this->nodeFactory->createFalse()))];
|
2020-12-19 08:56:24 +00:00
|
|
|
}
|
|
|
|
return $this->createMultipleIfsNegation($left, $return, $ifNegations);
|
|
|
|
}
|
|
|
|
}
|