Skip to content
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 @@ -40,6 +40,7 @@
'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' => ''],
'add_comments_page' => [null, 'callback' => "''|callable"],
'add_dashboard_page' => [null, 'callback' => "''|callable"],
'add_feed' => ['non-falsy-string', 'callback' => 'callable(bool, string): void'],
'add_link' => ['int<0, max>'],
'add_links_page' => [null, 'callback' => "''|callable"],
'add_management_page' => [null, 'callback' => "''|callable"],
Expand Down
16 changes: 16 additions & 0 deletions tests/ParameterTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ public function testAbsint(): void
);
}

public function testAddFeed(): void
{
$this->analyse(
__DIR__ . '/data/param/add-feed.php',
[
["Parameter #2 \$callback of function add_feed expects callable(bool, string): void, '' given.", 19],
['Parameter #2 $callback of function add_feed expects callable(bool, string): void, Closure(int): void given.', 20],
['Parameter #2 $callback of function add_feed expects callable(bool, string): void, Closure(bool, int): void given.', 21],
['Parameter #2 $callback of function add_feed expects callable(bool, string): void, Closure(bool, string): int given.', 22],
["Parameter #2 \$callback of function add_feed expects callable(bool, string): void, 'addFeedNotOkFirst' given.", 23],
["Parameter #2 \$callback of function add_feed expects callable(bool, string): void, 'addFeedNotOkSecond' given.", 24],
["Parameter #2 \$callback of function add_feed expects callable(bool, string): void, 'addFeedNotOkReturn' given.", 25],
]
);
}

public function testAddMenuPage(): void
{
$this->analyse(
Expand Down
33 changes: 33 additions & 0 deletions tests/data/param/add-feed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

// phpcs:disable

declare(strict_types=1);

use PhpStubs\WordPress\Core\Tests\Faker;

$feedname = Faker::string();

function addFeedNotOkFirst(int $isCommentFeed): void {}
function addFeedNotOkSecond(bool $isCommentFeed, int $feed): void {}
function addFeedNotOkReturn(bool $isCommentFeed, string $feed): int {return Faker::int();}
function addFeedCorrect(bool $isCommentFeed, string $feed): void {}
function addFeedCorrectFirst(bool $isCommentFeed): void {}
function addFeedCorrectNoParams(): void {}

// Incorrect usages
add_feed($feedname, ''); // Not a callable
add_feed($feedname, static function (int $isCommentFeed): void {}); // Incorrect type for $isCommentFeed
add_feed($feedname, static function (bool $isCommentFeed, int $feed): void {}); // Incorrect type for $feed
add_feed($feedname, static function (bool $isCommentFeed, string $feed): int {return Faker::int();}); // Incorrect callback return type
add_feed($feedname, 'addFeedNotOkFirst'); // Incorrect type for $isCommentFeed
add_feed($feedname, 'addFeedNotOkSecond'); // Incorrect type for $feed
add_feed($feedname, 'addFeedNotOkReturn'); // Incorrect callback return type

// Correct usages
add_feed($feedname, static function (bool $isCommentFeed, string $feed): void {});
add_feed($feedname, static function (bool $isCommentFeed): void {});
add_feed($feedname, static function (): void {});
add_feed($feedname, 'addFeedCorrect');
add_feed($feedname, 'addFeedCorrectFirst');
add_feed($feedname, 'addFeedCorrectNoParams');
13 changes: 13 additions & 0 deletions tests/data/return/add-feed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace PhpStubs\WordPress\Core\Tests;

use function add_feed;
use function PHPStan\Testing\assertType;

assertType('non-falsy-string', add_feed('', Faker::callable()));
assertType('non-falsy-string', add_feed('value', Faker::callable()));
assertType('non-falsy-string', add_feed(Faker::nonEmptyString(), Faker::callable()));
assertType('non-falsy-string', add_feed(Faker::nonFalsyString(), Faker::callable()));
2 changes: 2 additions & 0 deletions wordpress-stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -136496,6 +136496,8 @@ function remove_permastruct($name)
* @param string $feedname Feed name. Should not start with '_'.
* @param callable $callback Callback to run on feed display.
* @return string Feed action name.
* @phpstan-param callable(bool, string): void $callback
* @phpstan-return non-falsy-string
*/
function add_feed($feedname, $callback)
{
Expand Down