Find a Rector rule

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

Found 10 rules, grouped by set: Clear

PHP 8.3 1 match

Add type to constants based on their value

 final class SomeClass
 {
-    public const TYPE = 'some_type';
+    public const string TYPE = 'some_type';
 }
Not in a set 2 matches

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

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

Add @param node type to refactor() method based on getNodeTypes() of a Rector rule, to avoid instanceof checks and unknown types reported by PHPStan

 public function getNodeTypes(): array
 {
     return [MethodCall::class];
 }

+/**
+ * @param MethodCall $node
+ */
 public function refactor(Node $node): ?Node
 {
 }
Type Declarations 7 matches

Applies type hints to closures on Iterable method calls where key/value types are documented

 class SomeClass
 {
     /**
      * @param Collection<int, string> $collection
      */
     public function run(Collection $collection)
     {
-        return $collection->map(function ($item, $key) {
+        return $collection->map(function (string $item, int $key) {
             return $item . $key;
         });
     }
 }

Add return type declarations from yields

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

Applies type hints to array_map closures

-array_reduce($strings, function ($carry, $value, $key): string {
+array_reduce($strings, function (string $carry, string $value): string {
     return $carry . $value;
 }, $initialString);

Applies type hints to array_map closures

-array_map(function ($value) {
+array_map(function (string $value) {
     return strlen($value);
 }, $strings);

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 param object type based on the argument the assigned closure variable is called with

-$printItem = function ($item) {
+$printItem = function (Item $item) {
     echo $item->name;
 };

 $printItem(new Item());

Add closure return type void if there is no return

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