Skip to content

Commit 164f6f7

Browse files
authored
Add conditional return type and narrow argument type for has_shortcode() (#361)
1 parent cbe488c commit 164f6f7

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

functionMap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
'get_user_by' => ["(\$field is 'id'|'ID' ? (\$value is int<min, 0> ? false : \WP_User|false) : \WP_User|false)"],
109109
'has_action' => ['($callback is false ? bool : false|int)'],
110110
'has_filter' => ['($callback is false ? bool : false|int)'],
111+
'has_shortcode' => ['($tag is empty ? false : ($content is empty ? false : bool))', '@phpstan-assert-if-true =non-falsy-string $content' => '', '@phpstan-assert-if-true =non-empty-string $tag' => ''],
111112
'have_posts' => [null, '@phpstan-impure' => ''],
112113
'image_link_input_fields' => ['non-falsy-string'],
113114
'image_size_input_fields' => ["array{label: string, input: 'html', html: string}"],

tests/TypeInferenceTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public function dataFileAsserts(): iterable
5454
yield from $this->gatherAssertTypes(__DIR__ . '/data/get_user.php');
5555
yield from $this->gatherAssertTypes(__DIR__ . '/data/get_user_by.php');
5656
yield from $this->gatherAssertTypes(__DIR__ . '/data/has_filter.php');
57+
yield from $this->gatherAssertTypes(__DIR__ . '/data/has_shortcode.php');
5758
yield from $this->gatherAssertTypes(__DIR__ . '/data/have_posts.php');
5859
yield from $this->gatherAssertTypes(__DIR__ . '/data/image_link_input_fields.php');
5960
yield from $this->gatherAssertTypes(__DIR__ . '/data/image_size_input_fields.php');

tests/data/has_shortcode.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpStubs\WordPress\Core\Tests;
6+
7+
use function PHPStan\Testing\assertType;
8+
9+
/*
10+
* Check return type
11+
*/
12+
13+
assertType('false', has_shortcode('', ''));
14+
assertType('false', has_shortcode('', 'foo'));
15+
assertType('false', has_shortcode('foo', ''));
16+
assertType('bool', has_shortcode('foo', 'foo'));
17+
18+
assertType('false', has_shortcode('', ''));
19+
assertType('false', has_shortcode('', Faker::string()));
20+
assertType('false', has_shortcode(Faker::string(), ''));
21+
assertType('bool', has_shortcode(Faker::string(), Faker::string()));
22+
23+
/*
24+
* Check argument type
25+
*/
26+
27+
$content = Faker::string();
28+
$tag = Faker::string();
29+
if (has_shortcode($content, $tag)) {
30+
assertType('non-falsy-string', $content);
31+
assertType('non-empty-string', $tag);
32+
}
33+
assertType('string', $content);
34+
assertType('string', $tag);
35+
36+
// Check that types are not generalized
37+
$content = 'content';
38+
$tag = 'tag';
39+
if (has_shortcode($content, $tag)) {
40+
assertType("'content'", $content);
41+
assertType("'tag'", $tag);
42+
}

wordpress-stubs.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137627,6 +137627,9 @@ function shortcode_exists($tag)
137627137627
* @param string $content Content to search for shortcodes.
137628137628
* @param string $tag Shortcode tag to check.
137629137629
* @return bool Whether the passed content contains the given shortcode.
137630+
* @phpstan-assert-if-true =non-falsy-string $content
137631+
* @phpstan-assert-if-true =non-empty-string $tag
137632+
* @phpstan-return ($tag is empty ? false : ($content is empty ? false : bool))
137630137633
*/
137631137634
function has_shortcode($content, $tag)
137632137635
{

0 commit comments

Comments
 (0)