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\If_;
|
2018-10-26 22:16:09 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
2024-03-07 13:46:36 +00:00
|
|
|
use PhpParser\Node\Expr\BinaryOp\Equal;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Expr\BinaryOp\Identical;
|
2024-03-07 13:46:36 +00:00
|
|
|
use PhpParser\Node\Expr\BinaryOp\NotEqual;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
|
|
|
|
use PhpParser\Node\Expr\BooleanNot;
|
2023-03-23 23:21:34 +00:00
|
|
|
use PhpParser\Node\Stmt\Else_;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Stmt\If_;
|
|
|
|
use PhpParser\Node\Stmt\Return_;
|
|
|
|
use Rector\BetterPhpDocParser\Comment\CommentsMerger;
|
|
|
|
use Rector\CodeQuality\NodeManipulator\ExprBoolCaster;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
|
|
|
|
use Rector\PhpParser\Node\Value\ValueResolver;
|
|
|
|
use Rector\PhpParser\Printer\BetterStandardPrinter;
|
|
|
|
use Rector\Rector\AbstractRector;
|
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
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\CodeQuality\Rector\If_\SimplifyIfReturnBoolRector\SimplifyIfReturnBoolRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class SimplifyIfReturnBoolRector extends AbstractRector
|
2018-10-26 22:16:09 +00:00
|
|
|
{
|
2019-11-04 16:30:10 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\BetterPhpDocParser\Comment\CommentsMerger
|
2019-11-04 16:30:10 +00:00
|
|
|
*/
|
2021-01-16 21:45:18 +00:00
|
|
|
private $commentsMerger;
|
2020-12-04 23:22:12 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\CodeQuality\NodeManipulator\ExprBoolCaster
|
2020-12-04 23:22:12 +00:00
|
|
|
*/
|
2021-01-16 21:45:18 +00:00
|
|
|
private $exprBoolCaster;
|
2022-04-09 22:42:24 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\Printer\BetterStandardPrinter
|
2022-04-09 22:42:24 +00:00
|
|
|
*/
|
2023-06-08 14:23:03 +00:00
|
|
|
private $betterStandardPrinter;
|
2023-09-20 12:53:23 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\Node\Value\ValueResolver
|
2023-09-20 12:53:23 +00:00
|
|
|
*/
|
|
|
|
private $valueResolver;
|
|
|
|
public function __construct(CommentsMerger $commentsMerger, ExprBoolCaster $exprBoolCaster, BetterStandardPrinter $betterStandardPrinter, ValueResolver $valueResolver)
|
2020-12-06 00:27:00 +00:00
|
|
|
{
|
2020-12-04 23:22:12 +00:00
|
|
|
$this->commentsMerger = $commentsMerger;
|
2021-01-16 21:45:18 +00:00
|
|
|
$this->exprBoolCaster = $exprBoolCaster;
|
2023-06-08 14:23:03 +00:00
|
|
|
$this->betterStandardPrinter = $betterStandardPrinter;
|
2023-09-20 12:53:23 +00:00
|
|
|
$this->valueResolver = $valueResolver;
|
2019-11-04 16:30:10 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-10-26 22:16:09 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Shortens if return false/true to direct return', [new CodeSample(<<<'CODE_SAMPLE'
|
2018-10-26 22:16:09 +00:00
|
|
|
if (strpos($docToken->getContent(), "\n") === false) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2023-05-21 15:34:58 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
|
|
|
return strpos($docToken->getContent(), "\n") === false;
|
|
|
|
CODE_SAMPLE
|
|
|
|
)]);
|
2018-10-26 22:16:09 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-03-07 17:59:10 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-10-26 22:16:09 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2018-10-26 22:16:09 +00:00
|
|
|
{
|
2023-05-21 15:34:58 +00:00
|
|
|
return [StmtsAwareInterface::class];
|
2018-10-26 22:16:09 +00:00
|
|
|
}
|
|
|
|
/**
|
2023-05-21 15:34:58 +00:00
|
|
|
* @param StmtsAwareInterface $node
|
2018-10-26 22:16:09 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2018-10-26 22:16:09 +00:00
|
|
|
{
|
2023-05-21 15:34:58 +00:00
|
|
|
if ($node->stmts === null) {
|
2018-10-26 22:16:09 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-05-21 15:34:58 +00:00
|
|
|
foreach ($node->stmts as $key => $stmt) {
|
|
|
|
if (!$stmt instanceof Return_) {
|
|
|
|
continue;
|
2022-03-27 18:43:03 +00:00
|
|
|
}
|
2023-05-21 15:34:58 +00:00
|
|
|
$previousStmt = $node->stmts[$key - 1] ?? null;
|
|
|
|
if (!$previousStmt instanceof If_) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$if = $previousStmt;
|
|
|
|
if ($this->shouldSkipIfAndReturn($previousStmt, $stmt)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$return = $stmt;
|
|
|
|
/** @var Return_ $ifInnerNode */
|
|
|
|
$ifInnerNode = $if->stmts[0];
|
|
|
|
$innerIfInnerNode = $ifInnerNode->expr;
|
|
|
|
if (!$innerIfInnerNode instanceof Expr) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$newReturn = $this->resolveReturn($innerIfInnerNode, $if, $return);
|
|
|
|
if (!$newReturn instanceof Return_) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$this->commentsMerger->keepComments($newReturn, [$if, $return, $ifInnerNode]);
|
|
|
|
// remove previous IF
|
|
|
|
unset($node->stmts[$key - 1]);
|
|
|
|
$node->stmts[$key] = $newReturn;
|
|
|
|
return $node;
|
2018-10-26 22:16:09 +00:00
|
|
|
}
|
2023-05-21 15:34:58 +00:00
|
|
|
return null;
|
2018-10-26 22:16:09 +00:00
|
|
|
}
|
2023-05-21 15:34:58 +00:00
|
|
|
private function shouldSkipIfAndReturn(If_ $if, Return_ $return) : bool
|
2018-10-26 22:16:09 +00:00
|
|
|
{
|
2020-10-29 08:59:39 +00:00
|
|
|
if ($if->elseifs !== []) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2020-05-12 14:17:39 +00:00
|
|
|
}
|
2020-06-29 21:19:37 +00:00
|
|
|
if ($this->isElseSeparatedThenIf($if)) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2020-05-12 14:17:39 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->isIfWithSingleReturnExpr($if)) {
|
|
|
|
return \true;
|
2018-11-05 01:41:47 +00:00
|
|
|
}
|
2019-11-04 16:30:10 +00:00
|
|
|
/** @var Return_ $ifInnerNode */
|
2020-06-29 21:19:37 +00:00
|
|
|
$ifInnerNode = $if->stmts[0];
|
2019-11-08 23:12:44 +00:00
|
|
|
/** @var Expr $returnedExpr */
|
|
|
|
$returnedExpr = $ifInnerNode->expr;
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->valueResolver->isTrueOrFalse($returnedExpr)) {
|
|
|
|
return \true;
|
2018-10-26 22:16:09 +00:00
|
|
|
}
|
2023-05-21 15:34:58 +00:00
|
|
|
if (!$return->expr instanceof Expr) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2018-10-26 22:16:09 +00:00
|
|
|
}
|
2018-11-07 01:37:39 +00:00
|
|
|
// negate + negate → skip for now
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->valueResolver->isFalse($returnedExpr)) {
|
2023-05-21 15:34:58 +00:00
|
|
|
return !$this->valueResolver->isTrueOrFalse($return->expr);
|
2018-11-07 01:37:39 +00:00
|
|
|
}
|
2023-06-08 14:23:03 +00:00
|
|
|
$condString = $this->betterStandardPrinter->print($if->cond);
|
2021-06-27 14:06:45 +00:00
|
|
|
if (\strpos($condString, '!=') === \false) {
|
2023-05-21 15:34:58 +00:00
|
|
|
return !$this->valueResolver->isTrueOrFalse($return->expr);
|
2021-02-21 09:32:45 +00:00
|
|
|
}
|
2024-03-07 13:46:36 +00:00
|
|
|
return !$if->cond instanceof NotIdentical && !$if->cond instanceof NotEqual;
|
2018-10-26 22:16:09 +00:00
|
|
|
}
|
2023-09-09 22:49:40 +00:00
|
|
|
private function processReturnTrue(If_ $if, Return_ $nextReturn) : Return_
|
2018-10-26 22:16:09 +00:00
|
|
|
{
|
2023-09-09 22:49:40 +00:00
|
|
|
if ($if->cond instanceof BooleanNot && $nextReturn->expr instanceof Expr && $this->valueResolver->isTrue($nextReturn->expr)) {
|
2022-06-07 08:22:29 +00:00
|
|
|
return new Return_($this->exprBoolCaster->boolCastOrNullCompareIfNeeded($if->cond->expr));
|
2018-10-26 22:16:09 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return new Return_($this->exprBoolCaster->boolCastOrNullCompareIfNeeded($if->cond));
|
2018-10-26 22:16:09 +00:00
|
|
|
}
|
2023-09-09 22:49:40 +00:00
|
|
|
private function processReturnFalse(If_ $if, Return_ $nextReturn) : ?Return_
|
2018-10-26 22:16:09 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($if->cond instanceof Identical) {
|
|
|
|
$notIdentical = new NotIdentical($if->cond->left, $if->cond->right);
|
|
|
|
return new Return_($this->exprBoolCaster->boolCastOrNullCompareIfNeeded($notIdentical));
|
2018-10-26 22:16:09 +00:00
|
|
|
}
|
2024-03-07 13:46:36 +00:00
|
|
|
if ($if->cond instanceof Equal) {
|
|
|
|
$notIdentical = new NotEqual($if->cond->left, $if->cond->right);
|
|
|
|
return new Return_($this->exprBoolCaster->boolCastOrNullCompareIfNeeded($notIdentical));
|
|
|
|
}
|
2023-09-09 22:49:40 +00:00
|
|
|
if (!$nextReturn->expr instanceof Expr) {
|
2019-01-14 18:21:23 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-09-09 22:49:40 +00:00
|
|
|
if (!$this->valueResolver->isTrue($nextReturn->expr)) {
|
2018-11-09 23:15:36 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($if->cond instanceof BooleanNot) {
|
|
|
|
return new Return_($this->exprBoolCaster->boolCastOrNullCompareIfNeeded($if->cond->expr));
|
2018-10-26 22:16:09 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return new Return_($this->exprBoolCaster->boolCastOrNullCompareIfNeeded(new BooleanNot($if->cond)));
|
2018-10-26 22:16:09 +00:00
|
|
|
}
|
2020-08-05 20:45:36 +00:00
|
|
|
/**
|
|
|
|
* Matches: "else if"
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
private function isElseSeparatedThenIf(If_ $if) : bool
|
2020-08-05 20:45:36 +00:00
|
|
|
{
|
2023-03-23 23:21:34 +00:00
|
|
|
if (!$if->else instanceof Else_) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2020-08-05 20:45:36 +00:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (\count($if->else->stmts) !== 1) {
|
|
|
|
return \false;
|
2020-08-05 20:45:36 +00:00
|
|
|
}
|
|
|
|
$onlyStmt = $if->else->stmts[0];
|
2022-06-07 08:22:29 +00:00
|
|
|
return $onlyStmt instanceof If_;
|
2020-08-05 20:45:36 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function isIfWithSingleReturnExpr(If_ $if) : bool
|
2020-02-01 16:04:38 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if (\count($if->stmts) !== 1) {
|
|
|
|
return \false;
|
2020-02-01 16:04:38 +00:00
|
|
|
}
|
2020-10-29 08:59:39 +00:00
|
|
|
if ($if->elseifs !== []) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2020-02-01 16:04:38 +00:00
|
|
|
}
|
|
|
|
$ifInnerNode = $if->stmts[0];
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$ifInnerNode instanceof Return_) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2020-02-01 16:04:38 +00:00
|
|
|
}
|
|
|
|
// return must have value
|
2023-03-23 23:21:34 +00:00
|
|
|
return $ifInnerNode->expr instanceof Expr;
|
2020-02-01 16:04:38 +00:00
|
|
|
}
|
2023-05-21 15:34:58 +00:00
|
|
|
private function resolveReturn(Expr $innerExpr, If_ $if, Return_ $return) : ?Return_
|
|
|
|
{
|
|
|
|
if ($this->valueResolver->isTrue($innerExpr)) {
|
|
|
|
return $this->processReturnTrue($if, $return);
|
|
|
|
}
|
|
|
|
if ($this->valueResolver->isFalse($innerExpr)) {
|
|
|
|
/** @var Expr $expr */
|
|
|
|
$expr = $return->expr;
|
|
|
|
if ($if->cond instanceof NotIdentical && $this->valueResolver->isTrue($expr)) {
|
|
|
|
$if->cond = new Identical($if->cond->left, $if->cond->right);
|
|
|
|
return $this->processReturnTrue($if, $return);
|
|
|
|
}
|
2024-03-07 13:46:36 +00:00
|
|
|
if ($if->cond instanceof NotEqual && $this->valueResolver->isTrue($expr)) {
|
|
|
|
$if->cond = new Equal($if->cond->left, $if->cond->right);
|
|
|
|
return $this->processReturnTrue($if, $return);
|
|
|
|
}
|
2023-05-21 15:34:58 +00:00
|
|
|
return $this->processReturnFalse($if, $return);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2018-10-26 22:16:09 +00:00
|
|
|
}
|