Skip to content

Commit

Permalink
[EarlyReturn] Add ReturnEarlyIfVariableRector
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jul 2, 2022
1 parent 3e66b4a commit e9495e4
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config/set/early-return.php
Expand Up @@ -14,6 +14,7 @@
use Rector\EarlyReturn\Rector\Return_\PreparedValueToEarlyReturnRector;
use Rector\EarlyReturn\Rector\Return_\ReturnBinaryAndToEarlyReturnRector;
use Rector\EarlyReturn\Rector\Return_\ReturnBinaryOrToEarlyReturnRector;
use Rector\EarlyReturn\Rector\StmtsAwareInterface\ReturnEarlyIfVariableRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(ChangeNestedForeachIfsToEarlyContinueRector::class);
Expand All @@ -27,4 +28,5 @@
$rectorConfig->rule(ReturnAfterToEarlyOnBreakRector::class);
$rectorConfig->rule(PreparedValueToEarlyReturnRector::class);
$rectorConfig->rule(ReturnBinaryOrToEarlyReturnRector::class);
$rectorConfig->rule(ReturnEarlyIfVariableRector::class);
};
@@ -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;
}
}

?>
@@ -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';
}
}
@@ -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);
};
@@ -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;
}
}

0 comments on commit e9495e4

Please sign in to comment.