phpstan-doctrine icon indicating copy to clipboard operation
phpstan-doctrine copied to clipboard

BUG: Generic T not carried over in QueryBuilder::from()

Open arderyp opened this issue 9 months ago • 1 comments

see: this discussion and @ondrejmirtes's response.

CODE

/**
 * @template TypeEntity of AbstractThisEntity
 * @extends EntityRepository<TypeEntity>
 */
abstract class AbstractThisRepository extends EntityRepository
{
    /** @return TypeEntity[] */
    public function findAllActive(): array
    {
        return $this->getEntityManager()->createQueryBuilder()
            ->select('entity')
            ->from($this->getEntityClass(), 'entity')
            ->where('entity.active = 1')
            ->getQuery()
            ->getResult();
    }

    /** @return class-string<TypeEntity> */
    abstract protected function getEntityClass(): string;
}

ERROR:

Method App\Repository\AbstractThisRepository::findAllActive() should return array<TypeEntity of App\Entity\AbstractThisEntity> but returns list<App\Entity\AbstractThisEntity>.  
 🪪  return.type                                                                                                    
    💡 Type App\Entity\AbstractThisEntity is not always the same as TypeEntity. It breaks the         
         contract for some argument types, typically subtypes. 

arderyp avatar Feb 21 '25 18:02 arderyp

@ondrejmirtes is this something you still think is fixable?

arderyp avatar May 06 '25 21:05 arderyp