Skip to content

Commit fa9e550

Browse files
authored
Add conditional return type for wp_unique_id (#231)
1 parent 14ae80f commit fa9e550

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

functionMap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
'wp_slash' => ['T', '@phpstan-template' => 'T', 'value' => 'T'],
8585
'wp_tag_cloud' => ["(\$args is array{format: 'array'} ? array<int, string>|void : (\$args is array{echo: false|0} ? string|void : void))"],
8686
'wp_trigger_error' => [null, 'function_name' => 'callable-string', 'error_level' => '\E_USER_ERROR|\E_USER_WARNING|\E_USER_NOTICE|\E_USER_DEPRECATED'],
87+
'wp_unique_id' => ['($prefix is empty ? numeric-string : ($prefix is numeric ? numeric-string : string))'],
8788
'wp_unschedule_event' => ['($wp_error is false ? bool : true|\WP_Error)', 'args' => $cronArgsType],
8889
'wp_unslash' => ['T', '@phpstan-template' => 'T', 'value' => 'T'],
8990
'wp_widget_rss_form' => ['void', 'args' => $wpWidgetRssFormArgsType, 'inputs' => $wpWidgetRssFormInputsType],

tests/TypeInferenceTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public function dataFileAsserts(): iterable
6666
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_tag_cloud.php');
6767
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_theme.php');
6868
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_translations.php');
69+
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_unique_id.php');
6970
yield from $this->gatherAssertTypes(__DIR__ . '/data/wp_widget_factory.php');
7071
yield from $this->gatherAssertTypes(__DIR__ . '/data/wpdb.php');
7172
}

tests/data/wp_unique_id.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpStubs\WordPress\Core\Tests;
6+
7+
use function wp_unique_id;
8+
use function PHPStan\Testing\assertType;
9+
10+
/** @var numeric-string $numericString */
11+
$numericString = $_GET['numericString'];
12+
13+
assertType('numeric-string', wp_unique_id());
14+
assertType('numeric-string', wp_unique_id(''));
15+
assertType('numeric-string', wp_unique_id('1'));
16+
assertType('numeric-string', wp_unique_id($numericString));
17+
18+
assertType('string', wp_unique_id('string'));
19+
assertType('string', wp_unique_id($_GET['string']));

wordpress-stubs.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110473,6 +110473,7 @@ function wp_is_uuid($uuid, $version = \null)
110473110473
*
110474110474
* @param string $prefix Prefix for the returned ID.
110475110475
* @return string Unique ID.
110476+
* @phpstan-return ($prefix is empty ? numeric-string : ($prefix is numeric ? numeric-string : string))
110476110477
*/
110477110478
function wp_unique_id($prefix = '')
110478110479
{

0 commit comments

Comments
 (0)