Find the best Rector rule to solve your problem


MyCLabsMethodCallToEnumConstRector

Refactor MyCLabs enum fetch to Enum const

-$name = SomeEnum::VALUE()->getKey();
+$name = SomeEnum::VALUE;
SETS:  PHP 8.1

SpatieEnumMethodCallToEnumConstRector

Refactor Spatie enum method calls

-$value1 = SomeEnum::SOME_CONSTANT()->getValue();
-$value2 = SomeEnum::SOME_CONSTANT()->value;
-$name1 = SomeEnum::SOME_CONSTANT()->getName();
-$name2 = SomeEnum::SOME_CONSTANT()->name;
+$value1 = SomeEnum::SOME_CONSTANT->value;
+$value2 = SomeEnum::SOME_CONSTANT->value;
+$name1 = SomeEnum::SOME_CONSTANT->name;
+$name2 = SomeEnum::SOME_CONSTANT->name;
SETS:  PHP 8.1

NullToStrictStringFuncCallArgRector

Change null to strict string defined function call args

 class SomeClass
 {
     public function run()
     {
-        preg_split("#a#", null);
+        preg_split("#a#", '');
     }
 }
SETS:  PHP 8.1

ReadOnlyPropertyRector

Decorate read-only property with readonly attribute

 class SomeClass
 {
     public function __construct(
-        private string $name
+        private readonly string $name
     ) {
     }

     public function getName()
     {
         return $this->name;
     }
 }
SETS:  PHP 8.1

FirstClassCallableRector

Upgrade array callable to first class callable

 final class SomeClass
 {
     public function run()
     {
-        $name = [$this, 'name'];
+        $name = $this->name(...);
     }

     public function name()
     {
     }
 }
SETS:  PHP 8.1

Configurable

SpatieEnumClassToEnumRector

Refactor Spatie enum class to native Enum

-use \Spatie\Enum\Enum;
-
-/**
- * @method static self draft()
- * @method static self published()
- * @method static self archived()
- */
-class StatusEnum extends Enum
+enum StatusEnum : string
 {
+    case DRAFT = 'draft';
+    case PUBLISHED = 'published';
+    case ARCHIVED = 'archived';
 }
SETS:  PHP 8.1

MyCLabsClassToEnumRector

Refactor MyCLabs enum class to native Enum

-use MyCLabs\Enum\Enum;
-
-final class Action extends Enum
+enum Action : string
 {
-    private const VIEW = 'view';
-    private const EDIT = 'edit';
+    case VIEW = 'view';
+    case EDIT = 'edit';
 }
SETS:  PHP 8.1

NewInInitializerRector

Replace property declaration of new state with direct new

 class SomeClass
 {
-    private Logger $logger;
-
     public function __construct(
-        ?Logger $logger = null,
+        private Logger $logger = new NullLogger,
     ) {
-        $this->logger = $logger ?? new NullLogger;
     }
 }
SETS:  PHP 8.1

ReturnNeverTypeRector

Add "never" return-type for methods that never return anything

 final class SomeClass
 {
-    public function run()
+    public function run(): never
     {
         throw new InvalidException();
     }
 }