Skip to content

[CodeQuality] Move 4 early-return rules to code quality set - #8301

Merged
TomasVotruba merged 1 commit into
mainfrom
move-early-return-rules-to-code-quality
Aug 5, 2026
Merged

[CodeQuality] Move 4 early-return rules to code quality set#8301
TomasVotruba merged 1 commit into
mainfrom
move-early-return-rules-to-code-quality

Conversation

@TomasVotruba

@TomasVotruba TomasVotruba commented Aug 5, 2026

Copy link
Copy Markdown
Member

These 4 rules clean up local code quality rather than enforce the early-return coding style. Move their set registration from early-return to CodeQualityLevel::RULES (right after InlineArrayReturnAssignRector, same "assign then return" family).

Set membership only - the rule classes stay in Rector\EarlyReturn\*, no namespace change.

RemoveAlwaysElseRector:

 class SomeClass
 {
     public function run($value)
     {
         if ($value) {
             throw new \InvalidStateException;
-        } else {
-            return 10;
         }
+
+        return 10;
     }
 }

ChangeIfElseValueAssignToEarlyReturnRector:

 class SomeClass
 {
     public function run()
     {
         if ($this->hasDocBlock($tokens, $index)) {
-            $docToken = $tokens[$this->getDocBlockIndex($tokens, $index)];
-        } else {
-            $docToken = null;
+            return $tokens[$this->getDocBlockIndex($tokens, $index)];
         }

-        return $docToken;
+        return null;
     }
 }

PreparedValueToEarlyReturnRector:

 class SomeClass
 {
     public function run()
     {
-        $var = null;
-
         if (rand(0, 1)) {
-            $var = 1;
+            return 1;
         }

         if (rand(0, 1)) {
-            $var = 2;
+            return 2;
         }

-        return $var;
+        return null;
     }
 }

ReturnEarlyIfVariableRector:

 class SomeClass
 {
     public function run($value)
     {
         if ($value) {
-            $variable = 'yes';
-        } else {
-            $variable = 'no';
+            return 'yes';
         }

-        return $variable;
+        return 'no';
     }
 }

One unrelated hunk: InlineConstructorDefaultToPropertyRector had a pre-existing ECS violation on main (missing space in an arrow fn); composer fix-cs corrected it, so it rides along.

@TomasVotruba
TomasVotruba force-pushed the move-early-return-rules-to-code-quality branch 2 times, most recently from e00c35a to beef2df Compare August 5, 2026 22:41
@TomasVotruba TomasVotruba changed the title [CodeQuality] Move PreparedValueToEarlyReturnRector and ReturnEarlyIfVariableRector to CodeQuality [CodeQuality] Move RemoveAlwaysElseRector, PreparedValueToEarlyReturnRector and ReturnEarlyIfVariableRector to code quality set Aug 5, 2026
RemoveAlwaysElseRector, ChangeIfElseValueAssignToEarlyReturnRector,
PreparedValueToEarlyReturnRector and ReturnEarlyIfVariableRector improve
local code quality rather than enforce the early-return style. Move their
set registration from early-return to CodeQualityLevel. The rule classes
stay in their current namespace.
@TomasVotruba
TomasVotruba force-pushed the move-early-return-rules-to-code-quality branch from beef2df to 1c8f0b7 Compare August 5, 2026 22:47
@TomasVotruba TomasVotruba changed the title [CodeQuality] Move RemoveAlwaysElseRector, PreparedValueToEarlyReturnRector and ReturnEarlyIfVariableRector to code quality set [CodeQuality] Move 4 early-return rules to code quality set Aug 5, 2026
@TomasVotruba
TomasVotruba merged commit 179416a into main Aug 5, 2026
64 checks passed
@TomasVotruba
TomasVotruba deleted the move-early-return-rules-to-code-quality branch August 5, 2026 22:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant