diff --git a/rules-tests/EarlyReturn/Rector/StmtsAwareInterface/ReturnEarlyIfVariableRector/Fixture/mirror_comment.php.inc b/rules-tests/EarlyReturn/Rector/StmtsAwareInterface/ReturnEarlyIfVariableRector/Fixture/mirror_comment.php.inc deleted file mode 100644 index fbe4a9f3d0a..00000000000 --- a/rules-tests/EarlyReturn/Rector/StmtsAwareInterface/ReturnEarlyIfVariableRector/Fixture/mirror_comment.php.inc +++ /dev/null @@ -1,37 +0,0 @@ - ------ - diff --git a/rules-tests/EarlyReturn/Rector/StmtsAwareInterface/ReturnEarlyIfVariableRector/Fixture/skip_global_var.php.inc b/rules-tests/EarlyReturn/Rector/StmtsAwareInterface/ReturnEarlyIfVariableRector/Fixture/skip_global_var.php.inc deleted file mode 100644 index 6d06b51a06b..00000000000 --- a/rules-tests/EarlyReturn/Rector/StmtsAwareInterface/ReturnEarlyIfVariableRector/Fixture/skip_global_var.php.inc +++ /dev/null @@ -1,16 +0,0 @@ - ------ - diff --git a/rules-tests/EarlyReturn/Rector/StmtsAwareInterface/ReturnEarlyIfVariableRector/ReturnEarlyIfVariableRectorTest.php b/rules-tests/EarlyReturn/Rector/StmtsAwareInterface/ReturnEarlyIfVariableRector/ReturnEarlyIfVariableRectorTest.php deleted file mode 100644 index 62c775046b7..00000000000 --- a/rules-tests/EarlyReturn/Rector/StmtsAwareInterface/ReturnEarlyIfVariableRector/ReturnEarlyIfVariableRectorTest.php +++ /dev/null @@ -1,28 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/EarlyReturn/Rector/StmtsAwareInterface/ReturnEarlyIfVariableRector/config/configured_rule.php b/rules-tests/EarlyReturn/Rector/StmtsAwareInterface/ReturnEarlyIfVariableRector/config/configured_rule.php deleted file mode 100644 index 7356bc030fe..00000000000 --- a/rules-tests/EarlyReturn/Rector/StmtsAwareInterface/ReturnEarlyIfVariableRector/config/configured_rule.php +++ /dev/null @@ -1,9 +0,0 @@ -withRules([ReturnEarlyIfVariableRector::class]); diff --git a/rules/EarlyReturn/Rector/StmtsAwareInterface/ReturnEarlyIfVariableRector.php b/rules/EarlyReturn/Rector/StmtsAwareInterface/ReturnEarlyIfVariableRector.php index ce52e6f2222..02970dd8649 100644 --- a/rules/EarlyReturn/Rector/StmtsAwareInterface/ReturnEarlyIfVariableRector.php +++ b/rules/EarlyReturn/Rector/StmtsAwareInterface/ReturnEarlyIfVariableRector.php @@ -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', [ @@ -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 + )); } } diff --git a/src/Config/Level/CodeQualityLevel.php b/src/Config/Level/CodeQualityLevel.php index 56f7b7c01a0..e6368998a57 100644 --- a/src/Config/Level/CodeQualityLevel.php +++ b/src/Config/Level/CodeQualityLevel.php @@ -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; @@ -148,7 +147,6 @@ final class CodeQualityLevel RemoveAlwaysElseRector::class, ChangeIfElseValueAssignToEarlyReturnRector::class, PreparedValueToEarlyReturnRector::class, - ReturnEarlyIfVariableRector::class, InlineIsAInstanceOfRector::class, FlipNegatedTernaryInstanceofRector::class, InlineConstructorDefaultToPropertyRector::class, diff --git a/tests/Issues/IssueRemoveAlwaysElseReturnEarlyIfVariable/Fixture/fixture.php.inc b/tests/Issues/IssueRemoveAlwaysElseReturnEarlyIfVariable/Fixture/fixture.php.inc deleted file mode 100644 index eda3489ffc1..00000000000 --- a/tests/Issues/IssueRemoveAlwaysElseReturnEarlyIfVariable/Fixture/fixture.php.inc +++ /dev/null @@ -1,50 +0,0 @@ - ------ - diff --git a/tests/Issues/IssueRemoveAlwaysElseReturnEarlyIfVariable/IssueRemoveAlwaysElseReturnEarlyIfVariableTest.php b/tests/Issues/IssueRemoveAlwaysElseReturnEarlyIfVariable/IssueRemoveAlwaysElseReturnEarlyIfVariableTest.php deleted file mode 100644 index 77cb43a0d73..00000000000 --- a/tests/Issues/IssueRemoveAlwaysElseReturnEarlyIfVariable/IssueRemoveAlwaysElseReturnEarlyIfVariableTest.php +++ /dev/null @@ -1,28 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/tests/Issues/IssueRemoveAlwaysElseReturnEarlyIfVariable/config/configured_rule.php b/tests/Issues/IssueRemoveAlwaysElseReturnEarlyIfVariable/config/configured_rule.php deleted file mode 100644 index 3a2bcf62ef8..00000000000 --- a/tests/Issues/IssueRemoveAlwaysElseReturnEarlyIfVariable/config/configured_rule.php +++ /dev/null @@ -1,10 +0,0 @@ -withRules([RemoveAlwaysElseRector::class, ReturnEarlyIfVariableRector::class]);