mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-27 13:13:31 +00:00
[EarlyReturn] Clean up unnecessary tweak on ChangeOrIfReturnToEarlyReturnRector (#1843)
This commit is contained in:
parent
2dd05f65da
commit
33158773d1
|
@ -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';
|
||||
}
|
||||
}
|
|
@ -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';
|
||||
}
|
||||
}
|
|
@ -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_;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user