Find the best Rector rule to solve your problem


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;
 }
SETS:  Code Quality


Configurable

RenameClassConstFetchRector

Replaces defined class constants in their calls.

-$value = SomeClass::OLD_CONSTANT;
-$value = SomeClass::OTHER_OLD_CONSTANT;
+$value = SomeClass::NEW_CONSTANT;
+$value = DifferentClass::NEW_CONSTANT;

Configurable

RenameClassRector

Replaces defined classes by new ones.

 namespace App;

-use SomeOldClass;
+use SomeNewClass;

-function someFunction(SomeOldClass $someOldClass): SomeOldClass
+function someFunction(SomeNewClass $someOldClass): SomeNewClass
 {
-    if ($someOldClass instanceof SomeOldClass) {
-        return new SomeOldClass;
+    if ($someOldClass instanceof SomeNewClass) {
+        return new SomeNewClass;
     }
 }

Configurable

ArgumentRemoverRector

Removes defined arguments in defined methods and their calls.

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

Configurable

AddParamTypeDeclarationRector

Add param types where needed

 class SomeClass
 {
-    public function process($name)
+    public function process(string $name)
     {
     }
 }

Configurable

AddReturnTypeDeclarationRector

Changes defined return typehint of method and class.

 class SomeClass
 {
-    public function getData()
+    public function getData(): array
     {
     }
 }

ReplaceFetchAllMethodCallRector

Change Doctrine\DBAL\Connection and Doctrine\DBAL\Driver\ResultStatement ->fetchAll() to ->fetchAllAssociative() and other replacements

 use Doctrine\DBAL\Connection;

 class SomeClass
 {
     public function run(Connection $connection)
     {
-        return $connection->fetchAll();
+        return $connection->fetchAllAssociative();
     }
 }

ExtractArrayArgOnQueryBuilderSelectRector

Extract array arg on QueryBuilder select, addSelect, groupBy, addGroupBy

 function query(\Doctrine\DBAL\Query\QueryBuilder $queryBuilder)
 {
-    $query = $queryBuilder->select(['u.id', 'p.id']);
+    $query = $queryBuilder->select('u.id', 'p.id');
 }

CriteriaOrderingConstantsDeprecationRector

Replace ASC/DESC with enum Ordering in Criteria::orderBy method call, and remove usage of Criteria::ASC and Criteria::DESC constants elsewhere

 use Doctrine\Common\Collections\Criteria;

 $criteria = new Criteria();
-$criteria->orderBy(['someProperty' => 'ASC', 'anotherProperty' => 'DESC']);
+$criteria->orderBy(['someProperty' => \Doctrine\Common\Collections\Order::Ascending, 'anotherProperty' => \Doctrine\Common\Collections\Order::Descending]);

ChangeCompositeExpressionAddMultipleWithWithRector

Change CompositeExpression ->addMultiple($parts) to ->with(...$parts)

 use Doctrine\ORM\EntityRepository;
 use Doctrine\DBAL\Query\Expression\CompositeExpression;

 class SomeRepository extends EntityRepository
 {
     public function getSomething($parts)
     {
         $compositeExpression = CompositeExpression::and('', ...$parts);
-        $compositeExpression->addMultiple($parts);
+        $compositeExpression->with(...$parts);
     }
 }

EventSubscriberInterfaceToAttributeRector

Replace EventSubscriberInterface with AsDoctrineListener attribute(s)

+use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener;
 use Doctrine\ORM\Event\PrePersistEventArgs;
 use Doctrine\ORM\Event\PostUpdateEventArgs;
-use Doctrine\Common\EventSubscriberInterface;
 use Doctrine\ORM\Events;

-class MyEventSubscriber implements EventSubscriberInterface
+#[AsDoctrineListener(event: Events::postUpdate)]
+#[AsDoctrineListener(event: Events::prePersist)]
+class MyEventSubscriber
 {
-    public function getSubscribedEvents()
-    {
-        return array(
-            Events::postUpdate,
-            Events::prePersist,
-        );
-    }
-
     public function postUpdate(PostUpdateEventArgs $args)
     {
         // ...
     }

     public function prePersist(PrePersistEventArgs $args)
     {
         // ...
     }
 }

ReplaceLifecycleEventArgsByDedicatedEventArgsRector

Replace Doctrine\ORM\Event\LifecycleEventArgs with specific event classes based on the function call

-use Doctrine\ORM\Event\LifecycleEventArgs;
+use Doctrine\ORM\Event\PrePersistEventArgs;

 class PrePersistExample
 {
-    public function prePersist(LifecycleEventArgs $args)
+    public function prePersist(PrePersistEventArgs $args)
     {
         // ...
     }
 }

CorrectDefaultTypesOnEntityPropertyRector

Change default value types to match Doctrine annotation type

 use Doctrine\ORM\Mapping as ORM;

 /**
  * @ORM\Entity()
  */
 class User
 {
     /**
      * @ORM\Column(name="is_old", type="boolean")
      */
-    private $isOld = '0';
+    private $isOld = false;
 }
SETS:  Code Quality

TypedPropertyFromColumnTypeRector

Complete @var annotations or types based on @ORM\Column

 use Doctrine\ORM\Mapping as ORM;

 class SimpleColumn
 {
     /**
      * @ORM\Column(type="string")
      */
-    private $name;
+    private string|null $name = null;
 }
SETS:  Code Quality

Configurable

TypedPropertyFromToOneRelationTypeRector

Complete @var annotations or types based on @ORM\*toOne annotations or attributes

 use Doctrine\ORM\Mapping as ORM;

 class SimpleColumn
 {
     /**
      * @ORM\OneToOne(targetEntity="App\Company\Entity\Company")
      * @ORM\JoinColumn(nullable=false)
      */
-    private $company;
+    private ?\App\Company\Entity\Company $company = null;
 }
SETS:  Code Quality

ImproveDoctrineCollectionDocTypeInEntityRector

Improve @var, @param and @return types for Doctrine collections to make them useful both for PHPStan and PHPStorm

 use Doctrine\Common\Collections\Collection;
 use Doctrine\ORM\Mapping as ORM;

 /**
  * @ORM\Entity
  */
 class SomeClass
 {
     /**
      * @ORM\OneToMany(targetEntity=Trainer::class, mappedBy="trainer")
-     * @var Collection|Trainer[]
+     * @var Collection<int, Trainer>
      */
     private $trainings = [];

+    /**
+     * @param Collection<int, Trainer> $trainings
+     */
     public function setTrainings($trainings)
     {
         $this->trainings = $trainings;
     }
 }

MakeEntityDateTimePropertyDateTimeInterfaceRector

Make maker bundle generate DateTime property accept DateTimeInterface too

 use Doctrine\ORM\Mapping as ORM;

 /**
  * @ORM\Entity()
  */
 class User
 {
     /**
-     * @var DateTime|null
+     * @var DateTimeInterface|null
      */
     private $bornAt;

     public function setBornAt(DateTimeInterface $bornAt)
     {
         $this->bornAt = $bornAt;
     }
 }
SETS:  Code Quality

TypedPropertyFromToManyRelationTypeRector

Complete @var annotations or types based on @ORM\*toMany annotations or attributes

 use Doctrine\ORM\Mapping as ORM;

 class SimpleColumn
 {
     /**
      * @ORM\OneToMany(targetEntity="App\Product")
+     * @var \Doctrine\Common\Collections\Collection<int, \App\Product>
      */
-    private $products;
+    private \Doctrine\Common\Collections\Collection $products;
 }

MoveCurrentDateTimeDefaultInEntityToConstructorRector

Move default value for entity property to constructor, the safest place

 use Doctrine\ORM\Mapping as ORM;

 /**
  * @ORM\Entity()
  */
 class User
 {
     /**
      * @var DateTimeInterface
      *
-     * @ORM\Column(type="datetime", nullable=false, options={"default"="now()"})
+     * @ORM\Column(type="datetime", nullable=false)
      */
-    private $when = 'now()';
+    private $when;
+
+    public function __construct()
+    {
+        $this->when = new \DateTime();
+    }
 }
SETS:  Code Quality

ExplicitRelationCollectionRector

Use Collection object type for one-to-many relations of Doctrine entity/ODM document

+use Doctrine\ORM\Mapping\Entity;
 use Doctrine\ORM\Mapping\OneToMany;
-use Doctrine\ORM\Mapping\Entity;
+use Doctrine\Common\Collections\ArrayCollection;
+use Doctrine\Common\Collections\Collection;

 #[Entity]
 class SomeClass
 {
     #[OneToMany(targetEntity: 'SomeClass')]
-    private $items = [];
+    private Collection $items;
+
+    public function __construct()
+    {
+        $this->items = new ArrayCollection();
+    }
 }

RemoveEmptyTableAttributeRector

Remove empty Table attribute on entities because it's useless

 <?php

 namespace RectorPrefix202411;

 use RectorPrefix202411\Doctrine\ORM\Mapping as ORM;
-#[ORM\Table]
 #[ORM\Entity]
 class Product
 {
 }
 \class_alias('Product', 'Product', \false);
SETS:  Code Quality

AddReturnDocBlockToCollectionPropertyGetterByToManyAnnotationRector

Adds @return PHPDoc type to Collection property getter by *ToMany annotation/attribute

-use App\Entity\Training;
-
 /**
  * @ORM\Entity
  */
 final class Trainer
 {
     /**
      * @ORM\OneToMany(targetEntity=Training::class)
      */
     private $trainings;

+    /**
+     * @return \Doctrine\Common\Collections\Collection<int, \App\Entity\Training>
+     */
     public function getTrainings()
     {
         return $this->trainings;
     }
 }