rector icon indicating copy to clipboard operation
rector copied to clipboard

Incorrect behavior of AddReturnDocBlockToCollectionPropertyGetterByToManyAnnotationRector, ExplicitRelationCollectionRector

Open AlexeyKosov opened this issue 1 year ago • 0 comments

Bug Report

Subject Details
Rector version last dev-main
Installed as composer dependency

Minimal PHP Code Causing Issue

See https://getrector.com/demo/557ea3f4-d855-4f40-bc78-cdd3f46bebb0

<?php
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity()]
class Training
{
    #[ORM\Column(name: 'id', type: 'string')]
    #[ORM\Id]
    private string $id;
    
    #[ORM\ManyToOne(targetEntity: Trainer::class, inversedBy: "trainings")]
    private Trainer $trainer;
}

#[ORM\Entity()]
class Trainer
{
    /**
     * @var Collection|Training[]
     */
    #[ORM\OneToMany(targetEntity: Training::class, indexBy: "id", mappedBy: "trainer")]
    private $trainings = [];
    
    public function getTrainings()
    {
        return $this->trainings;
    }
    
    public function setTrainings($trainings)
    {
        $this->trainings = $trainings;
    }
}

Responsible rules

  • AddReturnDocBlockToCollectionPropertyGetterByToManyAnnotationRector

  • ExplicitRelationCollectionRector

Expected Behavior

The Training entity's ID is a string, so the Trainer::$trainings collection when indexed by ID (note the indexBy: "id" property), should have the Collection<string, Training> type rather than Collection<int, Training>.

So the point is that collections don't always have integer keys as it's hardcoded now, and it would be great to either properly determine their key type or skip the key definition at all (just use Collection<Training>)

AlexeyKosov avatar Aug 15 '24 15:08 AlexeyKosov