Skip to content

Commit 4186700

Browse files
committed
Narrow parameter type for add_feed()
1 parent a6fb82a commit 4186700

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-2
lines changed

functionMap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
'absint' => ['($maybeint is T&int<0, max> ? T : ($maybeint is int<min, -1> ? int<1, max> : ($maybeint is empty ? 0 : ($maybeint is numeric-string ? int<0, max> : ($maybeint is string ? 0 : ($maybeint is true|non-empty-array ? 1 : ($maybeint is bool ? 0|1 : int<0, max>)))))))', '@phpstan-template T' => 'of int', 'maybeint' => 'T|scalar|array|resource|null', '@phpstan-pure' => ''],
4141
'add_comments_page' => [null, 'callback' => "''|callable"],
4242
'add_dashboard_page' => [null, 'callback' => "''|callable"],
43-
'add_feed' => ['non-falsy-string'],
43+
'add_feed' => ['non-falsy-string', 'callback' => 'callable(bool, string): void'],
4444
'add_link' => ['int<0, max>'],
4545
'add_links_page' => [null, 'callback' => "''|callable"],
4646
'add_management_page' => [null, 'callback' => "''|callable"],

tests/Faker.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,12 @@ public static function list($type = null): array
104104
* @param TKeyOrValue $keyOrValueType
105105
* @param TValue $valueType
106106
* @return (TKeyOrValue is null ? non-empty-array<mixed> : (TValue is null ? non-empty-array<TKeyOrValue> : non-empty-array<TKeyOrValue, TValue>))
107+
*
108+
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
107109
*/
108110
public static function nonEmptyArray($keyOrValueType = null, $valueType = null): array
109111
{
110-
return [$keyOrValueType => $valueType];
112+
return ['non-empty'];
111113
}
112114

113115
/**

tests/ParameterTypeTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ public function testAbsint(): void
2828
);
2929
}
3030

31+
public function testAddFeed(): void
32+
{
33+
$this->analyse(
34+
__DIR__ . '/data/param/add-feed.php',
35+
[
36+
["Parameter #2 \$callback of function add_feed expects callable(bool, string): void, '' given.", 19],
37+
['Parameter #2 $callback of function add_feed expects callable(bool, string): void, Closure(int): void given.', 20],
38+
['Parameter #2 $callback of function add_feed expects callable(bool, string): void, Closure(bool, int): void given.', 21],
39+
['Parameter #2 $callback of function add_feed expects callable(bool, string): void, Closure(bool, string): int given.', 22],
40+
["Parameter #2 \$callback of function add_feed expects callable(bool, string): void, 'addFeedNotOkFirst' given.", 23],
41+
["Parameter #2 \$callback of function add_feed expects callable(bool, string): void, 'addFeedNotOkSecond' given.", 24],
42+
["Parameter #2 \$callback of function add_feed expects callable(bool, string): void, 'addFeedNotOkReturn' given.", 25],
43+
]
44+
);
45+
}
46+
3147
public function testAddMenuPage(): void
3248
{
3349
$this->analyse(

tests/data/param/add-feed.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
// phpcs:disable
4+
5+
declare(strict_types=1);
6+
7+
use PhpStubs\WordPress\Core\Tests\Faker;
8+
9+
$feedname = Faker::string();
10+
11+
function addFeedNotOkFirst(int $isCommentFeed): void {}
12+
function addFeedNotOkSecond(bool $isCommentFeed, int $feed): void {}
13+
function addFeedNotOkReturn(bool $isCommentFeed, string $feed): int {return Faker::int();}
14+
function addFeedCorrect(bool $isCommentFeed, string $feed): void {}
15+
function addFeedCorrectFirst(bool $isCommentFeed): void {}
16+
function addFeedCorrectNoParams(): void {}
17+
18+
// Incorrect usages
19+
add_feed($feedname, ''); // Not a callable
20+
add_feed($feedname, static function (int $isCommentFeed): void {}); // Incorrect type for $isCommentFeed
21+
add_feed($feedname, static function (bool $isCommentFeed, int $feed): void {}); // Incorrect type for $feed
22+
add_feed($feedname, static function (bool $isCommentFeed, string $feed): int {return Faker::int();}); // Incorrect callback return type
23+
add_feed($feedname, 'addFeedNotOkFirst'); // Incorrect type for $isCommentFeed
24+
add_feed($feedname, 'addFeedNotOkSecond'); // Incorrect type for $feed
25+
add_feed($feedname, 'addFeedNotOkReturn'); // Incorrect callback return type
26+
27+
// Correct usages
28+
add_feed($feedname, static function (bool $isCommentFeed, string $feed): void {});
29+
add_feed($feedname, static function (bool $isCommentFeed): void {});
30+
add_feed($feedname, static function (): void {});
31+
add_feed($feedname, 'addFeedCorrect');
32+
add_feed($feedname, 'addFeedCorrectFirst');
33+
add_feed($feedname, 'addFeedCorrectNoParams');

wordpress-stubs.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136496,6 +136496,7 @@ function remove_permastruct($name)
136496136496
* @param string $feedname Feed name. Should not start with '_'.
136497136497
* @param callable $callback Callback to run on feed display.
136498136498
* @return string Feed action name.
136499+
* @phpstan-param callable(bool, string): void $callback
136499136500
* @phpstan-return non-falsy-string
136500136501
*/
136501136502
function add_feed($feedname, $callback)

0 commit comments

Comments
 (0)