Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error messages for type callable #8039

Closed
dg opened this issue Feb 4, 2022 · 6 comments
Closed

Better error messages for type callable #8039

dg opened this issue Feb 4, 2022 · 6 comments

Comments

@dg
Copy link

dg commented Feb 4, 2022

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.

@cmb69
Copy link
Member

cmb69 commented Feb 6, 2022

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
Copy link
Member

cmb69 commented May 16, 2022

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

@nikiromagnoli
Copy link

nikiromagnoli commented Jun 16, 2022

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.

@bohwaz
Copy link
Contributor

bohwaz commented Jul 27, 2022

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

@gkemarskii
Copy link

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

@nikiromagnoli
Copy link

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

dg added a commit to nette/tracy that referenced this issue Jan 3, 2024
dg added a commit to nette/tracy that referenced this issue Jan 4, 2024
dg added a commit to nette/tracy that referenced this issue Mar 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants