Find the best Rector rule to solve your problem. Searching through 788 rules.
Found 70 rules:
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;
}
}
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)
{
}
}
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'];
}
}
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;
}
}
Add @var array docblock to array property based on iterable default value
class SomeClass
{
+ /**
+ * @var int[]
+ */
private array $items = [1, 2, 3];
}
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']],
];
}
}
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;
}
}
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
{
}
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']];
}
}
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',
];
}
}
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);
}
}
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';
}
}
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;
}
}
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);
}
}
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'];
}
}
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;
}
Change visibility of method from parent class
class FrameworkClass
{
protected function someMethod()
{
}
}
class MyClass extends FrameworkClass
{
- public function someMethod()
+ protected function someMethod()
{
}
}
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);
Change method call to function call
final class SomeClass
{
public function show()
{
- return $this->render('some_template');
+ return view('some_template');
}
}
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');
}
}
Changes use of function calls to use constants
class SomeClass
{
public function run()
{
- $value = php_sapi_name();
+ $value = PHP_SAPI;
}
}
Change configured function calls to new Instance
class SomeClass
{
public function run()
{
- $array = collection([]);
+ $array = new \Collection([]);
}
}
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('...');
}
}
Turn defined function call to static method call
-view("...", []);
+SomeClass::render("...", []);
Turn static call to function call
-OldClass::oldMethod("args");
+new_function("args");
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');
}
}
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);
}
}
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;
}
Change const fetch to class const fetch
-$x = CONTEXT_COURSE
+$x = course::LEVEL
Change strings to specific constants
final class SomeSubscriber
{
public static function getSubscribedEvents()
{
- return ['compiler.post_dump' => 'compile'];
+ return [\Yet\AnotherClass::CONSTANT => 'compile'];
}
}
Replaces Scalar values with a ConstFetch or ClassConstFetch
-$var = 10;
+$var = \SomeClass::FOOBAR_INT;
Change new Object to static call
class SomeClass
{
public function run()
{
- new Cookie($name);
+ Cookie::create($name);
}
}
Merge old interface to a new one, that already has its methods
-class SomeClass implements SomeInterface, SomeOldInterface
+class SomeClass implements SomeInterface
{
}
Add interface by used trait
-class SomeClass
+class SomeClass implements SomeInterface
{
use SomeTrait;
}
Wrap return value of specific method
final class SomeClass
{
public function getItem()
{
- return 1;
+ return [1];
}
}
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');
Remove parameter of method call
final class SomeClass
{
public function run(Caller $caller)
{
- $caller->process(1, 2);
+ $caller->process(1);
}
}
Streamline the operator arguments of version_compare function
-version_compare(PHP_VERSION, '5.6', 'gte');
+version_compare(PHP_VERSION, '5.6', 'ge');
Replace defined map of arguments in defined methods and their calls
$someObject = new SomeClass;
-$someObject->someMethod(SomeClass::OLD_CONSTANT);
+$someObject->someMethod(false);
Changes Closure to be static when possible
-function () {
+static function () {
if (rand(0, 1)) {
return 1;
}
return 2;
}
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];
}
}
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;
}
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',
+ };
}
}
Changes ArrowFunction to be static when possible
-fn (): string => 'test';
+static fn (): string => 'test';
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;
}
}
Turn defined function call new one
-view("...", []);
+Laravel\Templating\render("...", []);
Turn method names to new ones
-SomeClass::oldStaticMethod();
+AnotherExampleClass::newStaticMethod();
Replace defined old properties by new ones
-$someObject->someOldProperty;
+$someObject->someNewProperty;
Replace constant by new ones
final class SomeClass
{
public function run()
{
- return MYSQL_ASSOC;
+ return MYSQLI_ASSOC;
}
}
Change string value
class SomeClass
{
public function run()
{
- return 'ROLE_PREVIOUS_ADMIN';
+ return 'IS_IMPERSONATOR';
}
}
Rename attribute class names
-#[SimpleRoute()]
+#[BasicRoute()]
class SomeClass
{
}
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()
{
}
}
Remove annotation by names
-/**
- * @method getName()
- */
final class SomeClass
{
}
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;
}
}
}
Remove defined function calls
-$x = 'something';
-var_dump($x);
+$x = 'something';
Remove argument by position by function name
-remove_last_arg(1, 2);
+remove_last_arg(1);
Remove specific traits from code
class SomeClass
{
- use SomeTrait;
}
Remove interfaces from class
-class SomeClass implements SomeInterface
+class SomeClass
{
}
Remove defined arguments in defined methods and their calls
$someObject = new SomeClass;
-$someObject->someMethod(true);
+$someObject->someMethod();
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;
}
}
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;
}
}
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';
}
}
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();
}
}
Add type to property by added rules, mostly public/property by parent type
class SomeClass extends ParentClass
{
- public $name;
+ public string $name;
}
Add declare(strict_types=1) if missing in a namespaced file
+declare(strict_types=1);
+
namespace App;
class SomeClass
{
function someFunction(int $number)
{
}
}
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()
{
}
Add @return docblock for scalar array from strict array assignments
+/**
+ * @return string[]
+ */
function getSomeItems()
{
$items = [];
$items[] = 'hey';
$items[] = 'hello';
return $items;
}
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;
});
}
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);
}
}