Skip to content

Conversation

@vudaltsov
Copy link
Contributor

@vudaltsov
Copy link
Contributor Author

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

@trowski
Copy link
Member

trowski commented May 1, 2025

I originally removed this syntax to support PHPStan: 138801f

So this has now changed?

@vudaltsov
Copy link
Contributor Author

🤦🤦🤦

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
Copy link
Contributor Author

vudaltsov commented May 2, 2025

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: phpstan/phpstan#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

You cannot pass a Closure with narrower parameters to a Closure with
wider parameters, since parameters are contravariant.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants