From 7554fbf9c9158daa4e837c32e25a726dc94b844c Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Thu, 30 Jun 2022 00:54:28 +0100 Subject: [PATCH 1/2] Add support for specifying complete tag names in the node visitor. --- visitor.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/visitor.php b/visitor.php index 1c2a0953..e6fac7ab 100644 --- a/visitor.php +++ b/visitor.php @@ -156,6 +156,15 @@ private function addAdditionalParams(Doc $docComment): ?Doc $additions = []; foreach ($parameters as $paramName => $paramType) { + if (strpos($paramName, '@') === 0) { + $additions[] = sprintf( + '%s %s', + $paramName, + $paramType + ); + continue; + } + $additions[] = sprintf( '@phpstan-param %s $%s', $paramType, From 07ff97688c19dd30bdb1c895cde8d0efa2896bed Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Thu, 30 Jun 2022 00:56:28 +0100 Subject: [PATCH 2/2] Add PHPStan generics declarations. --- functionMap.php | 10 ++++++++++ wordpress-stubs.php | 30 ++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/functionMap.php b/functionMap.php index 12810106..23cca734 100644 --- a/functionMap.php +++ b/functionMap.php @@ -11,7 +11,15 @@ */ return [ 'add_meta_box' => ['void', 'context'=>'"normal"|"side"|"advanced"', 'priority'=>'"high"|"core"|"default"|"low"'], + 'addslashes_gpc' => ['T', '@phpstan-template'=>'T', 'gpc'=>'T'], + 'rawurlencode_deep' => ['T', '@phpstan-template'=>'T', 'value'=>'T'], 'remove_meta_box' => ['void', 'context'=>'"normal"|"side"|"advanced"'], + 'sanitize_category' => ['T', '@phpstan-template'=>'T of array|object', 'category'=>'T'], + 'sanitize_post' => ['T', '@phpstan-template'=>'T of array|object', 'post'=>'T'], + 'sanitize_term' => ['T', '@phpstan-template'=>'T of array|object', 'term'=>'T'], + 'stripslashes_deep' => ['T', '@phpstan-template'=>'T', 'value'=>'T'], + 'urldecode_deep' => ['T', '@phpstan-template'=>'T', 'value'=>'T'], + 'urlencode_deep' => ['T', '@phpstan-template'=>'T', 'value'=>'T'], 'WP_Http::get' => [$httpReturnType], 'WP_Http::head' => [$httpReturnType], 'WP_Http::post' => [$httpReturnType], @@ -27,4 +35,6 @@ 'wp_safe_remote_head' => [$httpReturnType], 'wp_safe_remote_post' => [$httpReturnType], 'wp_safe_remote_request' => [$httpReturnType], + 'wp_slash' => ['T', '@phpstan-template'=>'T', 'value'=>'T'], + 'wp_unslash' => ['T', '@phpstan-template'=>'T', 'value'=>'T'], ]; diff --git a/wordpress-stubs.php b/wordpress-stubs.php index 1fe0eb43..a38d4e2d 100644 --- a/wordpress-stubs.php +++ b/wordpress-stubs.php @@ -91360,6 +91360,9 @@ function cat_is_ancestor_of($cat1, $cat2) * @param object|array $category Category data. * @param string $context Optional. Default 'display'. * @return object|array Same type as $category with sanitized data for safe use. + * @phpstan-template T of array|object + * @phpstan-param T $category + * @phpstan-return T */ function sanitize_category($category, $context = 'display') { @@ -98902,6 +98905,9 @@ function untrailingslashit($string) * * @param string|array $gpc String or array of data to slash. * @return string|array Slashed `$gpc`. + * @phpstan-template T + * @phpstan-param T $gpc + * @phpstan-return T */ function addslashes_gpc($gpc) { @@ -98913,6 +98919,9 @@ function addslashes_gpc($gpc) * * @param mixed $value The value to be stripped. * @return mixed Stripped value. + * @phpstan-template T + * @phpstan-param T $value + * @phpstan-return T */ function stripslashes_deep($value) { @@ -98935,6 +98944,9 @@ function stripslashes_from_strings_only($value) * * @param mixed $value The array or string to be encoded. * @return mixed The encoded value. + * @phpstan-template T + * @phpstan-param T $value + * @phpstan-return T */ function urlencode_deep($value) { @@ -98946,6 +98958,9 @@ function urlencode_deep($value) * * @param mixed $value The array or string to be encoded. * @return mixed The encoded value. + * @phpstan-template T + * @phpstan-param T $value + * @phpstan-return T */ function rawurlencode_deep($value) { @@ -98957,6 +98972,9 @@ function rawurlencode_deep($value) * * @param mixed $value The array or string to be decoded. * @return mixed The decoded value. + * @phpstan-template T + * @phpstan-param T $value + * @phpstan-return T */ function urldecode_deep($value) { @@ -99897,6 +99915,9 @@ function sanitize_trackback_urls($to_ping) * * @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) { @@ -99911,6 +99932,9 @@ function wp_slash($value) * * @param string|array $value String or array of data to unslash. * @return string|array Unslashed `$value`. + * @phpstan-template T + * @phpstan-param T $value + * @phpstan-return T */ function wp_unslash($value) { @@ -119480,6 +119504,9 @@ function is_sticky($post_id = 0) * 'attribute', or 'js'. Default 'display'. * @return object|WP_Post|array The now sanitized post object or array (will be the * same type as `$post`). + * @phpstan-template T of array|object + * @phpstan-param T $post + * @phpstan-return T */ function sanitize_post($post, $context = 'display') { @@ -125323,6 +125350,9 @@ function term_is_ancestor_of($term1, $term2, $taxonomy) * Accepts 'raw', 'edit', 'db', 'display', 'rss', * 'attribute', or 'js'. Default 'display'. * @return array|object Term with all fields sanitized. + * @phpstan-template T of array|object + * @phpstan-param T $term + * @phpstan-return T */ function sanitize_term($term, $taxonomy, $context = 'display') {