diff --git a/functionMap.php b/functionMap.php index b2aa883..1b97d4c 100644 --- a/functionMap.php +++ b/functionMap.php @@ -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 : array)"], 'addslashes_gpc' => ['T', '@phpstan-template' => 'T', 'gpc' => 'T'], 'have_posts' => [null, '@phpstan-impure' => ''], 'rawurlencode_deep' => ['T', '@phpstan-template' => 'T', 'value' => 'T'], diff --git a/tests/TypeInferenceTest.php b/tests/TypeInferenceTest.php index 267dea3..19c7a8e 100644 --- a/tests/TypeInferenceTest.php +++ b/tests/TypeInferenceTest.php @@ -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'); diff --git a/tests/data/get_posts.php b/tests/data/get_posts.php new file mode 100644 index 0000000..31d019f --- /dev/null +++ b/tests/data/get_posts.php @@ -0,0 +1,66 @@ +', get_posts()); +assertType('array', get_posts(['key' => 'value'])); +assertType('array', get_posts(['fields' => ''])); +assertType('array', get_posts(['fields' => 'ids'])); +assertType('array', get_posts(['fields' => 'id=>parent'])); +assertType('array', get_posts(['fields' => 'Hello'])); + +// Nonconstant array +assertType('array', get_posts((array)$_GET['array'])); + +// Unions +$union = $_GET['foo'] ? ['key' => 'value'] : ['some' => 'thing']; +assertType('array', get_posts($union)); + +$union = $_GET['foo'] ? ['key' => 'value'] : ['fields' => 'ids']; +assertType('array', get_posts($union)); + +$union = $_GET['foo'] ? ['key' => 'value'] : ['fields' => '']; +assertType('array', get_posts($union)); + +$union = $_GET['foo'] ? ['key' => 'value'] : ['fields' => 'id=>parent']; +assertType('array', get_posts($union)); + +$union = $_GET['foo'] ? ['fields' => ''] : ['fields' => 'ids']; +assertType('array', get_posts($union)); + +$union = $_GET['foo'] ? ['fields' => ''] : ['fields' => 'id=>parent']; +assertType('array', get_posts($union)); + +$union = $_GET['foo'] ? ['fields' => 'ids'] : ['fields' => 'id=>parent']; +assertType('array', get_posts($union)); + +$union = $_GET['foo'] ? (array)$_GET['array'] : ['fields' => '']; +assertType('array', get_posts($union)); + +$union = $_GET['foo'] ? (array)$_GET['array'] : ['fields' => 'ids']; +assertType('array', get_posts($union)); + +$union = $_GET['foo'] ? (array)$_GET['array'] : ['fields' => 'id=>parent']; +assertType('array', get_posts($union)); + +$union = $_GET['foo'] ? (string)$_GET['string'] : ''; +assertType('array', get_posts(['fields' => $union])); + +$union = $_GET['foo'] ? (string)$_GET['string'] : 'ids'; +assertType('array', get_posts(['fields' => $union])); + +$union = $_GET['foo'] ? (string)$_GET['string'] : 'id=>parent'; +assertType('array', get_posts(['fields' => $union])); + +$union = $_GET['foo'] ? (string)$_GET['string'] : 'fields'; +assertType('array', get_posts([$union => ''])); + +$union = $_GET['foo'] ? (string)$_GET['string'] : 'fields'; +assertType('array', get_posts([$union => 'ids'])); + +$union = $_GET['foo'] ? (string)$_GET['string'] : 'fields'; +assertType('array', get_posts([$union => 'id=>parent'])); diff --git a/wordpress-stubs.php b/wordpress-stubs.php index 0c8bb91..e318c6f 100644 --- a/wordpress-stubs.php +++ b/wordpress-stubs.php @@ -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 : array) */ function get_posts($args = \null) {