Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[EarlyReturn] Add ReturnEarlyIfVariableRector
- Loading branch information
1 parent
3e66b4a
commit e9495e4
Showing
5 changed files
with
228 additions
and
0 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
35 changes: 35 additions & 0 deletions
35
...yReturn/Rector/StmtsAwareInterface/ReturnEarlyIfVariableRector/Fixture/some_class.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,35 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\EarlyReturn\Rector\StmtsAwareInterface\ReturnEarlyIfVariableRector\Fixture; | ||
|
||
final class SomeClass | ||
{ | ||
public function run($value) | ||
{ | ||
if ($value === 50) { | ||
$value = 100; | ||
} | ||
|
||
return $value; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\EarlyReturn\Rector\StmtsAwareInterface\ReturnEarlyIfVariableRector\Fixture; | ||
|
||
final class SomeClass | ||
{ | ||
public function run($value) | ||
{ | ||
if ($value === 50) { | ||
return 100; | ||
} | ||
|
||
return $value; | ||
} | ||
} | ||
|
||
?> |
33 changes: 33 additions & 0 deletions
33
...ector/StmtsAwareInterface/ReturnEarlyIfVariableRector/ReturnEarlyIfVariableRectorTest.php
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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\EarlyReturn\Rector\StmtsAwareInterface\ReturnEarlyIfVariableRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class ReturnEarlyIfVariableRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(SmartFileInfo $fileInfo): void | ||
{ | ||
$this->doTestFileInfo($fileInfo); | ||
} | ||
|
||
/** | ||
* @return Iterator<SmartFileInfo> | ||
*/ | ||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...yReturn/Rector/StmtsAwareInterface/ReturnEarlyIfVariableRector/config/configured_rule.php
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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\EarlyReturn\Rector\StmtsAwareInterface\ReturnEarlyIfVariableRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(ReturnEarlyIfVariableRector::class); | ||
}; |
148 changes: 148 additions & 0 deletions
148
rules/EarlyReturn/Rector/StmtsAwareInterface/ReturnEarlyIfVariableRector.php
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,148 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\EarlyReturn\Rector\StmtsAwareInterface; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\Assign; | ||
use PhpParser\Node\Expr\Variable; | ||
use PhpParser\Node\Stmt; | ||
use PhpParser\Node\Stmt\Expression; | ||
use PhpParser\Node\Stmt\If_; | ||
use PhpParser\Node\Stmt\Return_; | ||
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\EarlyReturn\Rector\StmtsAwareInterface\ReturnEarlyIfVariableRector\ReturnEarlyIfVariableRectorTest | ||
*/ | ||
final class ReturnEarlyIfVariableRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Replace if conditioned variable override with direct return', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
final class SomeClass | ||
{ | ||
public function run($value) | ||
{ | ||
if ($value === 50) { | ||
$value = 100; | ||
} | ||
return $value; | ||
} | ||
} | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
final class SomeClass | ||
{ | ||
public function run($value) | ||
{ | ||
if ($value === 50) { | ||
return 100; | ||
} | ||
return $value; | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [StmtsAwareInterface::class]; | ||
} | ||
|
||
/** | ||
* @param StmtsAwareInterface $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
$stmts = (array) $node->stmts; | ||
|
||
foreach ($stmts as $key => $stmt) { | ||
$returnVariable = $this->matchNextStmtReturnVariable($node, $key); | ||
if (! $returnVariable instanceof Variable) { | ||
continue; | ||
} | ||
|
||
if ($stmt instanceof If_ && $stmt->else === null && $stmt->elseifs === []) { | ||
// is single condition if | ||
$if = $stmt; | ||
if (count($if->stmts) !== 1) { | ||
continue; | ||
} | ||
|
||
$onlyIfStmt = $if->stmts[0]; | ||
$assignedExpr = $this->matchOnlyIfStmtReturnExpr($onlyIfStmt, $returnVariable); | ||
if (! $assignedExpr instanceof Expr) { | ||
continue; | ||
} | ||
|
||
$if->stmts[0] = new Return_($assignedExpr); | ||
return $node; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private function matchOnlyIfStmtReturnExpr(Stmt $onlyIfStmt, Variable $returnVariable): Expr|null | ||
{ | ||
if (! $onlyIfStmt instanceof Expression) { | ||
return null; | ||
} | ||
|
||
if (! $onlyIfStmt->expr instanceof Assign) { | ||
return null; | ||
} | ||
|
||
$assign = $onlyIfStmt->expr; | ||
|
||
// assign to same variable that is returned | ||
if (! $assign->var instanceof Variable) { | ||
return null; | ||
} | ||
|
||
if (! $this->nodeComparator->areNodesEqual($assign->var, $returnVariable)) { | ||
return null; | ||
} | ||
|
||
// return directly | ||
return $assign->expr; | ||
} | ||
|
||
private function matchNextStmtReturnVariable(StmtsAwareInterface $stmtsAware, int $key): Variable|null | ||
{ | ||
$nextStmt = $stmtsAware->stmts[$key + 1] ?? null; | ||
|
||
// last item → stop | ||
if ($nextStmt === null) { | ||
return null; | ||
} | ||
|
||
if (! $nextStmt instanceof Return_) { | ||
return null; | ||
} | ||
|
||
// next return must be variable | ||
if (! $nextStmt->expr instanceof Variable) { | ||
return null; | ||
} | ||
|
||
return $nextStmt->expr; | ||
} | ||
} |