szepeviktor / phpstan-wordpress

WordPress extensions for PHPStan ⛏️
https://packagist.org/packages/szepeviktor/phpstan-wordpress
MIT License
262 stars 26 forks source link

Misunderstanding of wp_kses() documentation #214

Closed manooweb closed 6 months ago

manooweb commented 6 months ago

wp_kses() $allowed_html second parameter is documented as array[]|string https://github.com/WordPress/wordpress-develop/blob/6.4/src/wp-includes/kses.php#L740

With the code below, it seems ok for us

<?php
$message = wp_kses(
    __( 'something goes wrong with wp_kses documentation.', 'mydomain' ),
    array(
        'a' => array( 'href' ),
        'br',
        'code',
        'em',
    )
);

but PHPStan returns

 ------ ---------------------------------------------------------------------------------------------------------------------------------
  :4     Parameter #2 $allowed_html of function wp_kses expects array<array>|string, array<int|string, array<int, string>|string> given.
 ------ ---------------------------------------------------------------------------------------------------------------------------------

With the other one

<?php
$message = wp_kses(
    __( 'something goes wrong with wp_kses documentation.', 'mydomain' ),
    array(
        'a'    => array( 'href' => true ),
        'br'   => true,
        'code' => true,
        'em'   => true,
    )
);

PHPStan returns

 ------ ----------------------------------------------------------------------------------------------------------------------------
  :4     Parameter #2 $allowed_html of function wp_kses expects array<array>|string, array<string, array<string, bool>|true> given.
 ------ ----------------------------------------------------------------------------------------------------------------------------

Is there something we are missing?

Thanks.

szepeviktor commented 6 months ago

Hello! PHPStan wants you to provide array[]-s only or strings only. But WordPress is liberal.

manooweb commented 6 months ago

Hello! PHPStan wants you to provide array[]-s only or strings only. But WordPress is liberal.

I was inspired by https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/functions.php#L6066-L6076 😑😁

<?php
$message = wp_kses(
    __( 'something goes wrong with wp_kses documentation.', 'mydomain' ),
    array(
        'a'    => array( 'href' => true ),
        'br'   => array(),
        'code' => array(),
        'em'   => array(),
    )
);

works perfectly.

Thanks