order of QueryBuilder::select arguments is not preserved in QueryBuilder::getResult

Open
#482 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
38/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
php
Domain
databases

Research direction

Start with src/Type/Doctrine/Query/QueryResultDynamicReturnTypeExtension.php at the referenced lines 117-127 and trace how selected expressions are converted into the getResult return type. Verify the behavior for select('sc, ld') and add coverage showing that the tuple preserves selection order; done means PHPStan reports the corresponding ordered array shape without the workaround.

Written by the indexing model from the issue text.

Description

According to https://github.com/phpstan/phpstan-doctrine/blob/90e60ba9dbea4b29c7b87026a29e91ac0a02674e/src/Type/Doctrine/Query/QueryResultDynamicReturnTypeExtension.php#L117-L127, QueryBuilder::getResult returns an array withh all returned types, and does not preserve order.

As an example, we had this code:

    /*
     * @return array{ 0: SearchCustomization | null, 1: LocalizationDetail | null }
     */
    public function findLandingInformation(string $url, string $locale): array
    {
        $queryBuilder = $this->getEntityManager()->createQueryBuilder()
            ->select('sc, ld')
            ->from(SearchCustomization::class, 'sc')
            ->innerJoin(SearchCustomizationText::class, 'sct', Join::WITH, 'sct.searchCustomization = sc')
            ->innerJoin(Language::class, 'l', Join::WITH, 'sct.language = l')
            ->leftJoin(LocalizationDetail::class, 'ld', Join::WITH, 'ld.localization = sc.localization')
                    ->where('sct.url = :url')
                    ->andWhere('l.code = :language')
                    ->andWhere('sc.isActive = 1')
                    ->andWhere('ld.language = l')
                    ->setParameter('url', $url)
                    ->setParameter('language', $locale);

        return $queryBuilder->getQuery()->getResult();
    }

and phpstan complained that

Method findLandingInformation()  
         should return array{Api\Entity\SearchCustomization|null,                        
         Api\Entity\LocalizationDetail|null} but returns array<int,                      
         Api\Entity\LocalizationDetail|Api\Entity\SearchCustomization|null>.

so we had to write this to help phpstan understand:

    /*
     * @return array{ 0: SearchCustomization | null, 1: LocalizationDetail | null }
     */
    public function findLandingInformation(string $url, string $locale): array
    {
        $queryBuilder = $this->getEntityManager()->createQueryBuilder()
            ->select('sc, ld')
            ->from(SearchCustomization::class, 'sc')
            ->innerJoin(SearchCustomizationText::class, 'sct', Join::WITH, 'sct.searchCustomization = sc')
            ->innerJoin(Language::class, 'l', Join::WITH, 'sct.language = l')
            ->leftJoin(LocalizationDetail::class, 'ld', Join::WITH, 'ld.localization = sc.localization')
                    ->where('sct.url = :url')
                    ->andWhere('l.code = :language')
                    ->andWhere('sc.isActive = 1')
                    ->andWhere('ld.language = l')
                    ->setParameter('url', $url)
                    ->setParameter('language', $locale);

        [$searchCustomization, $localizationDetail] = $queryBuilder->getQuery()->getResult();

        return [
            is_a($searchCustomization, SearchCustomization::class) ? $searchCustomization : null,
            is_a($localizationDetail, LocalizationDetail::class) ? $localizationDetail : null,
        ];
    }
Dominant language
PHP
Stars
678
Forks
122
Avg merge
5d 18h
Merged PRs (30d)
3

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from phpstan/phpstan-doctrine

All issues in phpstan/phpstan-doctrine

Similar issues

More PHP issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.