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

Query result type is not recognized if the alias is `"attribute"`

Open julienfalque opened this issue 1 year ago • 0 comments

When using the query builder, if the queried entity has alias "attribute", the type of the query result is considered mixed instead of list<Entity>. For example, with the following code:

namespace App\Repository;

use App\Entity\Attribute;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

/**
 * @extends ServiceEntityRepository<Attribute>
 */
class AttributeRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Attribute::class);
    }

    /**
     * @return list<Attribute>
     */
    public function all(): array
    {
        return $this->createQueryBuilder('attribute')
            ->getQuery()
            ->getResult();
    }
}

PHPStan will report:

------ -------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
  Line   src/App/Repository/AttributeRepository.php                                                                                          
 ------ -------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
  20     Method App\Repository\AttributeRepository::all() should return list<App\Entity\Attribute> but returns mixed.  
         💡 mixed is not a list.                                                                                                                                             
 ------ --------------------------------------------------------------------------------------------------------------------------------------------------------------------

Changing the alias to any other value fixes the error.

julienfalque avatar May 17 '24 07:05 julienfalque