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

Found 70 rules:

DocblockVarArrayFromGetterReturnRector

Add @var array property docblock from its getter @return

 class SomeClass
 {
+    /**
+     * @var int[]
+     */
     private array $items;

     /**
      * @return int[]
      */
     public function getItems(): array
     {
         return $this->items;
     }
 }

ClassMethodArrayDocblockParamFromLocalCallsRector

Add @param array docblock to a class method based on local call types

 class SomeClass
 {
     public function go()
     {
         $this->run(['item1', 'item2']);
     }

+    /**
+     * @param string[] $items
+     */
     private function run(array $items)
     {
     }
 }

AddReturnArrayDocblockFromDataProviderParamRector

Add @return array return from data provider param type

 use PHPUnit\Framework\TestCase;

 final class SomeClass extends TestCase
 {
     /**
      * @dataProvider provideNames()
      */
     public function test(string $name)
     {
     }

+    /**
+     * @return string[]
+     */
     public function provideNames(): array
     {
         return ['John', 'Jane'];
     }
 }

DocblockVarFromParamDocblockInConstructorRector

Add @var array docblock to a property based on @param of constructor assign

 class SomeClass
 {
+    /**
+     * @var string[]
+     */
     private array $items;

     /**
      * @param string[] $items
      */
     public function __construct(array $items)
     {
         $this->items = $items;
     }
 }

DocblockVarArrayFromPropertyDefaultsRector

Add @var array docblock to array property based on iterable default value

 class SomeClass
 {
+    /**
+     * @var int[]
+     */
     private array $items = [1, 2, 3];
 }

AddReturnDocblockDataProviderRector

Add @return array docblock to array provider method

 use PHPUnit\Framework\TestCase;

 final class SomeTest extends TestCase
 {
     /**
      * @dataProvider provideItems()
      */
     public function testSomething(array $items)
     {
     }

+    /**
+     * @return array<array<string>>
+     */
     public function provideItems()
     {
         return [
             [['item1', 'item2']],
             [['item3', 'item4']],
         ];
     }
 }

AddReturnDocblockForArrayDimAssignedObjectRector

Add @return docblock array of objects, that are dim assigned to returned variable

 final class ItemProvider
 {
+    /**
+     * @return Item[]
+     */
     public function provide(array $input): array
     {
         $items = [];

         foreach ($input as $value) {
             $items[] = new Item($value);
         }

         return $items;
     }
 }

AddReturnDocblockForCommonObjectDenominatorRector

Add @return docblock array of objects, that have common denominator interface/parent class

 final class ExtensionProvider
 {
+    /**
+     * @return ExtensionInterface[]
+     */
     public function getExtensions(): array
     {
         return [
             new FirstExtension(),
             new SecondExtension(),
         ];
     }
 }

 class FirstExtension implements ExtensionInterface
 {
 }

 class SecondExtension implements ExtensionInterface
 {
 }

AddParamArrayDocblockFromDataProviderRector

Add @param docblock array type, based on data provider data type

 use PHPUnit\Framework\TestCase;
 use PHPUnit\Framework\Attributes\DataProvider;

 final class SomeTest extends TestCase
 {
+    /**
+     * @param string[] $names
+     */
     #[DataProvider('provideData')]
     public function test(array $names): void
     {
     }

     public static function provideData()
     {
         yield [['Tom', 'John']];
     }
 }

DocblockReturnArrayFromDirectArrayInstanceRector

Add simple @return array docblock based on direct single level direct return of []

 class SomeClass
 {
+    /**
+     * @return array<string, string>
+     */
     public function getItems(): array
     {
         return [
             'hey' => 'now',
         ];
     }
 }

AddReturnDocblockForJsonArrayRector

Add @return docblock for array based on return of json_decode() return array

 final class SomeClass
 {
+    /**
+     * @return array<string, mixed>
+     */
     public function provide(string $contents): array
     {
         return json_decode($contents, true);
     }
 }

AddParamArrayDocblockFromAssignsParamToParamReferenceRector

Add @param docblock array type, based on type to assigned parameter reference

 final class SomeClass
 {
+    /**
+     * @param string[] $names
+     */
     public function run(array &$names): void
     {
         $names[] = 'John';
     }
 }

DocblockGetterReturnArrayFromPropertyDocblockVarRector

Add @return array docblock to a getter method based on @var of the property

 class SomeClass
 {
     /**
      * @var int[]
      */
     private array $items;

+    /**
+     * @return int[]
+     */
     public function getItems(): array
     {
         return $this->items;
     }
 }

AddParamArrayDocblockBasedOnArrayMapRector

Add @param array docblock if array_map is used on the parameter

 final class SomeClass
 {
+    /**
+     * @param string[] $names
+     */
     public function run(array $names): void
     {
         $names = array_map(fn(string $name) => trim($name), $names);
     }
 }

