Skip to content

Better error messages for type callable #8039

@dg

Description

@dg

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions