Skip to content

Closure as a subset of callable #10968

Closed Answered by ondrejmirtes
nyamsprod asked this question in Support
Discussion options

You must be logged in to vote

PHPStan is only complaining because of the reason it gives you:

Parameter #2 $key of passed callable is required but the parameter of accepting callable is optional. It might be called without it.

If you fix that it's okay to pass Closure into callable: https://phpstan.org/r/1e938868-d00d-4246-87d3-c8dcde692b96

The reason why Closure(array, int|string): true is not accepted into callable(array, int|string=): bool is because the callable might be called without the 2nd parameter.

If you call this callback without the 2nd parameter (which is okay based on the signature), PHP is going to crash: https://3v4l.org/lYvts

That's why PHPStan complains on line 16 and not line 9 in this example: h…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by nyamsprod
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Support
Labels
None yet
2 participants