AddParamArrayDocblockFromDimFetchAccessRector

Add @param docblock array type, based on array dim fetch access

 final class SomeClass
 {
+    /**
+     * @param array<string, mixed> $data
+     */
     public function process(array $data): void
     {
         $item = $data['key'];

         $anotherItem = $data['another_key'];
     }
 }

Configurable

ChangeConstantVisibilityRector

Change visibility of constant from parent class

 class FrameworkClass
 {
     protected const SOME_CONSTANT = 1;
 }

 class MyClass extends FrameworkClass
 {
-    public const SOME_CONSTANT = 1;
+    protected const SOME_CONSTANT = 1;
 }

Configurable

ChangeMethodVisibilityRector

Change visibility of method from parent class

 class FrameworkClass
 {
     protected function someMethod()
     {
     }
 }

 class MyClass extends FrameworkClass
 {
-    public function someMethod()
+    protected function someMethod()
     {
     }
 }

Configurable

AddAssertArrayFromClassMethodDocblockRector

Add key and value assert based on docblock @param type declarations (pick from "webmozart" or "beberlei" asserts)

 <?php

 namespace RectorPrefix202512;

+use RectorPrefix202512\Webmozart\Assert\Assert;
 class SomeClass
 {
     /**
      * @param int[] $items
      */
     public function run(array $items)
     {
+        Assert::allInteger($items);
     }
 }
 \class_alias('SomeClass', 'SomeClass', \false);

Configurable

MethodCallToFuncCallRector

Change method call to function call

 final class SomeClass
 {
     public function show()
     {
-        return $this->render('some_template');
+        return view('some_template');
     }
 }

Configurable

MethodCallToStaticCallRector

Change method call to desired static call

 final class SomeClass
 {
     private $anotherDependency;

     public function __construct(AnotherDependency $anotherDependency)
     {
         $this->anotherDependency = $anotherDependency;
     }

     public function loadConfiguration()
     {
-        return $this->anotherDependency->process('value');
+        return StaticCaller::anotherMethod('value');
     }
 }

Configurable

FuncCallToConstFetchRector

Changes use of function calls to use constants

 class SomeClass
 {
     public function run()
     {
-        $value = php_sapi_name();
+        $value = PHP_SAPI;
     }
 }

Configurable

FuncCallToNewRector

Change configured function calls to new Instance

 class SomeClass
 {
     public function run()
     {
-        $array = collection([]);
+        $array = new \Collection([]);
     }
 }

Configurable

FuncCallToMethodCallRector

Turn defined function calls to local method calls

 class SomeClass
 {
+    /**
+     * @var \Namespaced\SomeRenderer
+     */
+    private $someRenderer;
+
+    public function __construct(\Namespaced\SomeRenderer $someRenderer)
+    {
+        $this->someRenderer = $someRenderer;
+    }
+
     public function run()
     {
-        view('...');
+        $this->someRenderer->view('...');
     }
 }

Configurable

FuncCallToStaticCallRector

Turn defined function call to static method call

-view("...", []);
+SomeClass::render("...", []);

Configurable

StaticCallToFuncCallRector

Turn static call to function call

-OldClass::oldMethod("args");
+new_function("args");

Configurable

StaticCallToMethodCallRector

Change static call to service method via constructor injection

-use Nette\Utils\FileSystem;
+use App\Custom\SmartFileSystem;

 class SomeClass
 {
+    /**
+     * @var SmartFileSystem
+     */
+    private $smartFileSystem;
+
+    public function __construct(SmartFileSystem $smartFileSystem)
+    {
+        $this->smartFileSystem = $smartFileSystem;
+    }
+
     public function run()
     {
-        return FileSystem::write('file', 'content');
+        return $this->smartFileSystem->dumpFile('file', 'content');
     }
 }

Configurable

StaticCallToNewRector

Change static call to new instance

 class SomeClass
 {
     public function run()
     {
-        $dotenv = JsonResponse::create(['foo' => 'bar'], Response::HTTP_OK);
+        $dotenv = new JsonResponse(['foo' => 'bar'], Response::HTTP_OK);
     }
 }

Configurable

AttributeKeyToClassConstFetchRector

Replace key value on specific attribute to class constant

 use Doctrine\ORM\Mapping\Column;
+use Doctrine\DBAL\Types\Types;

 class SomeClass
 {
-    #[Column(type: "string")]
+    #[Column(type: Types::STRING)]
     public $name;
 }

Configurable

ConstFetchToClassConstFetchRector

Change const fetch to class const fetch

-$x = CONTEXT_COURSE
+$x = course::LEVEL

Configurable

StringToClassConstantRector

