szepeviktor/phpstan-wordpress

array_map callable not accepted

MocioF opened this issue · 5 comments

Writing a class for my plugin, I used this code to sanitize input:

$new_input['endpoints'] = array_map( 'esc_url_raw', $input['endpoints'], $protocols = array( 'https' ) );

where $input['endpoints'] is an array.

PHPStan seems not to see esc_url_raw as a callable function, and says:

Parameter #1 $callback of function array_map expects (callable(string, 'https'): mixed)|null, 'esc_url_raw' given.

I am not sure if this is related to stubs, or it is a syntax problem.

Hello @MocioF! 👋
Thank you for this issue.

PHPStan is an tool for OOP. It does not recognize a global function as a callable.

No! I was wrong. Recently it does support that.
https://phpstan.org/r/2d4c20b6-4b7f-4511-a837-18127e64d7f3
What version of PHPStan do you have?

Got it!

array_map( 'esc_url_raw', $input['endpoints'], $protocols = array( array( 'https' ) ) )

...actually there must be as many array( 'https' )-s as endpoints.

I will try, but I am not sure the problem is this one.
https://phpstan.org/r/1921d0e3-e3b7-4d9e-8e96-968a9638f2b2

The problem in my code (a class in a wordpress plugin) is with parameter #1 of array_map.
It seems that 'esc_url_raw' is not seen as a callable.

I got this problem in activator class; could the issue been related to this code is run before init? (I don't know exactly how phpstan discovers symbols

The cause of the problem is that esc_url_raw's second parameter is an array, not a string.
https://developer.wordpress.org/reference/functions/esc_url_raw/