amp icon indicating copy to clipboard operation
amp copied to clipboard

Replace \Closure(...) with \Closure(mixed...)

Open vudaltsov opened this issue 7 months ago • 4 comments

See https://github.com/phpstan/phpstan/issues/12954

vudaltsov avatar May 01 '25 12:05 vudaltsov

The \Closure(...) is not documented, by the way. This page only says that a type can be suffixed with ....

vudaltsov avatar May 01 '25 15:05 vudaltsov

I originally removed this syntax to support PHPStan: https://github.com/amphp/amp/commit/138801fb68cfc9c329da8a7b39d01ce7291ee4b0

So this has now changed?

trowski avatar May 01 '25 17:05 trowski

🤦🤦🤦

Well, the only thing I can tell is that with mixed... it now works in both analyzers: https://phpstan.org/r/681abd9e-ec0f-4edb-ab82-134e3be7088e https://psalm.dev/r/2dd7ca2ae0

vudaltsov avatar May 01 '25 19:05 vudaltsov

In fact my solution is wrong: https://phpstan.org/r/9c17bb5f-4087-4201-b684-72a28b0e069c .

PHPStan is correct: you cannot pass a Closure with narrower parameters to a Closure with wider parameters, since parameters are contravariant.

This is how async function would look like in TypeScript (thanks to @klimick for helping with the snippet). @ondrejmirtes is already discussing smth similar: https://github.com/phpstan/phpstan/issues/8214.

Here's the possible solution:

  • for PHPStan use a Closure with a bunch of never parameters for to indicate that Closure might have parameters with any wider types and suppress the place where it is invoked,
  • for Psalm keep the current Closure(...) syntax, because Psalm does not allow to have never parameters.

https://phpstan.org/r/4e98ea03-a863-4b39-8fcf-07cfdc2e8ea4 https://psalm.dev/r/cda9b2565b

vudaltsov avatar May 02 '25 09:05 vudaltsov