Skip to content

[TypeDeclaration] Handle getRepository() return via local variable in ReturnTypeFromGetRepositoryDocblockRector#8148

Merged
TomasVotruba merged 1 commit into
mainfrom
return-type-get-repository-via-variable
Jul 5, 2026
Merged

[TypeDeclaration] Handle getRepository() return via local variable in ReturnTypeFromGetRepositoryDocblockRector#8148
TomasVotruba merged 1 commit into
mainfrom
return-type-get-repository-via-variable

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Extends ReturnTypeFromGetRepositoryDocblockRector to also add the return type when the repository is fetched into a local variable, mutated, then returned - not only when returned directly.

Before

final class LeadModel
{
    public function __construct(private \Doctrine\ORM\EntityManagerInterface $em)
    {
    }

    /**
     * @return LeadListRepository
     */
    public function getRepository()
    {
        /** @var LeadListRepository $repo */
        $repo = $this->em->getRepository(LeadList::class);

        $repo->setDispatcher($this->dispatcher);
        $repo->setTranslator($this->translator);

        return $repo;
    }
}

After

final class LeadModel
{
    public function __construct(private \Doctrine\ORM\EntityManagerInterface $em)
    {
    }

    public function getRepository(): LeadListRepository
    {
        /** @var LeadListRepository $repo */
        $repo = $this->em->getRepository(LeadList::class);

        $repo->setDispatcher($this->dispatcher);
        $repo->setTranslator($this->translator);

        return $repo;
    }
}

Bails out when the returned variable is reassigned to a non-getRepository() expression, to avoid a wrong type.

@TomasVotruba TomasVotruba merged commit 0ff467c into main Jul 5, 2026
65 checks passed
@TomasVotruba TomasVotruba deleted the return-type-get-repository-via-variable branch July 5, 2026 21:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant