Skip to content

Issues with PHPStan templates and constant types #333

Description

@IanDelMar

Problem

  1. Without native types, not specifying the template type causes PHPStan to treat it as mixed. As a consequence, incorrect parameter types will not be reported. See https://phpstan.org/r/6e9ccffd-23c4-4062-a4f7-20fcb6337cfb for an example.
  2. While templates generally work well for general types, they do not for constant types as used in functionMap.php. As soon as the value of a constant type is modified (e.g. through sanitisation), its type (for constant types the value itself) no longer is equal to that (template) type. See https://phpstan.org/r/42bbacae-fb55-46dc-93d8-2067d709d7c5 for an example.

Possible solutions for issue #2

  1. Remove the function from the stubs (and optionally add it as a dynamic return type via szepeviktor/phpstan-wordpress).
  2. Generalise the return type in php-stubs/wordpress-stubs (and optionally add more complex logic as a dynamic return type via szepeviktor/phpstan-wordpress).

Affected functions

  • _wp_json_sanity_check(): I suggest removing it and not adding it to szepeviktor/phpstan-wordpress as it is a pseudo-private function anyways.
  • addslashes_gpc(), wp_slash(), wp_unslash(), stripslashes_deep(), stripslashes_from_strings_only()
  • rawurlencode_deep(), urldecode_deep(), urlencode_deep()
  • sanitize_category(), sanitize_post(), sanitize_term()

Solutions for issue #1

Specify types if any types remain unspecified after fixing issue #2.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions