Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[EarlyReturn] Skip ChangeAndIfToEarlyReturnRector in case of simple s…
…calar return (#2826)
- Loading branch information
1 parent
58edce7
commit 6306f9e
Showing
8 changed files
with
135 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
.../EarlyReturn/Rector/If_/ChangeAndIfToEarlyReturnRector/Fixture/skip_simple_return.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\EarlyReturn\Rector\If_\ChangeAndIfToEarlyReturnRector\Fixture; | ||
|
||
final class SkipSimpleReturn | ||
{ | ||
public function getEerrors($fileName) | ||
{ | ||
if (is_string($fileName) && str_contains($fileName, 'vendor')) { | ||
return []; | ||
} | ||
|
||
return ['some error']; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...turn/Rector/If_/ChangeAndIfToEarlyReturnRector/Fixture/skip_simple_return_of_bool.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\EarlyReturn\Rector\If_\ChangeAndIfToEarlyReturnRector\Fixture; | ||
|
||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Scalar\String_; | ||
|
||
final class SkipSimpleReturnOfBool | ||
{ | ||
public function resolve(Expr $expr) | ||
{ | ||
if ($expr instanceof String_ && $expr->value === '') { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\EarlyReturn\NodeAnalyzer; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\BinaryOp\BooleanAnd; | ||
use PhpParser\Node\Expr\Instanceof_; | ||
use PhpParser\Node\Stmt\If_; | ||
use PhpParser\Node\Stmt\Return_; | ||
use Rector\Core\PhpParser\Comparing\NodeComparator; | ||
use Rector\Core\PhpParser\Node\BetterNodeFinder; | ||
|
||
final class IfAndAnalyzer | ||
{ | ||
public function __construct( | ||
private readonly BetterNodeFinder $betterNodeFinder, | ||
private readonly NodeComparator $nodeComparator, | ||
) { | ||
} | ||
|
||
public function isIfAndWithInstanceof(BooleanAnd $booleanAnd): bool | ||
{ | ||
if (! $booleanAnd->left instanceof Instanceof_) { | ||
return false; | ||
} | ||
|
||
// only one instanceof check | ||
return ! $booleanAnd->right instanceof Instanceof_; | ||
} | ||
|
||
public function isIfStmtExprUsedInNextReturn(If_ $if, Return_ $return): bool | ||
{ | ||
if (! $return->expr instanceof Expr) { | ||
return false; | ||
} | ||
|
||
$ifExprs = $this->betterNodeFinder->findInstanceOf($if->stmts, Expr::class); | ||
foreach ($ifExprs as $ifExpr) { | ||
$isExprFoundInReturn = (bool) $this->betterNodeFinder->findFirst( | ||
$return->expr, | ||
fn (Node $node): bool => $this->nodeComparator->areNodesEqual($node, $ifExpr) | ||
); | ||
if ($isExprFoundInReturn) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\EarlyReturn\NodeAnalyzer; | ||
|
||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\Array_; | ||
use PhpParser\Node\Scalar\String_; | ||
|
||
final class SimpleScalarAnalyzer | ||
{ | ||
public function isSimpleScalar(Expr $expr): bool | ||
{ | ||
// empty array | ||
if ($expr instanceof Array_ && $expr->items === []) { | ||
return true; | ||
} | ||
|
||
// empty string | ||
return $expr instanceof String_ && $expr->value === ''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters