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;
     }
 }

Configure your rector.php:

<?php

use Rector\Config\RectorConfig;
use Rector\EarlyReturn\Rector\Return_\PreparedValueToEarlyReturnRector;

return RectorConfig::configure()
    ->withRules([
        PreparedValueToEarlyReturnRector::class,
    ]);
SETS:  Early return