Skip to content

Commit

Permalink
Add cond. return type and type specifier for wp_is_numeric_array() (#203
Browse files Browse the repository at this point in the history
)
  • Loading branch information
IanDelMar authored Aug 25, 2024
1 parent 0ad8a2b commit 31b7ea5
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions functionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> is int ? true : false) : false)', '@template' => 'T of mixed', 'data' => 'T', '@phpstan-assert-if-true' => '(T is list ? T : array<int, value-of<T>>) $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)'],
Expand Down
1 change: 1 addition & 0 deletions tests/TypeInferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
69 changes: 69 additions & 0 deletions tests/data/wp_is_numeric_array.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace PhpStubs\WordPress\Core\Tests;

use function wp_is_numeric_array;
use function PHPStan\Testing\assertType;

assertType('false', wp_is_numeric_array((string)$_GET['thing']));
assertType('false', wp_is_numeric_array((int)$_GET['thing']));
assertType('false', wp_is_numeric_array((bool)$_GET['value']));
assertType('false', wp_is_numeric_array(null));

assertType('true', wp_is_numeric_array([]));

/** @var list<mixed> $value */
$value = $_GET['value'];
assertType('true', wp_is_numeric_array($value));

/** @var array<int, mixed> $value */
$value = $_GET['value'];
assertType('true', wp_is_numeric_array($value));

/** @var array<string, mixed> $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<int|string, mixed> $value */
$value = $_GET['value'];
assertType('bool', wp_is_numeric_array($value));

/** @var array<int|string, mixed> $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>|string */
$value = $_GET['value'];
if (wp_is_numeric_array($value)) {
assertType('list<string>', $value);
}

/** @var array<int, string>|string */
$value = $_GET['value'];
if (wp_is_numeric_array($value)) {
assertType('array<int, string>', $value);
}

if (wp_is_numeric_array($_GET['value'])) {
assertType('array<int, mixed>', $_GET['value']);
}
4 changes: 4 additions & 0 deletions wordpress-stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, value-of<T>>) $data
* @phpstan-return (T is array ? (key-of<T> is int ? true : false) : false)
*/
function wp_is_numeric_array($data)
{
Expand Down

0 comments on commit 31b7ea5

Please sign in to comment.