Skip to content

Commit

Permalink
Map addition: bool_from_yn, is_new_day, wp_trigger_error (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
IanDelMar authored Sep 26, 2024
1 parent 832ba5a commit 199f37b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
5 changes: 4 additions & 1 deletion functionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@
return [
'_wp_json_sanity_check' => ['T', '@phpstan-template' => 'T', 'value' => 'T', 'depth' => 'positive-int'],
'_get_list_table' => ["(\$class_name is 'WP_Posts_List_Table'|'WP_Media_List_Table'|'WP_Terms_List_Table'|'WP_Users_List_Table'|'WP_Comments_List_Table'|'WP_Post_Comments_List_Table'|'WP_Links_List_Table'|'WP_Plugin_Install_List_Table'|'WP_Themes_List_Table'|'WP_Theme_Install_List_Table'|'WP_Plugins_List_Table'|'WP_Application_Passwords_List_Table'|'WP_MS_Sites_List_Table'|'WP_MS_Users_List_Table'|'WP_MS_Themes_List_Table'|'WP_Privacy_Data_Export_Requests_List_Table'|'WP_Privacy_Data_Removal_Requests_List_Table' ? T : false)", '@phpstan-template' => 'T', 'class_name' => 'class-string<T>', 'args' => 'array{screen?: string}'],
'absint' => ['($maybeint is T&non-negative-int ? T : ($maybeint is negative-int ? positive-int : ($maybeint is empty ? 0 : ($maybeint is numeric-string ? non-negative-int : ($maybeint is string ? 0 : ($maybeint is true|non-empty-array ? 1 : ($maybeint is bool ? 0|1 : non-negative-int)))))))', '@template T' => 'of int', 'maybeint' => 'T|scalar|array|resource|null'],
'absint' => ['($maybeint is T&non-negative-int ? T : ($maybeint is negative-int ? positive-int : ($maybeint is empty ? 0 : ($maybeint is numeric-string ? non-negative-int : ($maybeint is string ? 0 : ($maybeint is true|non-empty-array ? 1 : ($maybeint is bool ? 0|1 : non-negative-int)))))))', '@phpstan-template T' => 'of int', 'maybeint' => 'T|scalar|array|resource|null'],
'addslashes_gpc' => ['T', '@phpstan-template' => 'T', 'gpc' => 'T'],
'add_submenu_page' => [null, 'callback' => "''|callable"],
'bool_from_yn' => ["(\$yn is 'y' ? true : false)"],
'have_posts' => [null, '@phpstan-impure' => ''],
'is_new_day' => ['0|1'],
'paginate_links' => ["(\$args is array{total: int<min, 1>} ? void : (\$args is array{type: 'array'} ? list<string> : string))"],
'rawurlencode_deep' => ['T', '@phpstan-template' => 'T', 'value' => 'T'],
'sanitize_category' => ['T', '@phpstan-template' => 'T of array|object', 'category' => 'T'],
Expand Down Expand Up @@ -77,6 +79,7 @@
'wp_schedule_single_event' => ['($wp_error is false ? bool : true|\WP_Error)', 'args' => $cronArgsType],
'wp_slash' => ['T', '@phpstan-template' => 'T', 'value' => 'T'],
'wp_tag_cloud' => ["(\$args is array{format: 'array'} ? array<int, string>|void : (\$args is array{echo: false|0} ? string|void : void))"],
'wp_trigger_error' => [null, 'function_name' => 'callable-string', 'error_level' => '\E_USER_ERROR|\E_USER_WARNING|\E_USER_NOTICE|\E_USER_DEPRECATED'],
'wp_unschedule_event' => ['($wp_error is false ? bool : true|\WP_Error)', 'args' => $cronArgsType],
'wp_unslash' => ['T', '@phpstan-template' => 'T', 'value' => 'T'],
'wp_widget_rss_form' => ['void', 'args' => $wpWidgetRssFormArgsType, 'inputs' => $wpWidgetRssFormInputsType],
Expand Down
2 changes: 2 additions & 0 deletions tests/TypeInferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/_get_list_table.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/_wp_json_sanity_check.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/absint.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bool_from_yn.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/current_time.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/echo_parameter.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/get_approved_comments.php');
Expand All @@ -36,6 +37,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/get_term.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/get_taxonomies_for_attachments.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/has_filter.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/is_new_day.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/paginate_links.php');
Expand Down
13 changes: 13 additions & 0 deletions tests/data/bool_from_yn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace PhpStubs\WordPress\Core\Tests;

use function bool_from_yn;
use function PHPStan\Testing\assertType;

assertType('true', bool_from_yn('y'));
assertType('false', bool_from_yn('n'));
assertType('false', bool_from_yn('thing'));
assertType('bool', bool_from_yn((string)$_GET['string']));
10 changes: 10 additions & 0 deletions tests/data/is_new_day.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace PhpStubs\WordPress\Core\Tests;

use function is_new_day;
use function PHPStan\Testing\assertType;

assertType('0|1', is_new_day());
6 changes: 5 additions & 1 deletion wordpress-stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -107805,6 +107805,7 @@ function wp_get_http_headers($url, $deprecated = \false)
* @global string $previousday The day of the previous post in the loop.
*
* @return int 1 when new day, 0 if not a new day.
* @phpstan-return 0|1
*/
function is_new_day()
{
Expand Down Expand Up @@ -108044,6 +108045,7 @@ function get_num_queries()
*
* @param string $yn Character string containing either 'y' (yes) or 'n' (no).
* @return bool True if 'y', false on anything else.
* @phpstan-return ($yn is 'y' ? true : false)
*/
function bool_from_yn($yn)
{
Expand Down Expand Up @@ -109502,7 +109504,7 @@ function dead_db()
*
* @param mixed $maybeint Data you wish to have converted to a non-negative integer.
* @return int A non-negative integer.
* @template T of int
* @phpstan-template T of int
* @phpstan-param T|scalar|array|resource|null $maybeint
* @phpstan-return ($maybeint is T&non-negative-int ? T : ($maybeint is negative-int ? positive-int : ($maybeint is empty ? 0 : ($maybeint is numeric-string ? non-negative-int : ($maybeint is string ? 0 : ($maybeint is true|non-empty-array ? 1 : ($maybeint is bool ? 0|1 : non-negative-int)))))))
*/
Expand Down Expand Up @@ -109682,6 +109684,8 @@ function _doing_it_wrong($function_name, $message, $version)
* before passing to this function to avoid being stripped {@see wp_kses()}.
* @param int $error_level Optional. The designated error type for this error.
* Only works with E_USER family of constants. Default E_USER_NOTICE.
* @phpstan-param callable-string $function_name
* @phpstan-param \E_USER_ERROR|\E_USER_WARNING|\E_USER_NOTICE|\E_USER_DEPRECATED $error_level
* @phpstan-return void
*/
function wp_trigger_error($function_name, $message, $error_level = \E_USER_NOTICE)
Expand Down

0 comments on commit 199f37b

Please sign in to comment.