Change strings to specific constants

 final class SomeSubscriber
 {
     public static function getSubscribedEvents()
     {
-        return ['compiler.post_dump' => 'compile'];
+        return [\Yet\AnotherClass::CONSTANT => 'compile'];
     }
 }

Configurable

ScalarValueToConstFetchRector

Replaces Scalar values with a ConstFetch or ClassConstFetch

-$var = 10;
+$var = \SomeClass::FOOBAR_INT;

Configurable

NewToStaticCallRector

Change new Object to static call

 class SomeClass
 {
     public function run()
     {
-        new Cookie($name);
+        Cookie::create($name);
     }
 }

Configurable

MergeInterfacesRector

Merge old interface to a new one, that already has its methods

-class SomeClass implements SomeInterface, SomeOldInterface
+class SomeClass implements SomeInterface
 {
 }

Configurable

AddInterfaceByTraitRector

Add interface by used trait

-class SomeClass
+class SomeClass implements SomeInterface
 {
     use SomeTrait;
 }

Configurable

WrapReturnRector

Wrap return value of specific method

 final class SomeClass
 {
     public function getItem()
     {
-        return 1;
+        return [1];
     }
 }

Configurable

ArrayDimFetchToMethodCallRector

Change array dim fetch to method call

-$object['key'];
-$object['key'] = 'value';
-isset($object['key']);
-unset($object['key']);
+$object->get('key');
+$object->set('key', 'value');
+$object->has('key');
+$object->unset('key');

Configurable

RemoveMethodCallParamRector

Remove parameter of method call

 final class SomeClass
 {
     public function run(Caller $caller)
     {
-        $caller->process(1, 2);
+        $caller->process(1);
     }
 }

Configurable

FunctionArgumentDefaultValueReplacerRector

Streamline the operator arguments of version_compare function

-version_compare(PHP_VERSION, '5.6', 'gte');
+version_compare(PHP_VERSION, '5.6', 'ge');

Configurable

ReplaceArgumentDefaultValueRector

Replace defined map of arguments in defined methods and their calls

 $someObject = new SomeClass;
-$someObject->someMethod(SomeClass::OLD_CONSTANT);
+$someObject->someMethod(false);

StaticClosureRector

Changes Closure to be static when possible

