Find the best Rector rule to solve your problem


AddReturnTypeDeclarationFromYieldsRector

Add return type declarations from yields

 class SomeClass
 {
-    public function provide()
+    /**
+     * @return Iterator<int>
+     */
+    public function provide(): Iterator
     {
         yield 1;
     }
 }

AddParamTypeSplFixedArrayRector

Add exact fixed array type in known cases

+use PhpCsFixer\Tokenizer\Token;
 use PhpCsFixer\Tokenizer\Tokens;

 class SomeClass
 {
+    /**
+     * @param Tokens<Token>
+     */
     public function run(Tokens $tokens)
     {
     }
 }

AddClosureVoidReturnTypeWhereNoReturnRector

Add closure return type void if there is no return

-function () {
+function (): void {
 };

ClosureReturnTypeRector

Add return type to closures based on known return values

-function () {
+function (): int {
     return 100;
 };

AddClosureNeverReturnTypeRector

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

-function () {
+function (): never {
     throw new InvalidException();
 }

AddFunctionVoidReturnTypeWhereNoReturnRector

Add function return type void if there is no return

-function restore() {
+function restore(): void {
 }

TypedPropertyFromStrictConstructorRector

Add typed properties based only on strict constructor types

 class SomeObject
 {
-    private $name;
+    private string $name;

     public function __construct(string $name)
     {
         $this->name = $name;
     }
 }

Configurable

TypedPropertyFromAssignsRector

Add typed property from assigned types

 final class SomeClass
 {
-    private $name;
+    private string|null $name = null;

     public function run()
     {
         $this->name = 'string';
     }
 }

TypedPropertyFromStrictSetUpRector

Add strict typed property based on setUp() strict typed assigns in TestCase

 use PHPUnit\Framework\TestCase;

 final class SomeClass extends TestCase
 {
-    private $value;
+    private int $value;

     public function setUp()
     {
         $this->value = 1000;
     }
 }

AddArrowFunctionReturnTypeRector

Add known return type to arrow function

-fn () => [];
+fn (): array => [];

EmptyOnNullableObjectToInstanceOfRector

Change empty() on nullable object to instanceof check

 class SomeClass
 {
     public function run(?AnotherObject $anotherObject)
     {
-        if (empty($anotherObject)) {
+        if (! $anotherObject instanceof AnotherObject) {
             return false;
         }

         return true;
     }
 }

ReturnTypeFromStrictTernaryRector

Add method return type based on strict ternary values

 final class SomeClass
 {
-    public function getValue($number)
+    public function getValue($number): int
     {
         return $number ? 100 : 500;
     }
 }

TypedPropertyFromJMSSerializerAttributeTypeRector

Add typed property from JMS Serializer Type attribute

 final class SomeClass
 {
     #[\JMS\Serializer\Annotation\Type('string')]
-    private $name;
+    private ?string $name = null;
 }

AddTestsVoidReturnTypeWhereNoReturnRector

Add void to PHPUnit test methods

 use PHPUnit\Framework\TestCase;

 class SomeClass extends TestCase
 {
-    public function testSomething()
+    public function testSomething(): void
     {
     }
 }

MergeDateTimePropertyTypeDeclarationRector

Set DateTime to DateTimeInterface for DateTime property with DateTimeInterface docblock

 final class SomeClass
 {
-    /**
-     * @var DateTimeInterface
-     */
-    private DateTime $dateTime;
+    private DateTimeInterface $dateTime;
 }

ChildDoctrineRepositoryClassTypeRector

Add return type to classes that extend Doctrine\ORM\EntityRepository based on return Doctrine method names

 use Doctrine\ORM\EntityRepository;

 /**
  * @extends EntityRepository<SomeType>
  */
 final class SomeRepository extends EntityRepository
 {
-    public function getActiveItem()
+    public function getActiveItem(): ?SomeType
     {
         return $this->findOneBy([
             'something'
         ]);
     }
 }

PropertyTypeFromStrictSetterGetterRector

Add property type based on strict setter and getter method

 final class SomeClass
 {
-    private $name = 'John';
+    private string $name = 'John';

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

     public function getName(): string
     {
         return $this->name;
     }
 }

TypedPropertyFromCreateMockAssignRector

Add typed property from assigned mock

 use PHPUnit\Framework\TestCase;

 final class SomeTest extends TestCase
 {
-    private $someProperty;
+    private \PHPUnit\Framework\MockObject\MockObject $someProperty;

     protected function setUp(): void
     {
         $this->someProperty = $this->createMock(SomeMockedClass::class);
     }
 }

AddParamTypeFromPropertyTypeRector

Adds param type declaration based on property type the value is assigned to PHPUnit provider return type declaration

 final class SomeClass
 {
     private string $name;

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

ParamTypeByMethodCallTypeRector

Change param type based on passed method call type

 class SomeTypedService
 {
     public function run(string $name)
     {
     }
 }

 final class UseDependency
 {
     public function __construct(
         private SomeTypedService $someTypedService
     ) {
     }

-    public function go($value)
+    public function go(string $value)
     {
         $this->someTypedService->run($value);
     }
 }

ReturnTypeFromStrictNewArrayRector

Add strict return array type based on created empty array and returned

 final class SomeClass
 {
-    public function run()
+    public function run(): array
     {
         $values = [];

         return $values;
     }
 }

ReturnTypeFromReturnCastRector

Add return type to function like with return cast

 final class SomeClass
 {
-    public function action($param)
+    public function action($param): array
     {
         try {
             return (array) $param;
         } catch (Exception $exception) {
             // some logging
             throw $exception;
         }
     }
 }

ReturnTypeFromStrictNativeCallRector

Add strict return type based native function or native method

 final class SomeClass
 {
-    public function run()
+    public function run(): int
     {
         return strlen('value');
     }
 }

ReturnTypeFromReturnNewRector

Add return type to function like with return new

 final class SomeClass
 {
-    public function create()
+    public function create(): Project
     {
         return new Project();
     }
 }

StrictArrayParamDimFetchRector

Add array type based on array dim fetch use

 class SomeClass
 {
-    public function resolve($item)
+    public function resolve(array $item)
     {
         return $item['name'];
     }
 }

NumericReturnTypeFromStrictReturnsRector

Add int/float return type based on strict typed returns

 class SomeClass
 {
-    public function increase($value)
+    public function increase($value): int
     {
         return ++$value;
     }
 }

AddVoidReturnTypeWhereNoReturnRector

Add return type void to function like without any return

 final class SomeClass
 {
-    public function getValues()
+    public function getValues(): void
     {
         $value = 1000;
         return;
     }
 }

StrictStringParamConcatRector

Add string type based on concat use

 class SomeClass
 {
-    public function resolve($item)
+    public function resolve(string $item)
     {
         return $item . ' world';
     }
 }

BoolReturnTypeFromBooleanConstReturnsRector

Add return bool, based on direct true/false returns

 class SomeClass
 {
-    public function resolve($value)
+    public function resolve($value): bool
     {
         if ($value) {
             return false;
         }

         return true;
     }
 }

NumericReturnTypeFromStrictScalarReturnsRector

Add int/float return type based on strict scalar returns type

 class SomeClass
 {
-    public function getNumber()
+    public function getNumber(): int
     {
         return 200;
     }
 }

BoolReturnTypeFromBooleanStrictReturnsRector

Add bool return type based on strict bool returns type operations

 class SomeClass
 {
-    public function resolve($first, $second)
+    public function resolve($first, $second): bool
     {
         return $first > $second;
     }
 }

ReturnTypeFromReturnDirectArrayRector

Add return type from return direct array

 final class AddReturnArray
 {
-    public function getArray()
+    public function getArray(): array
     {
         return [1, 2, 3];
     }
 }

ReturnTypeFromStrictTypedPropertyRector

Add return method return type based on strict typed property

 final class SomeClass
 {
     private int $age = 100;

-    public function getAge()
+    public function getAge(): int
     {
         return $this->age;
     }
 }

AddReturnTypeDeclarationBasedOnParentClassMethodRector

Add missing return type declaration based on parent class method

 class A
 {
     public function execute(): int
     {
     }
 }

 class B extends A{
-    public function execute()
+    public function execute(): int
     {
     }
 }

ReturnUnionTypeRector

Add union return type

 final class SomeClass
 {
-    public function getData()
+    public function getData(): null|\DateTime|\stdClass
     {
         if (rand(0, 1)) {
             return null;
         }

         if (rand(0, 1)) {
             return new DateTime('now');
         }

         return new stdClass;
     }
 }

ReturnNullableTypeRector

Add basic ? nullable type to class methods and functions, as of PHP 7.1

 final class SomeClass
 {
-    public function getData()
+    public function getData(): ?int
     {
         if (rand(0, 1)) {
             return null;
         }

         return 100;
     }
 }

ReturnTypeFromStrictFluentReturnRector

Add return type from strict return $this

 final class SomeClass
 {
-    public function run()
+    public function run(): self
     {
         return $this;
     }
 }

ReturnNeverTypeRector

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

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

ReturnTypeFromStrictConstantReturnRector

Add strict type declaration based on returned constants

 class SomeClass
 {
     public const NAME = 'name';

-    public function run()
+    public function run(): string
     {
         return self::NAME;
     }
 }

ReturnTypeFromSymfonySerializerRector

Add return type from symfony serializer

 final class SomeClass
 {
     private \Symfony\Component\Serializer\Serializer $serializer;

-    public function resolveEntity($data)
+    public function resolveEntity($data): SomeType
     {
         return $this->serializer->deserialize($data, SomeType::class, 'json');
     }
 }

ReturnTypeFromStrictTypedCallRector

Add return type from strict return type of call

 final class SomeClass
 {
-    public function getData()
+    public function getData(): int
     {
         return $this->getNumber();
     }

     private function getNumber(): int
     {
         return 1000;
     }
 }

ReturnTypeFromStrictParamRector

Add return type based on strict parameter type

 class SomeClass
 {
-    public function resolve(ParamType $item)
+    public function resolve(ParamType $item): ParamType
     {
         return $item;
     }
 }

StringReturnTypeFromStrictStringReturnsRector

Add string return type based on returned strict string values

 final class SomeClass
 {
-    public function foo($condition, $value)
+    public function foo($condition, $value): string;
     {
         if ($value) {
             return 'yes';
         }

         return strtoupper($value);
     }
 }

AddMethodCallBasedStrictParamTypeRector

Change private method param type to strict type, based on passed strict types

 final class SomeClass
 {
     public function run(int $value)
     {
         $this->resolve($value);
     }

-    private function resolve($value)
+    private function resolve(int $value)
     {
     }
 }

ReturnTypeFromMockObjectRector

Add known property and return MockObject types

 class SomeTest extends TestCase
 {
-    public function createSomeMock()
+    public function createSomeMock(): \PHPUnit\Framework\MockObject\MockObject
     {
         $someMock = $this->createMock(SomeClass::class);
         return $someMock;
     }
 }

StringReturnTypeFromStrictScalarReturnsRector

Add string return type based on returned string scalar values

 final class SomeClass
 {
-    public function foo($condition)
+    public function foo($condition): string
     {
         if ($condition) {
             return 'yes';
         }

         return 'no';
     }
 }

ParamTypeByParentCallTypeRector

Change param type based on parent param type

 class SomeControl
 {
     public function __construct(string $name)
     {
     }
 }

 class VideoControl extends SomeControl
 {
-    public function __construct($name)
+    public function __construct(string $name)
     {
         parent::__construct($name);
     }
 }

AddParamTypeBasedOnPHPUnitDataProviderRector

Adds param type declaration based on PHPUnit provider return type declaration

 use PHPUnit\Framework\TestCase;

 final class SomeTest extends TestCase
 {
     /**
      * @dataProvider provideData()
      */
-    public function test($value)
+    public function test(string $value)
     {
     }

     public static function provideData()
     {
         yield ['name'];
     }
 }

ResponseReturnTypeControllerActionRector

Add Response object return type to controller actions

 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
+use Symfony\Component\HttpFoundation\Response;
 use Symfony\Component\Routing\Annotation\Route;

 final class SomeController extends AbstractController
 {
     #[Route]
-    public function detail()
+    public function detail(): Response
     {
         return $this->render('some_template');
     }
 }