Better error messages for type callable
Description
I deliberately avoid the callable type in my code because it produces misleading error messages.
Examples:
$cb = [PhpTokn::class, 'tokenize']; // typo in class name
(fn(callable $cb) => 0)($cb);
// expected error: Argument #1 ($cb) must be of type callable, but class "PhpTokn" does not exist
// actual error: Argument #1 ($cb) must be of type callable, array given
$cb = [PhpToken::class, 'tokenze']; // typo in method name
(fn(callable $cb) => 0)($cb);
// expected error: Argument #1 ($cb) must be of type callable, but method PhpToken::tokenze() does not exist
// actual error: Argument #1 ($cb) must be of type callable, array given
$cb = 'PhpTokn::tokenize'; // typo in class name
(fn(callable $cb) => 0)($cb);
// expected error: Argument #1 ($cb) must be of type callable, but class "PhpTokn" does not exist
// actual error: Argument #1 ($cb) must be of type callable, string given
$cb = 'PhpToken::tokenze'; // typo in method name
(fn(callable $cb) => 0)($cb);
// expected error: Argument #1 ($cb) must be of type callable, but method PhpToken::tokenze() does not exist
// actual error: Argument #1 ($cb) must be of type callable, string given
Not only does the error message not tell you exactly what is wrong with the callback, but it is also little misleading. Message Must be callable, array given suggests, that something other than array (or string) was expected.
Thanks for your consideration.
I'm not sure whether it's worth to try to improve this; new code should preferably use the first-class callable syntax, which gives the desired information. And in the long run, the callable syntax should be deprecated, in my opinion.
Since there have been no further comments, I assume this is a WONTFIX.
I just ran into this with Argument #1 ($func) must be of type callable, string given and I confirm that this is very misleading. Just like the OP stated, I was trying to side-call imagecreatefrompng but had not GD module enabled. As you can see, the error message was just pointing me completely elsewhere.
That callable will be one day deprecated makes sense, but I think that till then a little of effort to adjust this message might be desirable IMHO.
I'm experienced enough to stubborn on this message and think "What? A string IS a callable" and further investigate (here I am by the way), but a junior would just get confused and wander doubting of what he/she just learned.
Same issue here, I ran into this error a few times and it is quite misleading.
The same situation. Lost an hour looking in the wrong direction.
By the way, mistyping the function name in the string is enough to experience this.