phpstan-doctrine icon indicating copy to clipboard operation
phpstan-doctrine copied to clipboard

Use a @template for countDistinct

Open craigfrancis opened this issue 3 years ago • 6 comments

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) {
}

craigfrancis avatar Jul 18 '22 14:07 craigfrancis

I have opened PR #9911 on doctrine/orm, as it should be fixed there as well.

craigfrancis avatar Jul 18 '22 16:07 craigfrancis

Stub files are only for overriding PHPDocs, not variadic-ness of parameters :)

ondrejmirtes avatar Jul 18 '22 16:07 ondrejmirtes

Yep, thanks Ondřej, hopefully Doctrine will accept my PR :-)

craigfrancis avatar Jul 18 '22 16:07 craigfrancis

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:

  1. Leave this PR here until phpstan-doctrine starts using doctrine-orm 3.0 (could be a long wait).
  2. Temporarily comment out the 2 tests that check the 2nd and 3rd arguments, countDistinctNonLiteralString3 and countDistinctNonLiteralString4 (this PR kinda works, but will incorrectly say something is a literal-string even if any of the 2nd or later arguments are provided with an unsafe string; so it won't identify mistakes until 3.0 is in use, but will allow Expr::countDistinct() to return a literal-string).
  3. Close this PR, and try again when 3.0 is in use (could be a while)?

craigfrancis avatar Jul 19 '22 09:07 craigfrancis

Feel free to do option number 2) so that we have benefits faster :)

ondrejmirtes avatar Sep 21 '22 12:09 ondrejmirtes

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).

craigfrancis avatar Sep 21 '22 15:09 craigfrancis

Thank you :)

ondrejmirtes avatar Oct 26 '22 07:10 ondrejmirtes