phpstan-doctrine
phpstan-doctrine copied to clipboard
Use a @template for countDistinct
Not complete.
The tests that use a non-literal-string for parameters 2 and 3, incorrectly return a literal-string.
These tests should work (results).
I suspect the original function definition, which implies only 1 parameter is used (source):
public function countDistinct($x)
{
return 'COUNT(DISTINCT ' . implode(', ', func_get_args()) . ')';
}
Maybe the Stub file should be overriding this? Because I suspect the @template only considers the first parameter in this case, even though the Stub tries to correct it (source):
/**
* @template T of string
* @param T ...$x
* @return (T is literal-string ? literal-string : string)
*/
public function countDistinct(...$x) {
}
I have opened PR #9911 on doctrine/orm, as it should be fixed there as well.
Stub files are only for overriding PHPDocs, not variadic-ness of parameters :)
Yep, thanks Ondřej, hopefully Doctrine will accept my PR :-)
My PR to fix the Expr::countDistinct() arguments has been accepted & merged for Doctrine ORM 3.0.
But 3.0 doesen't seem to have an expected release date. There are "what to expect" type articles from 2019, and it looks like they are working on 2.12 (current) and 2.13 (dev)... so I doubt this will be resolved any time soon.
Should I:
- Leave this PR here until
phpstan-doctrinestarts usingdoctrine-orm3.0 (could be a long wait). - Temporarily comment out the 2 tests that check the 2nd and 3rd arguments,
countDistinctNonLiteralString3andcountDistinctNonLiteralString4(this PR kinda works, but will incorrectly say something is aliteral-stringeven if any of the 2nd or later arguments are provided with an unsafestring; so it won't identify mistakes until 3.0 is in use, but will allowExpr::countDistinct()to return aliteral-string). - Close this PR, and try again when 3.0 is in use (could be a while)?
Feel free to do option number 2) so that we have benefits faster :)
Thanks, have commented out those tests.
The failing tests are for PHP 8.2, which seems to be affecting main as well... and I did re-create the patch, as I made a mess when trying to rebase (one day I'll get that right).
Thank you :)