[EarlyReturn] Clean up unnecessary tweak on ChangeOrIfReturnToEarlyReturnRector (#1843)

This commit is contained in:
Abdul Malik Ikhsan 2022-02-20 21:07:51 +07:00 committed by GitHub
parent 2dd05f65da
commit 33158773d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 160 additions and 66 deletions

View File

@ -0,0 +1,20 @@
<?php
namespace Rector\Tests\EarlyReturn\Rector\If_\ChangeOrIfReturnToEarlyReturnRector\Fixture;
use PhpParser\Node\Scalar\DNumber;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;
final class SkipInstanceOfMany
{
public function run($value)
{
// a comment
if ($value instanceof String_ && $value instanceof DNumber || $value instanceof LNumber) {
return null;
}
return 'another';
}
}

View File

@ -0,0 +1,20 @@
<?php
namespace Rector\Tests\EarlyReturn\Rector\If_\ChangeOrIfReturnToEarlyReturnRector\Fixture;
use PhpParser\Node\Scalar\DNumber;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;
final class SkipInstanceOfMany
{
public function run($value)
{
// a comment
if ($value instanceof String_ || $value instanceof DNumber && $value instanceof LNumber) {
return null;
}
return 'another';
}
}

View File

@ -6,7 +6,7 @@ namespace Rector\EarlyReturn\Rector\If_;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Instanceof_;
@ -14,6 +14,7 @@ use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use Rector\Core\NodeManipulator\IfManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@ -88,7 +89,7 @@ CODE_SAMPLE
return null;
}
if ($this->isInstanceofCondOnlyOrHasBooleanAnd($node->cond)) {
if ($this->isInstanceofCondOnly($node->cond)) {
return null;
}
@ -99,6 +100,13 @@ CODE_SAMPLE
/** @var Return_ $return */
$return = $node->stmts[0];
// same return? skip
$next = $node->getAttribute(AttributeKey::NEXT_NODE);
if ($next instanceof Return_ && $this->nodeComparator->areNodesEqual($return, $next)) {
return null;
}
$ifs = $this->createMultipleIfs($node->cond, $return, []);
// ensure ifs not removed by other rules
@ -171,26 +179,16 @@ CODE_SAMPLE
);
}
private function isInstanceofCondOnlyOrHasBooleanAnd(BooleanOr $booleanOr): bool
private function isInstanceofCondOnly(BooleanOr|BinaryOp $booleanOr): bool
{
$currentNode = $booleanOr;
if ($currentNode->left instanceof BooleanAnd || $currentNode->right instanceof BooleanAnd) {
return true;
if ($booleanOr->left instanceof BinaryOp) {
return $this->isInstanceofCondOnly($booleanOr->left);
}
if ($currentNode->left instanceof BooleanOr) {
return $this->isInstanceofCondOnlyOrHasBooleanAnd($currentNode->left);
if ($booleanOr->right instanceof BinaryOp) {
return $this->isInstanceofCondOnly($booleanOr->right);
}
if ($currentNode->right instanceof BooleanOr) {
return $this->isInstanceofCondOnlyOrHasBooleanAnd($currentNode->right);
}
if (! $currentNode->right instanceof Instanceof_) {
return false;
}
return $currentNode->left instanceof Instanceof_;
return $booleanOr->right instanceof Instanceof_;
}
}

View File

@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Tests\Issues\IssueEarlyReturnAndOr\Fixture;
class AndNextOr
{
public function run($a, $b, $c, $d)
{
if (($a && false === $b) || ! $c) {
return '';
}
return $d;
}
}
?>
-----
<?php
declare(strict_types=1);
namespace Rector\Core\Tests\Issues\IssueEarlyReturnAndOr\Fixture;
class AndNextOr
{
public function run($a, $b, $c, $d)
{
if ($a && false === $b) {
return '';
}
if (! $c) {
return '';
}
return $d;
}
}
?>

View File

@ -1,19 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Tests\Issues\IssueEarlyReturnAndOr\Fixture;
class SkipAndNextOr
{
public function run($a, $b, $c, $d)
{
if (($a && false === $b) || ! $c) {
return '';
}
return $d;
}
}
?>

View File

@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Tests\Issues\IssueEarlyReturnAndOrDatetime\Fixture;
class AndNextOr
{
public function run($a, $b, $c, $d)
{
if ($a && $b || $c) {
return null;
}
$a = 1;
$b = 2;
if ($b > $a) {
return null;
}
$d = 1;
return;
}
}
?>
-----
<?php
declare(strict_types=1);
namespace Rector\Core\Tests\Issues\IssueEarlyReturnAndOrDatetime\Fixture;
class AndNextOr
{
public function run($a, $b, $c, $d)
{
if ($a && $b) {
return null;
}
if ($c) {
return null;
}
$a = 1;
$b = 2;
if ($b > $a) {
return null;
}
$d = 1;
return;
}
}
?>

View File

@ -1,28 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Tests\Issues\IssueEarlyReturnAndOrDatetime\Fixture;
class SkipAndNextOr
{
public function run($a, $b, $c, $d)
{
if ($a && $b || $c) {
return null;
}
$a = 1;
$b = 2;
if ($b > $a) {
return null;
}
$d = 1;
return;
}
}
?>

View File

@ -29,7 +29,10 @@ class ComplexIfCondOr
{
public function run($a, $b, $c, $d, $e)
{
if (($a && false === $b) || ! $c) {
if ($a && false === $b) {
return '';
}
if (! $c) {
return '';
}
if ($d) {