Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into import-extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
IanDelMar committed Aug 25, 2024
2 parents 85dc080 + f9d99d5 commit 4597207
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 1 deletion.
4 changes: 4 additions & 0 deletions functionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
'stripslashes_deep' => ['T', '@phpstan-template' => 'T', 'value' => 'T'],
'urldecode_deep' => ['T', '@phpstan-template' => 'T', 'value' => 'T'],
'urlencode_deep' => ['T', '@phpstan-template' => 'T', 'value' => 'T'],
'wp_die' => ['($args is array{exit: false} ? void : never))'],
'wp_dropdown_languages' => ["(\$args is array{id: null|''} ? void : (\$args is array{name: null|''} ? void : string))"],
'wp_clear_scheduled_hook' => ['(0|positive-int|($wp_error is false ? false : \WP_Error))', 'args' => $cronArgsType],
'wp_get_schedule' => [null, 'args' => $cronArgsType],
Expand All @@ -51,8 +52,10 @@
'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)'],
'WP_List_Table::set_pagination_args' => ['void', 'args' => 'array{total_items?: int, total_pages?: int, per_page?: int}'],
'wp_next_scheduled' => [null, 'args' => $cronArgsType],
'WP_Query::have_posts' => [null, '@phpstan-impure' => ''],
Expand Down Expand Up @@ -163,4 +166,5 @@
'wp_get_post_tags' => ["(\$args is array{fields: 'names'|'slugs'} ? list<string> : (\$args is array{fields: 'id=>name'|'id=>slug'} ? array<int, string> : (\$args is array{fields: 'id=>parent'} ? array<int, int> : (\$args is array{fields: 'ids'|'tt_ids'} ? list<int> : array<int, \WP_Term>))))|\WP_Error"],
'wp_get_post_terms' => ["(\$args is array{fields: 'names'|'slugs'} ? list<string> : (\$args is array{fields: 'id=>name'|'id=>slug'} ? array<int, string> : (\$args is array{fields: 'id=>parent'} ? array<int, int> : (\$args is array{fields: 'ids'|'tt_ids'} ? list<int> : array<int, \WP_Term>))))|\WP_Error"],
'wp_get_object_terms' => ["(\$args is array{fields: 'names'|'slugs'} ? list<string> : (\$args is array{fields: 'id=>name'|'id=>slug'} ? array<int, string> : (\$args is array{fields: 'id=>parent'} ? array<int, int> : (\$args is array{fields: 'ids'|'tt_ids'} ? list<int> : array<int, \WP_Term>))))|\WP_Error"],
'wp_parse_str' => [null, '@param-out' => 'array<int|string, array|string> $result'],
];
5 changes: 4 additions & 1 deletion tests/TypeInferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,24 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/has_filter.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/is_wp_error.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/mysql2date.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/rest_ensure_response.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/term_exists.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_debug_backtrace_summary.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_die.php');
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_get_post_categories.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_get_post_tags.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_get_post_terms.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_get_object_terms.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');
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_rest_request.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_theme.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/wpdb.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/rest_ensure_response.php');
}

/**
Expand Down
25 changes: 25 additions & 0 deletions tests/data/wp_die.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace PhpStubs\WordPress\Core\Tests;

use function wp_die;
use function PHPStan\Testing\assertType;

// default (['exit' => true])
assertType('never', wp_die());
assertType('never', wp_die(''));
assertType('never', wp_die('', ''));

// explicit exit
assertType('never', wp_die('', '', ['exit' => true]));
assertType('null', wp_die('', '', ['exit' => false]));

// unknonwn
assertType('null', wp_die('', '', ['exit' => (bool)$_GET['exit']]));
assertType('null', wp_die('', '', (array)$_GET['args']));

// non-array $args parameter ($args not string type per @phpstan-param)
/** @var int $args */
assertType('never', wp_die('', '', $args));
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']);
}
28 changes: 28 additions & 0 deletions tests/data/wp_list_pages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* Note:
* Starting from PHPStan 1.10.49, void types, including void in unions, are
* transformed into null.
*
* @link https://github.com/phpstan/phpstan-src/pull/2778
*/

declare(strict_types=1);

namespace PhpStubs\WordPress\Core\Tests;

use function wp_list_pages;
use function PHPStan\Testing\assertType;

// Default value of true
assertType('null', wp_list_pages());

// Explicit value of true
assertType('null', wp_list_pages(['echo' => true, 'key' => 'value']));

// Explicit value of false
assertType('string', wp_list_pages(['echo' => false, 'key' => 'value']));

// Unknown value
assertType('string|null', wp_list_pages(['echo' => (bool)$GET['echo'], 'key' => 'value']));
7 changes: 7 additions & 0 deletions wordpress-stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -112094,6 +112094,7 @@ function map_deep($value, $callback)
*
* @param string $input_string The string to be parsed.
* @param array $result Variables will be stored in this array.
* @param-out array<int|string, array|string> $result
*/
function wp_parse_str($input_string, &$result)
{
Expand Down Expand Up @@ -113899,6 +113900,7 @@ function wp_nonce_ays($action)
* code?: string,
* exit?: bool,
* } $args
* @phpstan-return ($args is array{exit: false} ? void : never))
*/
function wp_die($message = '', $title = '', $args = array())
{
Expand Down Expand Up @@ -114476,6 +114478,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 Expand Up @@ -132080,6 +132086,7 @@ function wp_dropdown_pages($args = '')
* post_type?: string,
* post_status?: string|array,
* } $args
* @phpstan-return ($args is array{echo: false} ? string : void)
*/
function wp_list_pages($args = '')
{
Expand Down

0 comments on commit 4597207

Please sign in to comment.