Closure as a subset of callable #10968
-
I have an issue that I could reproduce with the following snippet. https://phpstan.org/r/06e3c164-f67b-42b2-8e81-9178a6b0a358
Since all closures are subtype of callable why is PHPStan complaining ? 🤔
If the parameter is optional why complains if the parameter is provided ? 🤔 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
PHPStan is only complaining because of the reason it gives you:
If you fix that it's okay to pass The reason why 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: https://phpstan.org/r/3e8f88d5-a483-4d67-b2b6-6d9208085b21 |
Beta Was this translation helpful? Give feedback.
PHPStan is only complaining because of the reason it gives you:
If you fix that it's okay to pass
Closure
intocallable
: https://phpstan.org/r/1e938868-d00d-4246-87d3-c8dcde692b96The reason why
Closure(array, int|string): true
is not accepted intocallable(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…