Skip to content

Commit 474e882

Browse files
authored
Narrow parameter type for add_shortcode (#377)
* Narrow parameter type for add_shortcode * Update ParameterTypeTest.php
1 parent b9e3faf commit 474e882

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

functionMap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
'add_pages_page' => [null, 'callback' => "''|callable"],
5050
'add_plugins_page' => [null, 'callback' => "''|callable"],
5151
'add_posts_page' => [null, 'callback' => "''|callable"],
52+
'add_shortcode' => [null, 'tag' => 'non-empty-string'],
5253
'add_submenu_page' => [null, 'callback' => "''|callable"],
5354
'add_theme_page' => [null, 'callback' => "''|callable"],
5455
'add_users_page' => [null, 'callback' => "''|callable"],

tests/ParameterTypeTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ public function testAddMenuPage(): void
5050
);
5151
}
5252

53+
public function testAddShortcode(): void
54+
{
55+
$this->analyse(
56+
__DIR__ . '/data/param/add-shortcode.php',
57+
[
58+
['Parameter #1 $tag of function add_shortcode expects non-empty-string, 1 given.', 10],
59+
["Parameter #1 \$tag of function add_shortcode expects non-empty-string, '' given.", 11],
60+
// Maybes
61+
['Parameter #1 $tag of function add_shortcode expects non-empty-string, string given.', 14],
62+
]
63+
);
64+
}
65+
5366
public function testBookmarks(): void
5467
{
5568
$field = "'link_category'|'link_description'|'link_id'|'link_image'|'link_name'|'link_notes'|'link_owner'|'link_rating'|'link_rel'|'link_rss'|'link_target'|'link_updated'|'link_url'|'link_visible'";

tests/data/param/add-shortcode.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpStubs\WordPress\Core\Tests;
6+
7+
use function add_shortcode;
8+
9+
// Incorrect $tag
10+
add_shortcode(1, Faker::callable());
11+
add_shortcode('', Faker::callable());
12+
13+
// Maybe incorrect $tag
14+
add_shortcode(Faker::string(), Faker::callable());
15+
16+
// Correct $tag
17+
add_shortcode('0', Faker::callable()); // '0' is a valid tag
18+
add_shortcode('tag', Faker::callable());
19+
add_shortcode(Faker::nonEmptyString(), Faker::callable()); // '0' is a valid tag
20+
add_shortcode(Faker::nonFalsyString(), Faker::callable());

wordpress-stubs.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137586,6 +137586,7 @@ function wp_default_script_modules()
137586137586
* including an array of attributes (`$atts`), the shortcode content
137587137587
* or null if not set (`$content`), and finally the shortcode tag
137588137588
* itself (`$shortcode_tag`), in that order.
137589+
* @phpstan-param non-empty-string $tag
137589137590
* @phpstan-return void
137590137591
*/
137591137592
function add_shortcode($tag, $callback)

0 commit comments

Comments
 (0)