Find the best Rector rule to solve your problem
Add return type declarations from yields
class SomeClass
{
- public function provide()
+ /**
+ * @return Iterator<int>
+ */
+ public function provide(): Iterator
{
yield 1;
}
}
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)
{
}
}
Add closure return type void if there is no return
-function () {
+function (): void {
};
Add return type to closures based on known return values
-function () {
+function (): int {
return 100;
};
Add "never" return-type for closure that never return anything
-function () {
+function (): never {
throw new InvalidException();
}
Add function return type void if there is no return
-function restore() {
+function restore(): void {
}
Add typed properties based only on strict constructor types
class SomeObject
{
- private $name;
+ private string $name;
public function __construct(string $name)
{
$this->name = $name;
}
}
Add typed property from assigned types
final class SomeClass
{
- private $name;
+ private string|null $name = null;
public function run()
{
$this->name = 'string';
}
}
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;
}
}
Add known return type to arrow function
-fn () => [];
+fn (): array => [];
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;
}
}
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;
}
}
Add typed property from JMS Serializer Type attribute
final class SomeClass
{
#[\JMS\Serializer\Annotation\Type('string')]
- private $name;
+ private ?string $name = null;
}
Add void to PHPUnit test methods
use PHPUnit\Framework\TestCase;
class SomeClass extends TestCase
{
- public function testSomething()
+ public function testSomething(): void
{
}
}
Set DateTime to DateTimeInterface for DateTime property with DateTimeInterface docblock
final class SomeClass
{
- /**
- * @var DateTimeInterface
- */
- private DateTime $dateTime;
+ private DateTimeInterface $dateTime;
}
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'
]);
}
}
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;
}
}
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);
}
}
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;
}
}
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);
}
}
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;
}
}
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;
}
}
}
Add strict return type based native function or native method
final class SomeClass
{
- public function run()
+ public function run(): int
{
return strlen('value');
}
}
Add return type to function like with return new
final class SomeClass
{
- public function create()
+ public function create(): Project
{
return new Project();
}
}
Add array type based on array dim fetch use
class SomeClass
{
- public function resolve($item)
+ public function resolve(array $item)
{
return $item['name'];
}
}
Add int/float return type based on strict typed returns
class SomeClass
{
- public function increase($value)
+ public function increase($value): int
{
return ++$value;
}
}
Add return type void to function like without any return
final class SomeClass
{
- public function getValues()
+ public function getValues(): void
{
$value = 1000;
return;
}
}
Add string type based on concat use
class SomeClass
{
- public function resolve($item)
+ public function resolve(string $item)
{
return $item . ' world';
}
}
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;
}
}
Add int/float return type based on strict scalar returns type
class SomeClass
{
- public function getNumber()
+ public function getNumber(): int
{
return 200;
}
}
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;
}
}
Add return type from return direct array
final class AddReturnArray
{
- public function getArray()
+ public function getArray(): array
{
return [1, 2, 3];
}
}
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;
}
}
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
{
}
}
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;
}
}
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;
}
}
Add return type from strict return $this
final class SomeClass
{
- public function run()
+ public function run(): self
{
return $this;
}
}
Add "never" return-type for methods that never return anything
final class SomeClass
{
- public function run()
+ public function run(): never
{
throw new InvalidException();
}
}
Add strict type declaration based on returned constants
class SomeClass
{
public const NAME = 'name';
- public function run()
+ public function run(): string
{
return self::NAME;
}
}
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');
}
}
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;
}
}
Add return type based on strict parameter type
class SomeClass
{
- public function resolve(ParamType $item)
+ public function resolve(ParamType $item): ParamType
{
return $item;
}
}
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);
}
}
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)
{
}
}
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;
}
}
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';
}
}
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);
}
}
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'];
}
}
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');
}
}