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

stubs/ORM/Query/Expr.stub wrongs params

Open Thierry-Kq opened this issue 3 years ago • 1 comments

Hi, i got this error :

Parameter #2 $x of method Doctrine\ORM\Query\Expr::between() expects string, int|string given

But if i look in lib/Doctrine/ORM/Query/Expr.php, i got theses params :

     * @param mixed      $val Valued to be inspected by range values.
     * @param int|string $x   Starting range value to be used in BETWEEN() function.
     * @param int|string $y   End point value to be used in BETWEEN() function.

I solved it on my side by casting my int into string, but i guess the stubs/ORM/Query/Expr.stub is wrong, or maybe i misunderstood something.

	 * @param string $val
	 * @param string $x
	 * @param string $y

Im using phpstan-doctrine 1.3.13, phpstan/phpstan 1.8.5, phpstan/phpstan-symfony 1.2.13 and doctrine/orm 2.13.1

Thierry-Kq avatar Sep 14 '22 16:09 Thierry-Kq

  1. Research when the int type was added/removed in the Git history of the library
  2. If the int type doesn't look deprecated and is going to be supported into the future, send a PR here with a stub update.

ondrejmirtes avatar Sep 14 '22 16:09 ondrejmirtes

The stub was introduced by @craigfrancis in https://github.com/phpstan/phpstan-doctrine/pull/324

And this doesn't seems to be deprecated https://github.com/doctrine/orm/blob/0aa45dd607100182294d31dee42b89338773f87e/lib/Doctrine/ORM/Query/Expr.php#L638-L650

But there is a conditional return type:

@return ($val is literal-string ? ($x is literal-string ? ($y is literal-string ? literal-string&non-empty-string : string) : string) : string)

which will maybe need to be updated since $x and $y can be int.

If I look at https://phpstan.org/r/9324b003-b39b-49c9-b911-13a6d4521638

  • (string) $int is not considered as literal
  • literal-int doesn't exists.

So, I'm not sure there is a solution to infer

->between('literal', 1, 9)

as a literal-string since

->between('literal', '1', '9')

does.

VincentLanglet avatar Dec 27 '22 00:12 VincentLanglet

Thanks for finding Vincent,

I've created PR #407, which simply allows int values for these arguments.

There is no need to change the conditional return type, as it shouldn't return a literal-string when an integer is used.

The failing "mod function error" test should be unrelated to my changes.

As an aside, I'm drafting the LiteralString PHP RFC, and I've got an Integer Values section which hopefully summarises why PHP won't be able to support literal-int (which I'm still disappointed about, and that's why I don't think static analysis tools should support it either).

craigfrancis avatar Dec 28 '22 16:12 craigfrancis

The failing "mod function error" test should be unrelated to my changes.

Yes, it's fixed in https://github.com/phpstan/phpstan-doctrine/pull/405

There is no need to change the conditional return type, as it shouldn't return a literal-string when an integer is used.

As an aside, I'm drafting the LiteralString PHP RFC, and I've got an Integer Values section which hopefully summarises why PHP won't be able to support literal-int (which I'm still disappointed about, and that's why I don't think static analysis tools should support it either).

The method were doing

$a . ' BETWEEN ' . $b

So I would expect that both

'1'. ' BETWEEN ' . '2'
1. ' BETWEEN ' . 2

were considered as literal.

But looking at the RFC seems like it won't be possible :/

VincentLanglet avatar Dec 28 '22 16:12 VincentLanglet

Thanks for checking Vincent.

And yeah, it's annoying, but integers in PHP don't have the ability to add a literal flag onto them (not without major changes to the zval structure).

craigfrancis avatar Dec 28 '22 16:12 craigfrancis

Fixed by https://github.com/phpstan/phpstan-doctrine/pull/407

ondrejmirtes avatar Dec 30 '22 21:12 ondrejmirtes

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Jan 31 '23 00:01 github-actions[bot]