Skip to content

Commit 13139c7

Browse files
committed
Narrow $callback parameter type for add_shortcode()
1 parent 8a150a9 commit 13139c7

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

functionMap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +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'],
52+
'add_shortcode' => ['void', 'tag' => 'non-empty-string', 'callback' => 'callable(array<string>, string|null, string): string'],
5353
'add_submenu_page' => [null, 'callback' => "''|callable"],
5454
'add_theme_page' => [null, 'callback' => "''|callable"],
5555
'add_users_page' => [null, 'callback' => "''|callable"],

tests/ParameterTypeTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,14 @@ public function testAddShortcode(): void
5757
[
5858
['Parameter #1 $tag of function add_shortcode expects non-empty-string, 1 given.', 10],
5959
["Parameter #1 \$tag of function add_shortcode expects non-empty-string, '' given.", 11],
60-
// Maybes
6160
['Parameter #1 $tag of function add_shortcode expects non-empty-string, string given.', 14],
61+
['Parameter #2 $callback of function add_shortcode expects callable(array<string>, string|null, string): string, Closure(): void given.', 17],
62+
['Parameter #2 $callback of function add_shortcode expects callable(array<string>, string|null, string): string, Closure(string): string given.', 18],
63+
['Parameter #2 $callback of function add_shortcode expects callable(array<string>, string|null, string): string, Closure(array, string): string given.', 19],
64+
['Parameter #2 $callback of function add_shortcode expects callable(array<string>, string|null, string): string, Closure(array, string|null, bool): string given.', 20],
65+
["Parameter #2 \$callback of function add_shortcode expects callable(array<string>, string|null, string): string, 'addShortcodeVoid' given.", 21],
66+
["Parameter #2 \$callback of function add_shortcode expects callable(array<string>, string|null, string): string, 'addShortcodeIntDoc' given.", 22],
67+
6268
]
6369
);
6470
}

tests/data/param/add-shortcode.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
<?php
1+
<?php // phpcs:disable
22

33
declare(strict_types=1);
44

5-
namespace PhpStubs\WordPress\Core\Tests;
5+
use PhpStubs\WordPress\Core\Tests\Faker;
66

7-
use function add_shortcode;
7+
$tag = Faker::nonEmptyString();
88

99
// Incorrect $tag
1010
add_shortcode(1, Faker::callable());
@@ -13,8 +13,31 @@
1313
// Maybe incorrect $tag
1414
add_shortcode(Faker::string(), Faker::callable());
1515

16+
// Incorrect $callback
17+
add_shortcode($tag, static function(): void {}); // Incorrect return type
18+
add_shortcode($tag, static function(string $atts): string {return Faker::string();}); // Incorrect $atts type
19+
add_shortcode($tag, static function(array $atts, string $content): string {return Faker::string();}); // Incorrect $content type (must accept null)
20+
add_shortcode($tag, static function(array $atts, ?string $content, bool $tag): string {return Faker::string();}); // Incorrect $tag type
21+
add_shortcode($tag, 'addShortcodeVoid'); // Incorrect callback return type
22+
add_shortcode($tag, 'addShortcodeIntDoc'); // Incorrect callback return type
23+
1624
// Correct $tag
1725
add_shortcode('0', Faker::callable()); // '0' is a valid tag
1826
add_shortcode('tag', Faker::callable());
19-
add_shortcode(Faker::nonEmptyString(), Faker::callable()); // '0' is a valid tag
20-
add_shortcode(Faker::nonFalsyString(), Faker::callable());
27+
add_shortcode($tag, Faker::callable());
28+
29+
// Correct $callback
30+
add_shortcode($tag, static function(): string {return '';});
31+
add_shortcode($tag, static function(array $atts): string {return '';});
32+
add_shortcode($tag, static function(array $atts, ?string $content): string {return '';});
33+
add_shortcode($tag, static function(array $atts, ?string $content, string $tag): string {return '';});
34+
add_shortcode($tag, 'addShortcodeStringDoc');
35+
36+
function addShortcodeVoid(): void {}
37+
function addShortcodeString(): string {return Faker::string();}
38+
39+
/** @return int */
40+
function addShortcodeIntDoc() {return Faker::int();}
41+
42+
/** @return string */
43+
function addShortcodeStringDoc() {return Faker::string();}

wordpress-stubs.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137606,6 +137606,7 @@ function wp_default_script_modules()
137606137606
* or null if not set (`$content`), and finally the shortcode tag
137607137607
* itself (`$shortcode_tag`), in that order.
137608137608
* @phpstan-param non-empty-string $tag
137609+
* @phpstan-param callable(array<string>, string|null, string): string $callback
137609137610
* @phpstan-return void
137610137611
*/
137611137612
function add_shortcode($tag, $callback)

0 commit comments

Comments
 (0)