-
Notifications
You must be signed in to change notification settings - Fork 103
QueryBuilder Result returns mixed when created in another method #308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Have the same problem here, but cannot find the root cause. The QueryResultDynamicReturnTypeExtension always gives me MixedType as $queryResultType so i cannot infer the right type. But i cannot figure out why this is the case. |
Should have read better the documentation. Added the option objectManagerLoader with a proper php configuration and now it works. |
Nice but apparently it's not the case for the reproduction repo: https://github.com/alexander-schranz/phpstan-query-builder-reproducer/blob/26c04ecb3495a187f75d8082e53a5749cc62b3ff/phpstan.neon#L18 |
Same probleme here. @sabbelasichon can you explain please what does mean |
See README about Or here another example from our sulu/skeleton which is symfony based:
Sadly it doesn't work for my case. But hope it works for you. |
@alexander-schranz same problem for me. The proposed solution does not work... 😢 |
Can't get it to work either with the suggestions mentioned above. To ignore this error (supports subnamespaces like # phpstan.neon
parameters:
ignoreErrors:
- '#Method App\\Repository\\(\w+\\)*\w+Repository::(get|find)\w+\(\) should return ((array|iterable)<)?App\\Entity\\(\w+\\)*\w+(>)?(\|null)? but returns (object|mixed)\.#' |
|
@cyrille-osyla which errors are reported for your repositories when you run PHPStan? |
@CodeCasterNL |
There's a helpful tool on PHPStan's website to generate the |
@cyrille-osyla I've updated the regex, your method started with "get" instead of "find". |
@CodeCasterNL @ondrejmirtes good to know thank you ! |
Hi! Is there a way to fix this without ignoring it? |
I'm also running into this issue in a bleeding edge development environment, both with Symfony 6.2 and 6.3-beta. Simple reproducer: use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Unit>
*/
class UnitRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Unit::class);
}
/**
* @return Unit[]
*/
public function getUnits(): array
{
return $this->createQueryBuilder('u')->getQuery()->getResult();
}
} Gives:
Notably this only occurs at level 9, it passes without issue on level 8. PHPStan documents level 9 as:
Looking at https://github.com/phpstan/phpstan-doctrine/blob/1.3.x/stubs/ORM/QueryBuilder.stub#L17-L22 it would seem PHPStan is shooting its own foot - the stub is prescribing something that cannot pass level 9 by definition. |
Does anyone have a clue (because I don't tbh) on how to implement a fundamental solution or are we stuck with an ignore entry forever? Notably the 2.0 stub still uses |
What's in the stub here is irrelevant. There's a dynamic return type extension that overrides that: https://github.com/phpstan/phpstan-doctrine/blob/2.0.x/src/Type/Doctrine/QueryBuilder/QueryBuilderGetQueryDynamicReturnTypeExtension.php I suspect that the problem with:
Might happen for different reasons in different projects. It usually works properly. |
I have a project here with 12 of those rare exceptions 😆 Thanks for the pointer to that file, I see it changed a lot in 2.0 so I'll have a look at a fix when we've upgraded this project to 2.0 (which includes fixing 99 errors not reported before so not today). |
If you reproduce at least one, ideally here in the test suite, that would be great. Here's a very similar test:
It shows the happy paths work. |
Here's the most basic one I have, integrally copied: /** @extends ServiceEntityRepository<Payment> */
class PaymentRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Payment::class);
}
public function findByExternalTransactionId(string $id): ?Payment
{
return $this
->createQueryBuilder('p')
->where('p.externalTransactionId = :id')->setParameter('id', $id)
->getQuery()->getOneOrNullResult()
;
}
} Gives:
Using |
|
Oh my that does indeed fix it, but the DX on this is horrible. I also have cases with the dynamic query builder scenario mentioned there, which are only realistically discoverable by enabling We should definitely have more verbose errors here like an extra level 9 check on calling |
We are using this custom PHPStan rule to ensure only inferrable calls are used in our codebase. |
I'm still running into the #266 issue, which I thought should be fixed with: 149cf71
But in my cases it ends still in
mixed
state.I created a reproducer here:
git clone git@github.com:alexander-schranz/phpstan-query-builder-reproducer.git cd phpstan-query-builder-reproducer/ composer install vendor/bin/phpstan analyse
https://github.com/alexander-schranz/phpstan-query-builder-reproducer/blob/main/src/Repository/VisitorRepository.php
Output:
Not sure what is different then to the test case 🤔
The text was updated successfully, but these errors were encountered: