Skip to content

Commit e53228e

Browse files
authored
Add dynamic return type for the get_term_by function (#105)
- Return `array<int|string>` if `$output = 'ARRAY_N'`. - Return `array<string, int|string>` if default or `$output = 'ARRAY_A'`. - Return `WP_Term` if default or `$output = 'OBJECT'`. https://developer.wordpress.org/reference/functions/get_term_by/#parameters
1 parent 571e697 commit e53228e

File tree

4 files changed

+16
-0
lines changed

4 files changed

+16
-0
lines changed

functionMap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
'get_post_stati' => ["(\$output is 'names' ? array<string, string> : array<string, \stdClass>)"],
9090
'get_comment' => ["(\$output is 'ARRAY_A' ? array<string, mixed>|null : (\$output is 'ARRAY_N' ? array<int, mixed>|null : \WP_Comment|null))"],
9191
'get_post' => ["(\$output is 'ARRAY_A' ? array<string, mixed>|null : (\$output is 'ARRAY_N' ? array<int, mixed>|null : \WP_Post|null))"],
92+
'get_term_by' => ["(\$output is 'ARRAY_A' ? array<string, string|int>|\WP_Error|false : (\$output is 'ARRAY_N' ? list<string|int>|\WP_Error|false : \WP_Term|\WP_Error|false))"],
9293
'get_page_by_path' => ["(\$output is 'ARRAY_A' ? array<string, mixed>|null : (\$output is 'ARRAY_N' ? array<int, mixed>|null : \WP_Post|null))"],
9394
'get_term' => ["(\$output is 'ARRAY_A' ? array<string, string|int>|\WP_Error|null : (\$output is 'ARRAY_N' ? list<string|int>|\WP_Error|null : \WP_Term|\WP_Error|null))"],
9495
'has_action' => ['($callback is false ? bool : false|int)'],

tests/TypeInferenceTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public function dataFileAsserts(): iterable
1919
yield from $this->gatherAssertTypes(__DIR__ . '/data/get_post_types.php');
2020
yield from $this->gatherAssertTypes(__DIR__ . '/data/get_page_by_path.php');
2121
yield from $this->gatherAssertTypes(__DIR__ . '/data/get_permalink.php');
22+
yield from $this->gatherAssertTypes(__DIR__ . '/data/get_term_by.php');
2223
yield from $this->gatherAssertTypes(__DIR__ . '/data/get_taxonomies.php');
2324
yield from $this->gatherAssertTypes(__DIR__ . '/data/get_term.php');
2425
yield from $this->gatherAssertTypes(__DIR__ . '/data/get_taxonomies_for_attachments.php');

tests/data/get_term_by.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare( strict_types=1 );
4+
5+
namespace PhpStubs\WordPress\Core\Tests;
6+
7+
use function get_term_by;
8+
use function PHPStan\Testing\assertType;
9+
10+
assertType( 'WP_Error|WP_Term|false', get_term_by( 'term_id', 2, '', OBJECT ) );
11+
assertType( 'WP_Error|WP_Term|false', get_term_by( 'slug', 'test' ) );
12+
assertType( 'array<string, int|string>|WP_Error|false', get_term_by( 'term_id', 2, '', ARRAY_A ) );
13+
assertType( 'array<int, int|string>|WP_Error|false', get_term_by( 'term_id', 2, '', ARRAY_N ) );

wordpress-stubs.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129509,6 +129509,7 @@ function get_term($term, $taxonomy = '', $output = \OBJECT, $filter = 'raw')
129509129509
* @param string $filter Optional. How to sanitize term fields. Default 'raw'.
129510129510
* @return WP_Term|array|false WP_Term instance (or array) on success, depending on the `$output` value.
129511129511
* False if `$taxonomy` does not exist or `$term` was not found.
129512+
* @phpstan-return ($output is 'ARRAY_A' ? array<string, string|int>|\WP_Error|false : ($output is 'ARRAY_N' ? list<string|int>|\WP_Error|false : \WP_Term|\WP_Error|false))
129512129513
*/
129513129514
function get_term_by($field, $value, $taxonomy = '', $output = \OBJECT, $filter = 'raw')
129514129515
{

0 commit comments

Comments
 (0)