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

False positive when slashing data for wp_insert_post() and wp_update_post() #54

Open
Chouby opened this issue Oct 4, 2022 · 7 comments

Comments

@Chouby
Copy link

Chouby commented Oct 4, 2022

wp_insert_post() is expecting slashed data. So I'm used to prepare data passed to wp_insert_post() and wp_update_post() with wp_slash().
Here is a small sample to demonstrate the issue:

$post = get_post( $post_id, ARRAY_A );
$post['post_status'] = 'draft';
wp_update_post( wp_slash( $post ) );

Since the version 6.0.2 (I suppose since #49), PHPStan reports the following error on:

Parameter #1 $postarr of function wp_update_post expects
  array{ID?: int, post_author?: int, post_date?: string, post_date_gmt?: string, post_content?: string, post_content_filtered?: string, post_title?: string, post_excerpt?: string, ...},
  array<string, string> given.

If I remove wp_slash(), PHPstan would report nothing.

@szepeviktor
Copy link
Member

Hello @Chouby! 👋🏻
I'm glad that you've opened this issue.

There are many options open.

  • do not use WordPress 🙃
  • add another sed command to this repo (PHP generics for wp_slash: output=input)
  • add a new class to phpstan-wordpress

@szepeviktor
Copy link
Member

Oh no! We already have PHP generics

     * @param string|array $value String or array of data to slash.
     * @return string|array Slashed `$value`.
     * @phpstan-template T
     * @phpstan-param T $value
     * @phpstan-return T
     */
    function wp_slash($value)

@szepeviktor
Copy link
Member

szepeviktor commented Oct 4, 2022

Actually I do not know why arrays don't keep their shape.
Please ask at phpstan/phpstan.

@szepeviktor
Copy link
Member

szepeviktor commented Oct 4, 2022

@ondrejmirtes Could you help why PHP generics does not keep array shapes?
Please don't hate WordPress. We hate it for you. :)

@ondrejmirtes
Copy link

Please reproduce the problem on phpstan.org/try and open a discussion in PHPStan's GitHub, thanks.

@szepeviktor
Copy link
Member

@ondrejmirtes Thank you.

@szepeviktor
Copy link
Member

@Chouby Please temporarily edit WP stubs for wp_slash: @phpstan-template T of string|array

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants