Skip to content

Commit 493acc0

Browse files
authored
Implement generics (#37)
* Add support for specifying complete tag names in the node visitor. * Add PHPStan generics declarations.
1 parent 19e5c8f commit 493acc0

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

functionMap.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@
1111
*/
1212
return [
1313
'add_meta_box' => ['void', 'context'=>'"normal"|"side"|"advanced"', 'priority'=>'"high"|"core"|"default"|"low"'],
14+
'addslashes_gpc' => ['T', '@phpstan-template'=>'T', 'gpc'=>'T'],
15+
'rawurlencode_deep' => ['T', '@phpstan-template'=>'T', 'value'=>'T'],
1416
'remove_meta_box' => ['void', 'context'=>'"normal"|"side"|"advanced"'],
17+
'sanitize_category' => ['T', '@phpstan-template'=>'T of array|object', 'category'=>'T'],
18+
'sanitize_post' => ['T', '@phpstan-template'=>'T of array|object', 'post'=>'T'],
19+
'sanitize_term' => ['T', '@phpstan-template'=>'T of array|object', 'term'=>'T'],
20+
'stripslashes_deep' => ['T', '@phpstan-template'=>'T', 'value'=>'T'],
21+
'urldecode_deep' => ['T', '@phpstan-template'=>'T', 'value'=>'T'],
22+
'urlencode_deep' => ['T', '@phpstan-template'=>'T', 'value'=>'T'],
1523
'WP_Http::get' => [$httpReturnType],
1624
'WP_Http::head' => [$httpReturnType],
1725
'WP_Http::post' => [$httpReturnType],
@@ -27,4 +35,6 @@
2735
'wp_safe_remote_head' => [$httpReturnType],
2836
'wp_safe_remote_post' => [$httpReturnType],
2937
'wp_safe_remote_request' => [$httpReturnType],
38+
'wp_slash' => ['T', '@phpstan-template'=>'T', 'value'=>'T'],
39+
'wp_unslash' => ['T', '@phpstan-template'=>'T', 'value'=>'T'],
3040
];

visitor.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,15 @@ private function addAdditionalParams(Doc $docComment): ?Doc
156156
$additions = [];
157157

158158
foreach ($parameters as $paramName => $paramType) {
159+
if (strpos($paramName, '@') === 0) {
160+
$additions[] = sprintf(
161+
'%s %s',
162+
$paramName,
163+
$paramType
164+
);
165+
continue;
166+
}
167+
159168
$additions[] = sprintf(
160169
'@phpstan-param %s $%s',
161170
$paramType,

wordpress-stubs.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91360,6 +91360,9 @@ function cat_is_ancestor_of($cat1, $cat2)
9136091360
* @param object|array $category Category data.
9136191361
* @param string $context Optional. Default 'display'.
9136291362
* @return object|array Same type as $category with sanitized data for safe use.
91363+
* @phpstan-template T of array|object
91364+
* @phpstan-param T $category
91365+
* @phpstan-return T
9136391366
*/
9136491367
function sanitize_category($category, $context = 'display')
9136591368
{
@@ -98902,6 +98905,9 @@ function untrailingslashit($string)
9890298905
*
9890398906
* @param string|array $gpc String or array of data to slash.
9890498907
* @return string|array Slashed `$gpc`.
98908+
* @phpstan-template T
98909+
* @phpstan-param T $gpc
98910+
* @phpstan-return T
9890598911
*/
9890698912
function addslashes_gpc($gpc)
9890798913
{
@@ -98913,6 +98919,9 @@ function addslashes_gpc($gpc)
9891398919
*
9891498920
* @param mixed $value The value to be stripped.
9891598921
* @return mixed Stripped value.
98922+
* @phpstan-template T
98923+
* @phpstan-param T $value
98924+
* @phpstan-return T
9891698925
*/
9891798926
function stripslashes_deep($value)
9891898927
{
@@ -98935,6 +98944,9 @@ function stripslashes_from_strings_only($value)
9893598944
*
9893698945
* @param mixed $value The array or string to be encoded.
9893798946
* @return mixed The encoded value.
98947+
* @phpstan-template T
98948+
* @phpstan-param T $value
98949+
* @phpstan-return T
9893898950
*/
9893998951
function urlencode_deep($value)
9894098952
{
@@ -98946,6 +98958,9 @@ function urlencode_deep($value)
9894698958
*
9894798959
* @param mixed $value The array or string to be encoded.
9894898960
* @return mixed The encoded value.
98961+
* @phpstan-template T
98962+
* @phpstan-param T $value
98963+
* @phpstan-return T
9894998964
*/
9895098965
function rawurlencode_deep($value)
9895198966
{
@@ -98957,6 +98972,9 @@ function rawurlencode_deep($value)
9895798972
*
9895898973
* @param mixed $value The array or string to be decoded.
9895998974
* @return mixed The decoded value.
98975+
* @phpstan-template T
98976+
* @phpstan-param T $value
98977+
* @phpstan-return T
9896098978
*/
9896198979
function urldecode_deep($value)
9896298980
{
@@ -99897,6 +99915,9 @@ function sanitize_trackback_urls($to_ping)
9989799915
*
9989899916
* @param string|array $value String or array of data to slash.
9989999917
* @return string|array Slashed `$value`.
99918+
* @phpstan-template T
99919+
* @phpstan-param T $value
99920+
* @phpstan-return T
9990099921
*/
9990199922
function wp_slash($value)
9990299923
{
@@ -99911,6 +99932,9 @@ function wp_slash($value)
9991199932
*
9991299933
* @param string|array $value String or array of data to unslash.
9991399934
* @return string|array Unslashed `$value`.
99935+
* @phpstan-template T
99936+
* @phpstan-param T $value
99937+
* @phpstan-return T
9991499938
*/
9991599939
function wp_unslash($value)
9991699940
{
@@ -119480,6 +119504,9 @@ function is_sticky($post_id = 0)
119480119504
* 'attribute', or 'js'. Default 'display'.
119481119505
* @return object|WP_Post|array The now sanitized post object or array (will be the
119482119506
* same type as `$post`).
119507+
* @phpstan-template T of array|object
119508+
* @phpstan-param T $post
119509+
* @phpstan-return T
119483119510
*/
119484119511
function sanitize_post($post, $context = 'display')
119485119512
{
@@ -125323,6 +125350,9 @@ function term_is_ancestor_of($term1, $term2, $taxonomy)
125323125350
* Accepts 'raw', 'edit', 'db', 'display', 'rss',
125324125351
* 'attribute', or 'js'. Default 'display'.
125325125352
* @return array|object Term with all fields sanitized.
125353+
* @phpstan-template T of array|object
125354+
* @phpstan-param T $term
125355+
* @phpstan-return T
125326125356
*/
125327125357
function sanitize_term($term, $taxonomy, $context = 'display')
125328125358
{

0 commit comments

Comments
 (0)