Skip to content

Commit 8b6e534

Browse files
authored
Fix parameter name for WP_REST_Request::has_param() in functionMap (#188)
1 parent 8f47cd1 commit 8b6e534

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

functionMap.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@
7070
'rest_ensure_response' => ['($response is WP_Error ? WP_Error : WP_REST_Response)'],
7171
'WP_REST_Request' => [null, '@phpstan-template' => 'T of array', '@phpstan-implements' => 'ArrayAccess<key-of<T>, value-of<T>>'],
7272
'WP_REST_Request::offsetExists' => [null, 'offset' => 'key-of<T>'],
73-
'WP_REST_Request::offsetGet' => ['T[TOffset]', '@phpstan-template' => 'TOffset of key-of<T>', 'offset' => 'TOffset'],
73+
'WP_REST_Request::offsetGet' => ['($offset is TOffset ? T[TOffset] : null)', '@phpstan-template' => 'TOffset of key-of<T>', 'offset' => 'TOffset'],
7474
'WP_REST_Request::offsetSet' => ['void', '@phpstan-template' => 'TOffset of key-of<T>', 'offset' => 'TOffset', 'value' => 'T[TOffset]'],
7575
'WP_REST_Request::offsetUnset' => ['void', '@phpstan-template' => 'TOffset of key-of<T>', 'offset' => 'TOffset'],
76-
'WP_REST_Request::get_param' => ['T[TOffset]', '@phpstan-template' => 'TOffset of key-of<T>', 'key' => 'TOffset'],
76+
'WP_REST_Request::get_param' => ['($key is TOffset ? T[TOffset] : null)', '@phpstan-template' => 'TOffset of key-of<T>', 'key' => 'TOffset'],
7777
'WP_REST_Request::get_params' => ['T'],
7878
'WP_REST_Request::set_param' => ['void', '@phpstan-template' => 'TOffset of key-of<T>', 'key' => 'TOffset', 'value' => 'T[TOffset]'],
79-
'WP_REST_Request::has_param' => [null, 'offset' => 'key-of<T>'],
79+
'WP_REST_Request::has_param' => [null, 'key' => 'key-of<T>'],
8080
'WP_Theme' => [null, '@phpstan-type' => "ThemeKey 'Name'|'Version'|'Status'|'Title'|'Author'|'Author Name'|'Author URI'|'Description'|'Template'|'Stylesheet'|'Template Files'|'Stylesheet Files'|'Template Dir'|'Stylesheet Dir'|'Screenshot'|'Tags'|'Theme Root'|'Theme Root URI'|'Parent Theme'"],
8181
'WP_Theme::get' => ["(\$header is 'Name'|'ThemeURI'|'Description'|'Author'|'AuthorURI'|'Version'|'Template'|'Status'|'Tags'|'TextDomain'|'DomainPath'|'RequiresWP'|'RequiresPHP'|'UpdateURI' ? (\$header is 'Tags' ? string[] : string) : false)"],
8282
'WP_Theme::offsetExists' => ['($offset is ThemeKey ? true : false)'],

tests/data/wp_rest_request.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@
66

77
use function PHPStan\Testing\assertType;
88

9+
/**
10+
* @var \WP_REST_Request<array> $request
11+
*/
12+
$request = new WP_REST_Request();
13+
14+
assertType('mixed', $request->get_param('maybeParam'));
15+
16+
assertType('mixed', $request['maybeParam']);
17+
18+
assertType('array', $request->get_params());
19+
20+
assertType('bool', $request->has_param('maybeParam'));
21+
922
/**
1023
* @var \WP_REST_Request<array{
1124
* stringParam: string,
@@ -18,16 +31,16 @@
1831
assertType('string', $request->get_param('stringParam'));
1932
assertType('int', $request->get_param('intParam'));
2033
assertType('bool', $request->get_param('boolParam'));
21-
assertType('null', $request->get_param('unknownParam'));
34+
assertType('null', $request->get_param('nonExistentParam'));
2235

2336
assertType('string', $request['stringParam']);
2437
assertType('int', $request['intParam']);
2538
assertType('bool', $request['boolParam']);
26-
assertType('null', $request['unknownParam']);
39+
assertType('null', $request['nonExistentParam']);
2740

2841
assertType('array{stringParam: string, intParam: int, boolParam: bool}', $request->get_params());
2942

3043
assertType('bool', $request->has_param('stringParam'));
3144
assertType('bool', $request->has_param('intParam'));
3245
assertType('bool', $request->has_param('boolParam'));
33-
assertType('bool', $request->has_param('unknownParam'));
46+
assertType('bool', $request->has_param('nonExistentParam'));

wordpress-stubs.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68946,7 +68946,7 @@ protected function get_parameter_order()
6894668946
* @return mixed|null Value if set, null otherwise.
6894768947
* @phpstan-template TOffset of key-of<T>
6894868948
* @phpstan-param TOffset $key
68949-
* @phpstan-return T[TOffset]
68949+
* @phpstan-return ($key is TOffset ? T[TOffset] : null)
6895068950
*/
6895168951
public function get_param($key)
6895268952
{
@@ -68961,7 +68961,7 @@ public function get_param($key)
6896168961
*
6896268962
* @param string $key Parameter name.
6896368963
* @return bool True if a param exists for the given key.
68964-
* @phpstan-param key-of<T> $offset
68964+
* @phpstan-param key-of<T> $key
6896568965
*/
6896668966
public function has_param($key)
6896768967
{
@@ -69261,7 +69261,7 @@ public function offsetExists($offset)
6926169261
* @return mixed|null Value if set, null otherwise.
6926269262
* @phpstan-template TOffset of key-of<T>
6926369263
* @phpstan-param TOffset $offset
69264-
* @phpstan-return T[TOffset]
69264+
* @phpstan-return ($offset is TOffset ? T[TOffset] : null)
6926569265
*/
6926669266
#[\ReturnTypeWillChange]
6926769267
public function offsetGet($offset)

0 commit comments

Comments
 (0)