Find the best Rector rule to solve your problem


Configurable

StaticCallToFuncCallRector

Turns static call to function call.

-OldClass::oldMethod("args");
+new_function("args");
SETS:  PHP 8.0

Configurable

FunctionArgumentDefaultValueReplacerRector

Streamline the operator arguments of version_compare function

-version_compare(PHP_VERSION, '5.6', 'gte');
+version_compare(PHP_VERSION, '5.6', 'ge');
SETS:  PHP 8.0

Configurable

ArgumentAdderRector

This Rector adds new default arguments in calls of defined methods and class types.

 $someObject = new SomeExampleClass;
-$someObject->someMethod();
+$someObject->someMethod(true);

 class MyCustomClass extends SomeExampleClass
 {
-    public function someMethod()
+    public function someMethod($value = true)
     {
     }
 }

ConsistentImplodeRector

Changes various implode forms to consistent one

 class SomeClass
 {
     public function run(array $items)
     {
-        $itemsAsStrings = implode($items);
-        $itemsAsStrings = implode($items, '|');
+        $itemsAsStrings = implode('', $items);
+        $itemsAsStrings = implode('|', $items);
     }
 }

Configurable

RenameFunctionRector

Turns defined function call new one.

-view("...", []);
+Laravel\Templating\render("...", []);

RemoveParentCallWithoutParentRector

Remove unused parent call with no parent class

 class OrphanClass
 {
     public function __construct()
     {
-         parent::__construct();
     }
 }
SETS:  PHP 8.0 Dead Code

ClassOnObjectRector

Change get_class($object) to faster $object::class

 class SomeClass
 {
     public function run($object)
     {
-        return get_class($object);
+        return $object::class;
     }
 }
SETS:  PHP 8.0

ClassOnThisVariableObjectRector

Change $this::class to static::class or self::class depends on class modifier

 class SomeClass
 {
     public function run()
     {
-        return $this::class;
+        return static::class;
     }
 }
SETS:  PHP 8.0

RemoveUnusedVariableInCatchRector

Remove unused variable in catch()

 final class SomeClass
 {
     public function run()
     {
         try {
-        } catch (Throwable $notUsedThrowable) {
+        } catch (Throwable) {
         }
     }
 }
SETS:  PHP 8.0

StrEndsWithRector

Change helper functions to str_ends_with()

 class SomeClass
 {
     public function run()
     {
-        $isMatch = substr($haystack, -strlen($needle)) === $needle;
+        $isMatch = str_ends_with($haystack, $needle);

-        $isNotMatch = substr($haystack, -strlen($needle)) !== $needle;
+        $isNotMatch = !str_ends_with($haystack, $needle);
     }
 }
SETS:  PHP 8.0 Polyfills

StrStartsWithRector

Change helper functions to str_starts_with()

 class SomeClass
 {
     public function run()
     {
-        $isMatch = substr($haystack, 0, strlen($needle)) === $needle;
+        $isMatch = str_starts_with($haystack, $needle);

-        $isNotMatch = substr($haystack, 0, strlen($needle)) !== $needle;
+        $isNotMatch = ! str_starts_with($haystack, $needle);
     }
 }
SETS:  PHP 8.0 Polyfills

StrContainsRector

Replace strpos() !== false and strstr() with str_contains()

 class SomeClass
 {
     public function run()
     {
-        return strpos('abc', 'a') !== false;
+        return str_contains('abc', 'a');
     }
 }
SETS:  PHP 8.0 Polyfills

GetDebugTypeRector

Change ternary type resolve to get_debug_type()

 class SomeClass
 {
     public function run($value)
     {
-        return is_object($value) ? get_class($value) : gettype($value);
+        return get_debug_type($value);
     }
 }
SETS:  PHP 8.0 Polyfills

ChangeSwitchToMatchRector

Change switch() to match()

-switch ($input) {
-    case Lexer::T_SELECT:
-        $statement = 'select';
-        break;
-    case Lexer::T_UPDATE:
-        $statement = 'update';
-        break;
-    default:
-        $statement = 'error';
-}
+$statement = match ($input) {
+    Lexer::T_SELECT => 'select',
+    Lexer::T_UPDATE => 'update',
+    default => 'error',
+};
SETS:  PHP 8.0

StringableForToStringRector

Add Stringable interface to classes with __toString() method

-class SomeClass
+class SomeClass implements Stringable
 {
-    public function __toString()
+    public function __toString(): string
     {
         return 'I can stringz';
     }
 }
SETS:  PHP 8.0

Configurable

ClassPropertyAssignToConstructorPromotionRector

Change simple property init and assign to constructor promotion

 class SomeClass
 {
-    public float $price;
-
     public function __construct(
-        float $price = 0.0
+        public float $price = 0.0
     ) {
-        $this->price = $price;
     }
 }
SETS:  PHP 8.0

FinalPrivateToPrivateVisibilityRector

Changes method visibility from final private to only private

 class SomeClass
 {
-    final private function getter() {
+    private function getter() {
         return $this;
     }
 }
SETS:  PHP 8.0

SetStateToStaticRector

Adds static visibility to __set_state() methods

 class SomeClass
 {
-    public function __set_state($properties) {
+    public static function __set_state($properties) {

     }
 }
SETS:  PHP 8.0

AddParamBasedOnParentClassMethodRector

Add missing parameter based on parent class method

 class A
 {
     public function execute($foo)
     {
     }
 }

 class B extends A{
-    public function execute()
+    public function execute($foo)
     {
     }
 }
SETS:  PHP 8.0

OptionalParametersAfterRequiredRector

Move required parameters after optional ones

 class SomeObject
 {
-    public function run($optional = 1, $required)
+    public function run($required, $optional = 1)
     {
     }
 }