diff --git a/functionMap.php b/functionMap.php index 1692e4f..d352543 100644 --- a/functionMap.php +++ b/functionMap.php @@ -70,13 +70,13 @@ 'rest_ensure_response' => ['($response is WP_Error ? WP_Error : WP_REST_Response)'], 'WP_REST_Request' => [null, '@phpstan-template' => 'T of array', '@phpstan-implements' => 'ArrayAccess, value-of>'], 'WP_REST_Request::offsetExists' => [null, 'offset' => 'key-of'], - 'WP_REST_Request::offsetGet' => ['T[TOffset]', '@phpstan-template' => 'TOffset of key-of', 'offset' => 'TOffset'], + 'WP_REST_Request::offsetGet' => ['($offset is TOffset ? T[TOffset] : null)', '@phpstan-template' => 'TOffset of key-of', 'offset' => 'TOffset'], 'WP_REST_Request::offsetSet' => ['void', '@phpstan-template' => 'TOffset of key-of', 'offset' => 'TOffset', 'value' => 'T[TOffset]'], 'WP_REST_Request::offsetUnset' => ['void', '@phpstan-template' => 'TOffset of key-of', 'offset' => 'TOffset'], - 'WP_REST_Request::get_param' => ['T[TOffset]', '@phpstan-template' => 'TOffset of key-of', 'key' => 'TOffset'], + 'WP_REST_Request::get_param' => ['($key is TOffset ? T[TOffset] : null)', '@phpstan-template' => 'TOffset of key-of', 'key' => 'TOffset'], 'WP_REST_Request::get_params' => ['T'], 'WP_REST_Request::set_param' => ['void', '@phpstan-template' => 'TOffset of key-of', 'key' => 'TOffset', 'value' => 'T[TOffset]'], - 'WP_REST_Request::has_param' => [null, 'offset' => 'key-of'], + 'WP_REST_Request::has_param' => [null, 'key' => 'key-of'], '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'"], '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)"], 'WP_Theme::offsetExists' => ['($offset is ThemeKey ? true : false)'], diff --git a/tests/data/wp_rest_request.php b/tests/data/wp_rest_request.php index 734b601..119925b 100644 --- a/tests/data/wp_rest_request.php +++ b/tests/data/wp_rest_request.php @@ -6,6 +6,19 @@ use function PHPStan\Testing\assertType; +/** + * @var \WP_REST_Request $request + */ +$request = new WP_REST_Request(); + +assertType('mixed', $request->get_param('maybeParam')); + +assertType('mixed', $request['maybeParam']); + +assertType('array', $request->get_params()); + +assertType('bool', $request->has_param('maybeParam')); + /** * @var \WP_REST_Requestget_param('stringParam')); assertType('int', $request->get_param('intParam')); assertType('bool', $request->get_param('boolParam')); -assertType('null', $request->get_param('unknownParam')); +assertType('null', $request->get_param('nonExistentParam')); assertType('string', $request['stringParam']); assertType('int', $request['intParam']); assertType('bool', $request['boolParam']); -assertType('null', $request['unknownParam']); +assertType('null', $request['nonExistentParam']); assertType('array{stringParam: string, intParam: int, boolParam: bool}', $request->get_params()); assertType('bool', $request->has_param('stringParam')); assertType('bool', $request->has_param('intParam')); assertType('bool', $request->has_param('boolParam')); -assertType('bool', $request->has_param('unknownParam')); +assertType('bool', $request->has_param('nonExistentParam')); diff --git a/wordpress-stubs.php b/wordpress-stubs.php index adfba41..da47897 100644 --- a/wordpress-stubs.php +++ b/wordpress-stubs.php @@ -68946,7 +68946,7 @@ protected function get_parameter_order() * @return mixed|null Value if set, null otherwise. * @phpstan-template TOffset of key-of * @phpstan-param TOffset $key - * @phpstan-return T[TOffset] + * @phpstan-return ($key is TOffset ? T[TOffset] : null) */ public function get_param($key) { @@ -68961,7 +68961,7 @@ public function get_param($key) * * @param string $key Parameter name. * @return bool True if a param exists for the given key. - * @phpstan-param key-of $offset + * @phpstan-param key-of $key */ public function has_param($key) { @@ -69261,7 +69261,7 @@ public function offsetExists($offset) * @return mixed|null Value if set, null otherwise. * @phpstan-template TOffset of key-of * @phpstan-param TOffset $offset - * @phpstan-return T[TOffset] + * @phpstan-return ($offset is TOffset ? T[TOffset] : null) */ #[\ReturnTypeWillChange] public function offsetGet($offset)