Find the best Rector rule to solve your problem. Searching through 637 rules.

Found 8 rules:

PreparedValueToEarlyReturnRector

Return early prepared value in ifs

 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;
     }
 }
SETS:  Early return

ReturnBinaryOrToEarlyReturnRector

Changes Single return of || to early returns

 class SomeClass
 {
     public function accept()
     {
-        return $this->something() || $this->somethingElse();
+        if ($this->something()) {
+            return true;
+        }
+        return (bool) $this->somethingElse();
     }
 }
SETS:  Early return

ReturnEarlyIfVariableRector

Replace if conditioned variable override with direct return

 final class SomeClass
 {
     public function run($value)
     {
         if ($value === 50) {
-            $value = 100;
+            return 100;
         }

         return $value;
     }
 }
SETS:  Early return

ChangeNestedIfsToEarlyReturnRector

Change nested ifs to early return

 class SomeClass
 {
     public function run()
     {
-        if ($value === 5) {
-            if ($value2 === 10) {
-                return 'yes';
-            }
+        if ($value !== 5) {
+            return 'no';
+        }
+
+        if ($value2 === 10) {
+            return 'yes';
         }

         return 'no';
     }
 }
SETS:  Early return

RemoveAlwaysElseRector

Split if statement, when if condition always break execution flow

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

ChangeIfElseValueAssignToEarlyReturnRector

Change if/else value to early return

 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;
     }
 }
SETS:  Early return

ChangeOrIfContinueToMultiContinueRector

Changes if || to early return

 class SomeClass
 {
     public function canDrive(Car $newCar)
     {
         foreach ($cars as $car) {
-            if ($car->hasWheels() || $car->hasFuel()) {
+            if ($car->hasWheels()) {
+                continue;
+            }
+            if ($car->hasFuel()) {
                 continue;
             }

             $car->setWheel($newCar->wheel);
             $car->setFuel($newCar->fuel);
         }
     }
 }
SETS:  Early return

ChangeNestedForeachIfsToEarlyContinueRector

Change nested ifs to foreach with continue

 class SomeClass
 {
     public function run()
     {
         $items = [];

         foreach ($values as $value) {
-            if ($value === 5) {
-                if ($value2 === 10) {
-                    $items[] = 'maybe';
-                }
+            if ($value !== 5) {
+                continue;
             }
+            if ($value2 !== 10) {
+                continue;
+            }
+
+            $items[] = 'maybe';
         }
     }
 }
SETS:  Early return