order of QueryBuilder::select arguments is not preserved in QueryBuilder::getResult
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 38/100
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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from phpstan/phpstan-doctrine
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
phpstan/phpstan-doctrine#794 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
phpstan/phpstan-doctrine#785 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 65/100
phpstan/phpstan-doctrine#792 · 1 reaction ·
-
Difficulty 3/5 1-2 days Newbie friendliness 65/100
phpstan/phpstan-doctrine#788 · 2 comments ·
-
Difficulty 3/5 1-2 days Newbie friendliness 55/100
phpstan/phpstan-doctrine#763 ·
All issues in phpstan/phpstan-doctrine
Similar issues
-
priority: p3
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
googleapis/librarian#7636 ·
-
0. Needs triage bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
nextcloud/fulltextsearch#1011 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
Automattic/static-site-importer#1767 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100