Skip to content

Add conditional return type and narrow argument type for has_shortcode() #361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions functionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
'get_user_by' => ["(\$field is 'id'|'ID' ? (\$value is int<min, 0> ? false : \WP_User|false) : \WP_User|false)"],
'has_action' => ['($callback is false ? bool : false|int)'],
'has_filter' => ['($callback is false ? bool : false|int)'],
'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' => ''],
'have_posts' => [null, '@phpstan-impure' => ''],
'image_link_input_fields' => ['non-falsy-string'],
'image_size_input_fields' => ["array{label: string, input: 'html', html: string}"],
Expand Down
1 change: 1 addition & 0 deletions tests/TypeInferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/get_user.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/get_user_by.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/has_filter.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/has_shortcode.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/have_posts.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/image_link_input_fields.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/image_size_input_fields.php');
Expand Down
42 changes: 42 additions & 0 deletions tests/data/has_shortcode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace PhpStubs\WordPress\Core\Tests;

use function PHPStan\Testing\assertType;

/*
* Check return type
*/

assertType('false', has_shortcode('', ''));
assertType('false', has_shortcode('', 'foo'));
assertType('false', has_shortcode('foo', ''));
assertType('bool', has_shortcode('foo', 'foo'));

assertType('false', has_shortcode('', ''));
assertType('false', has_shortcode('', Faker::string()));
assertType('false', has_shortcode(Faker::string(), ''));
assertType('bool', has_shortcode(Faker::string(), Faker::string()));

/*
* Check argument type
*/

$content = Faker::string();
$tag = Faker::string();
if (has_shortcode($content, $tag)) {
assertType('non-falsy-string', $content);
assertType('non-empty-string', $tag);
}
assertType('string', $content);
assertType('string', $tag);

// Check that types are not generalized
$content = 'content';
$tag = 'tag';
if (has_shortcode($content, $tag)) {
assertType("'content'", $content);
assertType("'tag'", $tag);
}
3 changes: 3 additions & 0 deletions wordpress-stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -137625,6 +137625,9 @@ function shortcode_exists($tag)
* @param string $content Content to search for shortcodes.
* @param string $tag Shortcode tag to check.
* @return bool Whether the passed content contains the given shortcode.
* @phpstan-assert-if-true =non-falsy-string $content
* @phpstan-assert-if-true =non-empty-string $tag
* @phpstan-return ($tag is empty ? false : ($content is empty ? false : bool))
*/
function has_shortcode($content, $tag)
{
Expand Down