Fix return type on EntityRepository::matching() generics#742
Fix return type on EntityRepository::matching() generics#742Firehed wants to merge 8 commits intophpstan:2.0.xfrom
EntityRepository::matching() generics#742Conversation
| * @return \Doctrine\Common\Collections\Collection | ||
| * | ||
| * @psalm-return \Doctrine\Common\Collections\Collection<int, TEntityClass> | ||
| * @phpstan-return \Doctrine\Common\Collections\AbstractLazyCollection<int, TEntityClass> |
There was a problem hiding this comment.
According to doctrine/orm it should be
AbstractLazyCollection<int, T>&Selectable<int, T>
NB: this was introduced in doctrine/orm 2.8+
doctrine/orm@f1219f1
Given the fact we have the correct type in doctrine/orm in 2.16+
https://github.com/doctrine/orm/blame/495cd06b9a630f9c38a21ceb249ed008edbe8414/lib/Doctrine/ORM/EntityRepository.php#L327
and the fact we only test this lib on 2.16+
phpstan-doctrine/composer.json
Line 30 in 81dac0e
Maybe it's better to remove the method and rely on the original signature ?
|
I originally had it with that signature and PHPStan refined it out (as noted, |
|
@VincentLanglet if I remove the definition for This implies to me that info is getting lost in the call to I don't particularly understand why this is the case here - all of the types in the stubs look correct to my eyes. Perhaps something is off with a Given that, I left it in place (but re-added the &Selectable from upstream). The PHPstan failure itself I don't understand at all - it says |
|
If you use AbstractLazyCollection in a stub, a stub is need for AbstractLazyCollection too |
Fixes generic type inference for both ORM2 and ORM3 by computing the return type programmatically instead of relying on stubs. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
Ended up taking a different path here, trying to rectify the subtle differences between ORM v2 and v3 didn't seem to work with purely stub tweaks. That said, there are still some adjustments that correct divergence from the actual library. I tested it against a real project updated to ORMv3 (using the local composer repository symlink thing) and saw the expected behavior, and was able to remove some baselines and Happy to adjust further, LMK what you think. The platform test seems to be unrelated and failing on the default branch. |
|
@VincentLanglet LGTM - I tested your patch (at 9e07424) against my problem-repo and that also fixed the issue. It's closer to what I was originally aiming for, but you're far more familiar with the internals than I am so I guess you figured out the right trick. Feel free to close this in favor of your implementation. |
|
Thanks, after the merge, I'll also try to reduce the stub by removing some methods. For instance I think the issue you encounter when you tried to remove |
|
Sounds good, thanks for the assist! |
Fixes a regression where the collection returned by matching() would lose the generic metadata, and downstream code reading the values would get
mixed.e.g. this worked under ORM2 + this plugin, but fails under ORM3 because $feed became mixed:
This changed in ORM3, which actually has native types that are correct and were being overridden by the stub. I've updated the stub and added a test to reflect the return value.
Note: ORM3 specifies
AbstractLazyCollection&Selectableas the return type, butAbstractLazyCollectionalready implementsSelectableso PHPStan refines it out as redundant.