php-src icon indicating copy to clipboard operation
php-src copied to clipboard

Better error messages for type callable

Open dg opened this issue 4 years ago • 6 comments

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.

dg avatar Feb 04 '22 22:02 dg

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.

cmb69 avatar Feb 06 '22 11:02 cmb69

Since there have been no further comments, I assume this is a WONTFIX.

cmb69 avatar May 16 '22 15:05 cmb69

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.

nikiromagnoli avatar Jun 16 '22 14:06 nikiromagnoli

Same issue here, I ran into this error a few times and it is quite misleading.

bohwaz avatar Jul 27 '22 20:07 bohwaz

The same situation. Lost an hour looking in the wrong direction.

gkemarskii avatar Apr 11 '23 13:04 gkemarskii

By the way, mistyping the function name in the string is enough to experience this.

nikiromagnoli avatar Dec 28 '23 11:12 nikiromagnoli