Skip to content

Commit 534be49

Browse files
committed
Add conditional return type for _wp_json_sanity_check()
1 parent d2a203c commit 534be49

File tree

4 files changed

+24
-0
lines changed

4 files changed

+24
-0
lines changed

functionMap.php

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* @link https://github.com/phpstan/phpstan-src/blob/1.10.x/resources/functionMap.php
3333
*/
3434
return [
35+
'_wp_json_sanity_check' => ['T', '@phpstan-template' => 'T', 'value' => 'T', 'depth' => 'positive-int'],
3536
'_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}'],
3637
'addslashes_gpc' => ['T', '@phpstan-template' => 'T', 'gpc' => 'T'],
3738
'add_submenu_page' => [null, 'callback' => "''|callable"],

tests/TypeInferenceTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class TypeInferenceTest extends \PHPStan\Testing\TypeInferenceTestCase
1010
public function dataFileAsserts(): iterable
1111
{
1212
yield from $this->gatherAssertTypes(__DIR__ . '/data/_get_list_table.php');
13+
yield from $this->gatherAssertTypes(__DIR__ . '/data/_wp_json_sanity_check.php');
1314
yield from $this->gatherAssertTypes(__DIR__ . '/data/current_time.php');
1415
yield from $this->gatherAssertTypes(__DIR__ . '/data/echo_parameter.php');
1516
yield from $this->gatherAssertTypes(__DIR__ . '/data/get_approved_comments.php');

tests/data/_wp_json_sanity_check.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpStubs\WordPress\Core\Tests;
6+
7+
use stdClass;
8+
9+
use function PHPStan\Testing\assertType;
10+
use function _wp_json_sanity_check;
11+
12+
assertType('null', _wp_json_sanity_check(null, 1));
13+
assertType('bool', _wp_json_sanity_check((bool)$_GET['value'], 1));
14+
assertType('int', _wp_json_sanity_check((int)$_GET['value'], 1));
15+
assertType('string', _wp_json_sanity_check((string)$_GET['value'], 1));
16+
assertType('array', _wp_json_sanity_check((array)$_GET['value'], 1));
17+
assertType('stdClass', _wp_json_sanity_check(new stdClass(), 1));
18+
assertType('mixed', _wp_json_sanity_check($_GET['value'], 1));

wordpress-stubs.php

+4
Original file line numberDiff line numberDiff line change
@@ -114096,6 +114096,10 @@ function wp_json_encode($value, $flags = 0, $depth = 512)
114096114096
* @param mixed $value Variable (usually an array or object) to encode as JSON.
114097114097
* @param int $depth Maximum depth to walk through $value. Must be greater than 0.
114098114098
* @return mixed The sanitized data that shall be encoded to JSON.
114099+
* @phpstan-template T
114100+
* @phpstan-param T $value
114101+
* @phpstan-param positive-int $depth
114102+
* @phpstan-return T
114099114103
*/
114100114104
function _wp_json_sanity_check($value, $depth)
114101114105
{

0 commit comments

Comments
 (0)