Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Demo: Add conditional return type for $args array of get_posts - Can we? #134

Closed
wants to merge 11 commits into from
1 change: 1 addition & 0 deletions functionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* @link https://github.com/phpstan/phpstan-src/blob/1.10.x/resources/functionMap.php
*/
return [
'get_posts' => ["(\$args is array{'fields': 'id=>parent'|'ids'} ? array<int, int> : array<int, WP_Post>)"],
'addslashes_gpc' => ['T', '@phpstan-template' => 'T', 'gpc' => 'T'],
'have_posts' => [null, '@phpstan-impure' => ''],
'rawurlencode_deep' => ['T', '@phpstan-template' => 'T', 'value' => 'T'],
Expand Down
1 change: 1 addition & 0 deletions tests/TypeInferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/get_post.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/get_post_stati.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/get_post_types.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/get_posts.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/get_page_by_path.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/get_permalink.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/get_term_by.php');
Expand Down
66 changes: 66 additions & 0 deletions tests/data/get_posts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace PhpStubs\WordPress\Core\Tests;

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']));
1 change: 1 addition & 0 deletions wordpress-stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -126599,6 +126599,7 @@ function is_post_publicly_viewable($post = \null)
* w?: int,
* year?: int,
* } $args
* @phpstan-return ($args is array{'fields': 'id=>parent'|'ids'} ? array<int, int> : array<int, WP_Post>)
*/
function get_posts($args = \null)
{
Expand Down