From 31b7ea556e9e3ea8acec6ed1e8b801c34f85cdc1 Mon Sep 17 00:00:00 2001 From: IanDelMar <42134098+IanDelMar@users.noreply.github.com> Date: Sun, 25 Aug 2024 04:58:58 +0200 Subject: [PATCH] Add cond. return type and type specifier for wp_is_numeric_array() (#203) --- functionMap.php | 1 + tests/TypeInferenceTest.php | 1 + tests/data/wp_is_numeric_array.php | 69 ++++++++++++++++++++++++++++++ wordpress-stubs.php | 4 ++ 4 files changed, 75 insertions(+) create mode 100644 tests/data/wp_is_numeric_array.php diff --git a/functionMap.php b/functionMap.php index 83e2ee5..49d6fca 100644 --- a/functionMap.php +++ b/functionMap.php @@ -52,6 +52,7 @@ 'WP_Http::head' => [$httpReturnType], 'WP_Http::post' => [$httpReturnType], 'WP_Http::request' => [$httpReturnType], + 'wp_is_numeric_array' => ['(T is array ? (key-of is int ? true : false) : false)', '@template' => 'T of mixed', 'data' => 'T', '@phpstan-assert-if-true' => '(T is list ? T : array>) $data'], 'wp_list_bookmarks' => ['($args is array{echo: false|0} ? string : void)'], 'wp_list_categories' => ['($args is array{echo: false|0} ? string|false : false|void)'], 'wp_list_pages' => ['($args is array{echo: false} ? string : void)'], diff --git a/tests/TypeInferenceTest.php b/tests/TypeInferenceTest.php index 168eeeb..cf78f4e 100644 --- a/tests/TypeInferenceTest.php +++ b/tests/TypeInferenceTest.php @@ -37,6 +37,7 @@ public function dataFileAsserts(): iterable yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_dropdown_languages.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_error_parameter.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_get_archives.php'); + yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_is_numeric_array.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_list_bookmarks.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_list_categories.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_list_pages.php'); diff --git a/tests/data/wp_is_numeric_array.php b/tests/data/wp_is_numeric_array.php new file mode 100644 index 0000000..4c72bac --- /dev/null +++ b/tests/data/wp_is_numeric_array.php @@ -0,0 +1,69 @@ + $value */ +$value = $_GET['value']; +assertType('true', wp_is_numeric_array($value)); + +/** @var array $value */ +$value = $_GET['value']; +assertType('true', wp_is_numeric_array($value)); + +/** @var array $value */ +$value = $_GET['value']; +assertType('false', wp_is_numeric_array($value)); + +/** @var array $value */ +$value = $_GET['value']; +assertType('bool', wp_is_numeric_array($value)); + +/** @var array $value */ +$value = $_GET['value']; +assertType('bool', wp_is_numeric_array($value)); + +/** @var array $value */ +$value = $_GET['value']; +assertType('bool', wp_is_numeric_array($value)); + +/** @var array{'key1': 'value1', 'key2': 'value2'} $value */ +$value = $_GET['value']; +assertType('false', wp_is_numeric_array($value)); + +/** @var array{0: 'value0', 1: 'value1'} $value */ +$value = $_GET['value']; +assertType('true', wp_is_numeric_array($value)); + +/** @var array{0: 'value0', key1: 'value1'} $value */ +$value = $_GET['value']; +assertType('bool', wp_is_numeric_array($value)); + +assertType('bool', wp_is_numeric_array($_GET['value'])); + +/** @var list|string */ +$value = $_GET['value']; +if (wp_is_numeric_array($value)) { + assertType('list', $value); +} + +/** @var array|string */ +$value = $_GET['value']; +if (wp_is_numeric_array($value)) { + assertType('array', $value); +} + +if (wp_is_numeric_array($_GET['value'])) { + assertType('array', $_GET['value']); +} diff --git a/wordpress-stubs.php b/wordpress-stubs.php index e59c07e..d6e08ea 100644 --- a/wordpress-stubs.php +++ b/wordpress-stubs.php @@ -114475,6 +114475,10 @@ function _wp_to_kebab_case($input_string) * * @param mixed $data Variable to check. * @return bool Whether the variable is a list. + * @template T of mixed + * @phpstan-param T $data + * @phpstan-assert-if-true (T is list ? T : array>) $data + * @phpstan-return (T is array ? (key-of is int ? true : false) : false) */ function wp_is_numeric_array($data) {