Skip to content

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

Open
@arderyp

Description

@arderyp

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. 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions