Find the best Rector rule to solve your problem


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;
     }
 }

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;
 }

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();
+    }
 }

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;
     }
 }