1

I've created a function in my repository :

/**
     * @return array<int, array<int, string>>
     */
    public function findByBuildingGroupByCompany(string $buildingId): array
    {
        return $this->createQueryBuilder('qr')
            ->select('DISTINCT(qr.company)')
            ->leftJoin('qr.building', 'b')
            ->leftJoin('qr.quotes', 'q')
            ->andWhere('b.id = :buildingId')
            ->andWhere('qr.company IS NOT NULL')
            ->setParameter('buildingId', $buildingId)
            ->getQuery()
            ->getResult()
            ;
    }

This function is called in my controller and if I dump it, I've got

array:3 [▼
  0 => array:1 [▼
    1 => "a00e9bcb-bffd-442a-aa9c-b0ca627fda36"
  ]
  1 => array:1 [▼
    1 => "777ea601-39d0-4d54-939f-64a56912a843"
  ]
  2 => array:1 [▼
    1 => "b78bf56c-b99d-40c7-b317-458c4df7e286"
  ]
]

So my function return what I typed array<int, array<int, string>>but phpstan throw this error : Method App\Repository\QuoteRequestRepository::findByBuildingGroupByCompany() should return array<int, array<int, string>> but returns mixed.
And if I return mixed, it throws those 2 errors :

Method
         App\Repository\QuoteRequestRepository::findByBuildingGroupByCompany()
         return type has no value type specified in iterable type array.
          See:
         https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-i
         terable-type
  49     Method
         App\Repository\QuoteRequestRepository::findByBuildingGroupByCompany()
         should return array but returns mixed

I really don't know how to handle it :(

Any idea ?

1 Answers1

1

Recent versions of phpstan-doctrine extension now include type inference for DQL, including QueryBuilders, so the returned type will now be more specific than “mixed”.

Install the extension and see how it improves.

Ondřej Mirtes
  • 5,054
  • 25
  • 36