Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,18 @@
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\Else_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\NodeAnalyzer\VariableAnalyzer;
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\PhpParser\Enum\NodeGroup;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
use Rector\RuleDoc\RuleDefinition;

/**
* @see \Rector\Tests\EarlyReturn\Rector\StmtsAwareInterface\ReturnEarlyIfVariableRector\ReturnEarlyIfVariableRectorTest
* @deprecated This rule is deprecated, as too niche and can reduce readability by moving a value out of its logical place, while blocking a later refactoring to match.
*/
final class ReturnEarlyIfVariableRector extends AbstractRector
final class ReturnEarlyIfVariableRector extends AbstractRector implements DeprecatedInterface
{
public function __construct(
private readonly VariableAnalyzer $variableAnalyzer,
private readonly PhpDocInfoFactory $phpDocInfoFactory
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Replace if conditioned variable override with direct return', [
Expand Down Expand Up @@ -76,101 +61,11 @@ public function getNodeTypes(): array
return NodeGroup::STMTS_AWARE;
}

/**
* @param StmtsAware $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 instanceof Else_ && $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);
$this->mirrorComments($if->stmts[0], $onlyIfStmt);

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->variableAnalyzer->isStaticOrGlobal($assign->var)) {
return null;
}

if ($this->variableAnalyzer->isUsedByReference($assign->var)) {
return null;
}

if (! $this->nodeComparator->areNodesEqual($assign->var, $returnVariable)) {
return null;
}

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($onlyIfStmt);
if ($phpDocInfo->getVarTagValueNode() instanceof VarTagValueNode) {
return null;
}

// return directly
return $assign->expr;
}

/**
* @param StmtsAware $stmtsAware
*/
private function matchNextStmtReturnVariable(Node $stmtsAware, int $key): Variable|null
{
$nextStmt = $stmtsAware->stmts[$key + 1] ?? null;

// last item → stop
if (! $nextStmt instanceof Stmt) {
return null;
}

if (! $nextStmt instanceof Return_) {
return null;
}

// next return must be variable
if (! $nextStmt->expr instanceof Variable) {
return null;
}

return $nextStmt->expr;
throw new ShouldNotHappenException(sprintf(
'"%s" rule is deprecated, as too niche and can reduce readability',
self::class
));
}
}
2 changes: 0 additions & 2 deletions src/Config/Level/CodeQualityLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
use Rector\EarlyReturn\Rector\If_\ChangeIfElseValueAssignToEarlyReturnRector;
use Rector\EarlyReturn\Rector\If_\RemoveAlwaysElseRector;
use Rector\EarlyReturn\Rector\Return_\PreparedValueToEarlyReturnRector;
use Rector\EarlyReturn\Rector\StmtsAwareInterface\ReturnEarlyIfVariableRector;
use Rector\Instanceof_\Rector\Ternary\FlipNegatedTernaryInstanceofRector;
use Rector\Php52\Rector\Property\VarToPublicPropertyRector;
use Rector\Php71\Rector\FuncCall\RemoveExtraParametersRector;
Expand Down Expand Up @@ -148,7 +147,6 @@ final class CodeQualityLevel
RemoveAlwaysElseRector::class,
ChangeIfElseValueAssignToEarlyReturnRector::class,
PreparedValueToEarlyReturnRector::class,
ReturnEarlyIfVariableRector::class,
InlineIsAInstanceOfRector::class,
FlipNegatedTernaryInstanceofRector::class,
InlineConstructorDefaultToPropertyRector::class,
Expand Down

This file was deleted.

Loading
Loading