-function () {
+static function () {
     if (rand(0, 1)) {
         return 1;
     }

     return 2;
 }

ArraySpreadInsteadOfArrayMergeRector

Change array_merge() to spread operator

 class SomeClass
 {
     public function run($iter1, $iter2)
     {
-        $values = array_merge(iterator_to_array($iter1), iterator_to_array($iter2));
+        $values = [...$iter1, ...$iter2];

         // Or to generalize to all iterables
-        $anotherValues = array_merge(
-            is_array($iter1) ? $iter1 : iterator_to_array($iter1),
-            is_array($iter2) ? $iter2 : iterator_to_array($iter2)
-        );
+        $anotherValues = [...$iter1, ...$iter2];
     }
 }

EnumCaseToPascalCaseRector

Convert enum cases to PascalCase and update their usages

 enum Status
 {
-    case PENDING;
-    case published;
-    case IN_REVIEW;
-    case waiting_for_approval;
+    case Pending;
+    case Published;
+    case InReview;
+    case WaitingForApproval;
 }

NestedTernaryToMatchRector

Convert nested ternary expressions to match(true) statements

 class SomeClass
 {
     public function getValue($input)
     {
-        return $input > 100 ? 'more than 100' : ($input > 5 ? 'more than 5' : 'less');
+        return match (true) {
+            $input > 100 => 'more than 100',
+            $input > 5 => 'more than 5',
+            default => 'less',
+        };
     }
 }

StaticArrowFunctionRector

Changes ArrowFunction to be static when possible

-fn (): string => 'test';
+static fn (): string => 'test';

PostIncDecToPreIncDecRector

Use ++$value or --$value instead of $value++ or $value--

 class SomeClass
 {
     public function run($value = 1)
     {
-        $value++; echo $value;
-        $value--; echo $value;
+        ++$value; echo $value;
+        --$value; echo $value;
     }
 }

Configurable

RenameFunctionRector

Turn defined function call new one

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

Configurable

RenameCastRector

Renames casts

-$real = (real) $real;
+$real = (float) $real;

Configurable

RenameStaticMethodRector

Turn method names to new ones

-SomeClass::oldStaticMethod();
+AnotherExampleClass::newStaticMethod();

Configurable

RenamePropertyRector

Replace defined old properties by new ones

-$someObject->someOldProperty;
+$someObject->someNewProperty;

Configurable

RenameConstantRector

Replace constant by new ones

 final class SomeClass
 {
     public function run()
     {
-        return MYSQL_ASSOC;
+        return MYSQLI_ASSOC;
     }
 }

Configurable

RenameStringRector

Change string value

 class SomeClass
 {
     public function run()
     {
-        return 'ROLE_PREVIOUS_ADMIN';
+        return 'IS_IMPERSONATOR';
     }
 }

Configurable

RenameAttributeRector

Rename attribute class names

-#[SimpleRoute()]
+#[BasicRoute()]
 class SomeClass
 {
 }

Configurable

RenameAnnotationRector

Turn defined annotations above properties and methods to their new values

 use PHPUnit\Framework\TestCase;

 final class SomeTest extends TestCase
 {
     /**
-     * @test
+     * @scenario
      */
     public function someMethod()
     {
     }
 }

Configurable

RemoveAnnotationRector

Remove annotation by names

-/**
- * @method getName()
- */
 final class SomeClass
 {
 }

RemoveNextSameValueConditionRector

Remove already checked if condition repeated in the very next stmt

 final class SomeClass
 {
     public function __construct(array $items)
     {
         $count = 100;
         if ($items === []) {
             $count = 0;
-        }
-
-        if ($items === []) {
             return $count;
         }
     }
 }

Configurable

RemoveFuncCallRector

Remove defined function calls

-$x = 'something';
-var_dump($x);
+$x = 'something';

Configurable

RemoveFuncCallArgRector

Remove argument by position by function name

-remove_last_arg(1, 2);
+remove_last_arg(1);

Configurable

RemoveTraitUseRector

Remove specific traits from code

 class SomeClass
 {
-    use SomeTrait;
 }

Configurable

RemoveInterfacesRector

Remove interfaces from class

-class SomeClass implements SomeInterface
+class SomeClass
 {
 }

Configurable

ArgumentRemoverRector

Remove defined arguments in defined methods and their calls

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

FluentSettersToStandaloneCallMethodRector

Change fluent setter chain calls, to standalone line of setters

 class SomeClass
 {
     public function run()
     {
-        return (new SomeFluentClass())
-            ->setName('John')
-            ->setAge(30);
+        $someFluentClass = new SomeFluentClass();
+        $someFluentClass->setName('John');
+        $someFluentClass->setAge(30);
+
+        return $someFluentClass;
     }
 }

RemoveReturnThisFromSetterClassMethodRector

Remove return $this from setter method, to make explicit setter without return value. Goal is to make code unambiguous with one way to set value

 class SomeClass
 {
     private $name;

-    public function setName(string $name): self
+    public function setName(string $name): void
     {
         $this->name = $name;
-        return $this;
     }
 }

DirnameDirConcatStringToDirectStringPathRector

Change dirname() and string concat, to __DIR__ and direct string path

 class SomeClass
 {
     public function run()
     {
-        $path = dirname(__DIR__) . '/vendor/autoload.php';
+        $path = __DIR__ . '/../vendor/autoload.php';
     }
 }

DynamicDocBlockPropertyToNativePropertyRector

Turn dynamic docblock properties on class with no parents to explicit ones

-/**
- * @property SomeDependency $someDependency
- */
-#[\AllowDynamicProperties]
 final class SomeClass
 {
+    private SomeDependency $someDependency;
+
     public function __construct()
     {
         $this->someDependency = new SomeDependency();
     }
 }

Configurable

AddPropertyTypeDeclarationRector

Add type to property by added rules, mostly public/property by parent type

 class SomeClass extends ParentClass
 {
-    public $name;
+    public string $name;
 }

DeclareStrictTypesRector

Add declare(strict_types=1) if missing in a namespaced file

+declare(strict_types=1);
+
 namespace App;

 class SomeClass
 {
     function someFunction(int $number)
     {
     }
 }

Configurable

IncreaseDeclareStrictTypesRector

Add declare strict types to a limited amount of classes at a time, to try out in the wild and increase level gradually

+declare(strict_types=1);
+
 function someFunction()
 {
 }

AddReturnDocblockForScalarArrayFromAssignsRector

Add @return docblock for scalar array from strict array assignments

+/**
+ * @return string[]
+ */
 function getSomeItems()
 {
     $items = [];
     $items[] = 'hey';
     $items[] = 'hello';
     return $items;
 }

AddParamArrayDocblockBasedOnCallableNativeFuncCallRector

Add param array docblock based on callable native function call

+/**
+ * @param stdClass[] $items
+ */
 function process(array $items): void
 {
     array_walk($items, function (stdClass $item) {
         echo $item->value;
     });
 }

AddReturnArrayDocblockBasedOnArrayMapRector

Add @return array docblock based on array_map() return strict type

 class SomeClass
 {
+    /**
+     * @return int[]
+     */
     public function getItems(array $items)
     {
         return array_map(function ($item): int {
             return $item->id;
         }, $items);
     }
 }