Skip to content

Commit 7b5e911

Browse files
authored
Fix: Remove WP_Error from get_term_by return type (#286)
`get_term_by` calls `get_terms` and `get_term`, both of which can return a `WP_Error` object if the taxonomy specified in `$args['taxonomy']` (corresponding to the `$taxonomy` parameter of `get_term_by`) does not exist. However, `get_term_by` handles any potential errors from `get_term` and `get_terms` by converting them into a `false` return value, thereby ensuring it never directly returns a `WP_Error` object. Removing `WP_Error` from the return type of `get_term_by` reflects the actual behaviour of the function. Closes #284
1 parent 66b4a69 commit 7b5e911

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

functionMap.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@
136136
'get_post_stati' => ["(\$output is 'names' ? array<string, string> : array<string, \stdClass>)"],
137137
'get_comment' => ["(\$comment is \WP_Comment ? array<array-key, mixed>|\WP_Comment : array<array-key, mixed>|\WP_Comment|null) & (\$output is 'ARRAY_A' ? array<string, mixed>|null : (\$output is 'ARRAY_N' ? array<int, mixed>|null : \WP_Comment|null))", 'output' => "'OBJECT'|'ARRAY_A'|'ARRAY_N'"],
138138
'get_post' => ["(\$post is \WP_Post ? array<array-key, mixed>|\WP_Post : array<array-key, mixed>|\WP_Post|null) & (\$output is 'ARRAY_A' ? array<string, mixed>|null : (\$output is 'ARRAY_N' ? array<int, mixed>|null : \WP_Post|null))", 'output' => "'OBJECT'|'ARRAY_A'|'ARRAY_N'" ],
139-
'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))"],
139+
'get_term_by' => ["false|(\$output is 'ARRAY_A' ? array<string, string|int> : (\$output is 'ARRAY_N' ? list<string|int> : \WP_Term))"],
140140
'get_page_by_path' => ["(\$output is 'ARRAY_A' ? array<string, mixed>|null : (\$output is 'ARRAY_N' ? array<int, mixed>|null : \WP_Post|null))"],
141141
'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))", 'output' => "'OBJECT'|'ARRAY_A'|'ARRAY_N'"],
142142
'has_action' => ['($callback is false ? bool : false|int)'],

tests/data/get_term_by.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use function get_term_by;
88
use function PHPStan\Testing\assertType;
99

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('list<int|string>|WP_Error|false', get_term_by('term_id', 2, '', ARRAY_N));
10+
assertType('WP_Term|false', get_term_by('term_id', Faker::int(), '', OBJECT));
11+
assertType('WP_Term|false', get_term_by('slug', 'test'));
12+
assertType('array<string, int|string>|false', get_term_by('term_id', Faker::int(), '', ARRAY_A));
13+
assertType('list<int|string>|false', get_term_by('term_id', Faker::int(), '', ARRAY_N));

wordpress-stubs.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -138433,7 +138433,7 @@ function get_term($term, $taxonomy = '', $output = \OBJECT, $filter = 'raw')
138433138433
* @param string $filter Optional. How to sanitize term fields. Default 'raw'.
138434138434
* @return WP_Term|array|false WP_Term instance (or array) on success, depending on the `$output` value.
138435138435
* False if `$taxonomy` does not exist or `$term` was not found.
138436-
* @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))
138436+
* @phpstan-return false|($output is 'ARRAY_A' ? array<string, string|int> : ($output is 'ARRAY_N' ? list<string|int> : \WP_Term))
138437138437
*/
138438138438
function get_term_by($field, $value, $taxonomy = '', $output = \OBJECT, $filter = 'raw')
138439138439
{

0 commit comments

Comments
 (0)