Skip to content

Commit ba070da

Browse files
authored
Narrow argument type for shortcode_exists (#385)
1 parent 4aa8bfd commit ba070da

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

functionMap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
'sanitize_term' => ['T', '@phpstan-template' => 'T of array|object', 'term' => 'T'],
140140
'sanitize_term_field' => ["(\$field is 'parent'|'term_id'|'count'|'term_group'|'term_taxonomy_id'|'object_id' ? int<0, max> : (\$context is 'raw' ? T : (\$context is 'attribute'|'edit'|'js' ? string : mixed)))", '@phpstan-template T' => 'of string', 'value' => 'T'],
141141
'sanitize_title_with_dashes' => ['lowercase-string', 'context' => "'display'|'save'"],
142+
'shortcode_exists' => [null, '@phpstan-assert-if-true' => '=non-empty-string $tag'],
142143
'single_cat_title' => ['($display is true ? void : string|void)'],
143144
'single_month_title' => ['($display is true ? false|void : false|string)'],
144145
'single_post_title' => ['($display is true ? void : string|void)'],
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpStubs\WordPress\Core\Tests;
6+
7+
use function shortcode_exists;
8+
use function PHPStan\Testing\assertType;
9+
10+
$tag = Faker::string();
11+
if (shortcode_exists($tag)) {
12+
assertType('non-empty-string', $tag);
13+
}
14+
if (! shortcode_exists($tag)) {
15+
assertType('string', $tag);
16+
}
17+
18+
// Check that constant strings are not generalized
19+
$tag = 'tag';
20+
if (shortcode_exists($tag)) {
21+
assertType("'tag'", $tag);
22+
}

wordpress-stubs.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137631,6 +137631,7 @@ function remove_all_shortcodes()
137631137631
*
137632137632
* @param string $tag Shortcode tag to check.
137633137633
* @return bool Whether the given shortcode exists.
137634+
* @phpstan-assert-if-true =non-empty-string $tag
137634137635
*/
137635137636
function shortcode_exists($tag)
137636137637
{

0 commit comments

Comments
 (0)