-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #195 from IanDelMar/import-extensions
Add conditional return types for several functions
- Loading branch information
Showing
13 changed files
with
409 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpStubs\WordPress\Core\Tests; | ||
|
||
use function get_approved_comments; | ||
use function PHPStan\Testing\assertType; | ||
|
||
assertType('array<int, WP_Comment>', get_approved_comments(1)); | ||
|
||
assertType('int', get_approved_comments(1, ['count' => true])); | ||
|
||
assertType('int', get_approved_comments(1, ['count' => true,'fields' => 'ids'])); | ||
|
||
assertType('array<int, WP_Comment>', get_approved_comments(1, ['count' => false])); | ||
|
||
assertType('array<int, int>', get_approved_comments(1, ['fields' => 'ids'])); | ||
|
||
assertType('array<int, int>', get_approved_comments(1, ['count' => false, 'fields' => 'ids'])); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpStubs\WordPress\Core\Tests; | ||
|
||
use function get_posts; | ||
use function PHPStan\Testing\assertType; | ||
|
||
assertType('array<int, WP_Post>', get_posts()); | ||
assertType('array<int, WP_Post>', get_posts(['key' => 'value'])); | ||
assertType('array<int, WP_Post>', get_posts(['fields' => ''])); | ||
assertType('array<int, int>', get_posts(['fields' => 'ids'])); | ||
assertType('array<int, int>', get_posts(['fields' => 'id=>parent'])); | ||
assertType('array<int, WP_Post>', get_posts(['fields' => 'Hello'])); | ||
|
||
// Nonconstant array | ||
assertType('array<int, int|WP_Post>', get_posts((array)$_GET['array'])); | ||
|
||
// Unions | ||
$union = $_GET['foo'] ? ['key' => 'value'] : ['some' => 'thing']; | ||
assertType('array<int, WP_Post>', get_posts($union)); | ||
|
||
$union = $_GET['foo'] ? ['key' => 'value'] : ['fields' => 'ids']; | ||
assertType('array<int, int|WP_Post>', get_posts($union)); | ||
|
||
$union = $_GET['foo'] ? ['key' => 'value'] : ['fields' => '']; | ||
assertType('array<int, WP_Post>', get_posts($union)); | ||
|
||
$union = $_GET['foo'] ? ['key' => 'value'] : ['fields' => 'id=>parent']; | ||
assertType('array<int, int|WP_Post>', get_posts($union)); | ||
|
||
$union = $_GET['foo'] ? ['fields' => ''] : ['fields' => 'ids']; | ||
assertType('array<int, int|WP_Post>', get_posts($union)); | ||
|
||
$union = $_GET['foo'] ? ['fields' => ''] : ['fields' => 'id=>parent']; | ||
assertType('array<int, int|WP_Post>', get_posts($union)); | ||
|
||
$union = $_GET['foo'] ? ['fields' => 'ids'] : ['fields' => 'id=>parent']; | ||
assertType('array<int, int>', get_posts($union)); | ||
|
||
$union = $_GET['foo'] ? (array)$_GET['array'] : ['fields' => '']; | ||
assertType('array<int, int|WP_Post>', get_posts($union)); | ||
|
||
$union = $_GET['foo'] ? (array)$_GET['array'] : ['fields' => 'ids']; | ||
assertType('array<int, int|WP_Post>', get_posts($union)); | ||
|
||
$union = $_GET['foo'] ? (array)$_GET['array'] : ['fields' => 'id=>parent']; | ||
assertType('array<int, int|WP_Post>', get_posts($union)); | ||
|
||
$union = $_GET['foo'] ? (string)$_GET['string'] : ''; | ||
assertType('array<int, int|WP_Post>', get_posts(['fields' => $union])); | ||
|
||
$union = $_GET['foo'] ? (string)$_GET['string'] : 'ids'; | ||
assertType('array<int, int|WP_Post>', get_posts(['fields' => $union])); | ||
|
||
$union = $_GET['foo'] ? (string)$_GET['string'] : 'id=>parent'; | ||
assertType('array<int, int|WP_Post>', get_posts(['fields' => $union])); | ||
|
||
$union = $_GET['foo'] ? (string)$_GET['string'] : 'fields'; | ||
assertType('array<int, WP_Post>', get_posts([$union => ''])); | ||
|
||
$union = $_GET['foo'] ? (string)$_GET['string'] : 'fields'; | ||
assertType('array<int, int|WP_Post>', get_posts([$union => 'ids'])); | ||
|
||
$union = $_GET['foo'] ? (string)$_GET['string'] : 'fields'; | ||
assertType('array<int, int|WP_Post>', get_posts([$union => 'id=>parent'])); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpStubs\WordPress\Core\Tests; | ||
|
||
use function get_sites; | ||
use function PHPStan\Testing\assertType; | ||
|
||
// Default parameter | ||
assertType('array<int, WP_Site>', get_sites()); | ||
assertType('array<int, WP_Site>', get_sites([])); | ||
|
||
// Non constant array parameter | ||
/** @var array<int|string,mixed> $value */ | ||
$value = $_GET['foo']; | ||
assertType('array<int, int|WP_Site>|int', get_sites($value)); | ||
|
||
// Array parameter with explicit fields value and default count value. | ||
assertType('array<int, int>', get_sites(['fields' => 'ids'])); | ||
assertType('array<int, WP_Site>', get_sites(['fields' => ''])); | ||
assertType('array<int, WP_Site>', get_sites(['fields' => 'nonEmptyString'])); | ||
|
||
// Array parameter with count set to true. | ||
assertType('int', get_sites(['count' => true])); | ||
assertType('int', get_sites(['fields' => '', 'count' => true])); | ||
assertType('int', get_sites(['fields' => 'ids', 'count' => true])); | ||
assertType('int', get_sites(['fields' => 'nonEmptyString', 'count' => true])); | ||
|
||
// Array parameter with count set to false. | ||
assertType('array<int, WP_Site>', get_sites(['count' => false])); | ||
assertType('array<int, WP_Site>', get_sites(['fields' => '', 'count' => false])); | ||
assertType('array<int, int>', get_sites(['fields' => 'ids', 'count' => false])); | ||
assertType('array<int, WP_Site>', get_sites(['fields' => 'nonEmptyString', 'count' => false])); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpStubs\WordPress\Core\Tests; | ||
|
||
use function get_tags; | ||
use function PHPStan\Testing\assertType; | ||
|
||
// Default argument values (fields => all) | ||
assertType('array<int, WP_Term>|WP_Error', get_tags()); | ||
assertType('array<int, WP_Term>|WP_Error', get_tags([])); | ||
|
||
// Requesting a count | ||
assertType('numeric-string|WP_Error', get_tags(['fields' => 'count'])); | ||
assertType('numeric-string|WP_Error', get_tags(['foo' => 'bar','fields' => 'count'])); | ||
|
||
// Requesting names or slugs | ||
assertType('list<string>|WP_Error', get_tags(['fields' => 'names'])); | ||
assertType('list<string>|WP_Error', get_tags(['fields' => 'slugs'])); | ||
assertType('array<int, string>|WP_Error', get_tags(['fields' => 'id=>name'])); | ||
assertType('array<int, string>|WP_Error', get_tags(['fields' => 'id=>slug'])); | ||
|
||
// Requesting IDs | ||
assertType('list<int>|WP_Error', get_tags(['fields' => 'ids'])); | ||
assertType('list<int>|WP_Error', get_tags(['fields' => 'tt_ids'])); | ||
|
||
// Requesting parent IDs | ||
assertType('array<int, int>|WP_Error', get_tags(['fields' => 'id=>parent'])); | ||
|
||
// Requesting objects | ||
assertType('array<int, WP_Term>|WP_Error', get_tags(['fields' => 'all'])); | ||
assertType('array<int, WP_Term>|WP_Error', get_tags(['fields' => 'all_with_object_id'])); | ||
assertType('array<int, WP_Term>|WP_Error', get_tags(['fields' => 'foo'])); | ||
|
||
// Unknown fields value | ||
assertType('array<int, int|string|WP_Term>|numeric-string|WP_Error', get_tags(['fields' => (string)$_GET['fields']])); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpStubs\WordPress\Core\Tests; | ||
|
||
use function get_terms; | ||
use function PHPStan\Testing\assertType; | ||
|
||
// Default argument values (fields => all) | ||
assertType('array<int, WP_Term>|WP_Error', get_terms()); | ||
assertType('array<int, WP_Term>|WP_Error', get_terms([])); | ||
|
||
// Requesting a count | ||
assertType('numeric-string|WP_Error', get_terms(['fields' => 'count'])); | ||
assertType('numeric-string|WP_Error', get_terms(['foo' => 'bar','fields' => 'count'])); | ||
|
||
// Requesting names or slugs | ||
assertType('list<string>|WP_Error', get_terms(['fields' => 'names'])); | ||
assertType('list<string>|WP_Error', get_terms(['fields' => 'slugs'])); | ||
assertType('array<int, string>|WP_Error', get_terms(['fields' => 'id=>name'])); | ||
assertType('array<int, string>|WP_Error', get_terms(['fields' => 'id=>slug'])); | ||
|
||
// Requesting IDs | ||
assertType('list<int>|WP_Error', get_terms(['fields' => 'ids'])); | ||
assertType('list<int>|WP_Error', get_terms(['fields' => 'tt_ids'])); | ||
|
||
// Requesting parent IDs | ||
assertType('array<int, int>|WP_Error', get_terms(['fields' => 'id=>parent'])); | ||
|
||
// Requesting objects | ||
assertType('array<int, WP_Term>|WP_Error', get_terms(['fields' => 'all'])); | ||
assertType('array<int, WP_Term>|WP_Error', get_terms(['fields' => 'all_with_object_id'])); | ||
assertType('array<int, WP_Term>|WP_Error', get_terms(['fields' => 'foo'])); | ||
|
||
// Unknown fields value | ||
assertType('array<int, int|string|WP_Term>|numeric-string|WP_Error', get_terms(['fields' => (string)$_GET['fields']])); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpStubs\WordPress\Core\Tests; | ||
|
||
use function wp_get_object_terms; | ||
use function PHPStan\Testing\assertType; | ||
|
||
$objectIDs = 123; | ||
$taxonomies = 'category'; | ||
|
||
// Default argument values (fields => all) | ||
assertType('array<int, WP_Term>|WP_Error', wp_get_object_terms($objectIDs, $taxonomies)); | ||
assertType('array<int, WP_Term>|WP_Error', wp_get_object_terms($objectIDs, $taxonomies, [])); | ||
|
||
// Empty $object_ids or $taxonomies | ||
assertType('array{}', wp_get_object_terms(0, $taxonomies, ['fields' => 'count'])); | ||
assertType('array{}', wp_get_object_terms([], $taxonomies)); | ||
assertType('array{}', wp_get_object_terms($objectIDs, '')); | ||
assertType('array{}', wp_get_object_terms($objectIDs, [])); | ||
|
||
// Requesting names or slugs | ||
assertType('list<string>|WP_Error', wp_get_object_terms($objectIDs, $taxonomies, ['fields' => 'names'])); | ||
assertType('list<string>|WP_Error', wp_get_object_terms($objectIDs, $taxonomies, ['fields' => 'slugs'])); | ||
assertType('array<int, string>|WP_Error', wp_get_object_terms($objectIDs, $taxonomies, ['fields' => 'id=>name'])); | ||
assertType('array<int, string>|WP_Error', wp_get_object_terms($objectIDs, $taxonomies, ['fields' => 'id=>slug'])); | ||
|
||
// Requesting IDs | ||
assertType('list<int>|WP_Error', wp_get_object_terms($objectIDs, $taxonomies, ['fields' => 'ids'])); | ||
assertType('list<int>|WP_Error', wp_get_object_terms($objectIDs, $taxonomies, ['fields' => 'tt_ids'])); | ||
|
||
// Requesting parent IDs | ||
assertType('array<int, int>|WP_Error', wp_get_object_terms($objectIDs, $taxonomies, ['fields' => 'id=>parent'])); | ||
|
||
// Requesting objects | ||
assertType('array<int, WP_Term>|WP_Error', wp_get_object_terms($objectIDs, $taxonomies, ['fields' => 'all'])); | ||
assertType('array<int, WP_Term>|WP_Error', wp_get_object_terms($objectIDs, $taxonomies, ['fields' => 'all_with_object_id'])); | ||
assertType('array<int, WP_Term>|WP_Error', wp_get_object_terms($objectIDs, $taxonomies, ['fields' => 'foo'])); | ||
|
||
// Requesting a count | ||
assertType('numeric-string|WP_Error', wp_get_object_terms($objectIDs, $taxonomies, ['fields' => 'count'])); | ||
|
||
// Unknown fields value | ||
assertType('array<int, int|string|WP_Term>|numeric-string|WP_Error', wp_get_object_terms($objectIDs, $taxonomies, ['fields' => (string)$_GET['fields']])); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpStubs\WordPress\Core\Tests; | ||
|
||
use function wp_get_post_categories; | ||
use function PHPStan\Testing\assertType; | ||
|
||
$postID = 123; | ||
|
||
// Default argument values (fields => ids) | ||
assertType('list<int>|WP_Error', wp_get_post_categories($postID)); | ||
assertType('list<int>|WP_Error', wp_get_post_categories($postID, [])); | ||
|
||
// Empty $post_id | ||
assertType('array{}', wp_get_post_categories(0)); | ||
|
||
// Requesting names or slugs | ||
assertType('list<string>|WP_Error', wp_get_post_categories($postID, ['fields' => 'names'])); | ||
assertType('list<string>|WP_Error', wp_get_post_categories($postID, ['fields' => 'slugs'])); | ||
assertType('array<int, string>|WP_Error', wp_get_post_categories($postID, ['fields' => 'id=>name'])); | ||
assertType('array<int, string>|WP_Error', wp_get_post_categories($postID, ['fields' => 'id=>slug'])); | ||
|
||
// Requesting IDs | ||
assertType('list<int>|WP_Error', wp_get_post_categories($postID, ['fields' => 'ids'])); | ||
assertType('list<int>|WP_Error', wp_get_post_categories($postID, ['fields' => 'tt_ids'])); | ||
|
||
// Requesting parent IDs | ||
assertType('array<int, int>|WP_Error', wp_get_post_categories($postID, ['fields' => 'id=>parent'])); | ||
|
||
// Requesting objects | ||
assertType('array<int, WP_Term>|WP_Error', wp_get_post_categories($postID, ['fields' => 'all'])); | ||
assertType('array<int, WP_Term>|WP_Error', wp_get_post_categories($postID, ['fields' => 'all_with_object_id'])); | ||
assertType('list<int>|WP_Error', wp_get_post_categories($postID, ['fields' => 'foo'])); | ||
|
||
// Requesting a count | ||
assertType('numeric-string|WP_Error', wp_get_post_categories($postID, ['fields' => 'count'])); | ||
|
||
// Unknown fields value | ||
assertType('array<int, int|string|WP_Term>|numeric-string|WP_Error', wp_get_post_categories($postID, ['fields' => (string)$_GET['fields']])); |
Oops, something went wrong.