phpstan-wordpress icon indicating copy to clipboard operation
phpstan-wordpress copied to clipboard

Named arguments break add_filter/add_action callback check

Open marijnvdwerf opened this issue 2 years ago • 3 comments

Reproduce:

add_filter('filter', function($one, $two) {
    return 123;
}, accepted_args: 2);

Expected: No errors

Actual

Callback expects 2 parameters, $accepted_args is set to 1.

marijnvdwerf avatar Jun 24 '23 12:06 marijnvdwerf

Hello @marijnvdwerf! 👋🏻

Thank you for your report. Yes, this feature of PHP is not supported.

If you take a look at the WordPress ecosystem (not core only), you see that WordPress installations should run on PHP 7.4. Maybe next year it will be 8.0 (or 8.1) compatible. Core stats show this: https://wordpress.org/about/stats/#php_versions

szepeviktor avatar Jun 24 '23 12:06 szepeviktor

A second thought. Explicit is better than implicit.

add_filter(
    'filter',
    function ($one, $two) {
        return 123;
    },
    10,
    2
);

source

szepeviktor avatar Jun 24 '23 12:06 szepeviktor

A third thought!

/**
 * @hook filter
 */
public function filterHook($one, $two)
{

source

szepeviktor avatar Jun 24 '23 12:06 szepeviktor