Skip to content

Commit 9ce8173

Browse files
authored
Fix return type of sanitize_post (#381)
1 parent 33178a4 commit 9ce8173

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

functionMap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@
133133
'rest_ensure_response' => ['($response is \WP_Error ? \WP_Error : \WP_REST_Response)'],
134134
'sanitize_bookmark_field' => ['array<int, int>|int|string', 'field' => "'link_id'|'link_url'|'link_name'|'link_image'|'link_target'|'link_description'|'link_visible'|'link_owner'|'link_rating'|'link_updated'|'link_rel'|'link_notes'|'link_rss'|'link_category'"],
135135
'sanitize_category' => ['T', '@phpstan-template' => 'T of array|object', 'category' => 'T'],
136-
'sanitize_post' => ['T', '@phpstan-template' => 'T of array|object', 'post' => 'T'],
136+
'sanitize_post' => ['(T is \WP_Post ? \WP_Post : (T is object ? object : (T is array ? array : T)))', '@phpstan-template T' => 'of mixed', 'post' => 'T'],
137137
'sanitize_sql_orderby' => ['(T is non-falsy-string ? T|false : false)', '@phpstan-template T' => 'of string', 'orderby' => 'T'],
138138
'sanitize_term' => ['T', '@phpstan-template' => 'T of array|object', 'term' => 'T'],
139139
'sanitize_title_with_dashes' => ['lowercase-string', 'context' => "'display'|'save'"],
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpStubs\WordPress\Core\Tests;
6+
7+
use stdClass;
8+
9+
use function sanitize_post;
10+
use function PHPStan\Testing\assertType;
11+
12+
assertType('WP_Post', sanitize_post(Faker::wpPost(), Faker::string()));
13+
assertType('object', sanitize_post(Faker::object(), Faker::string()));
14+
assertType('object', sanitize_post(new stdClass(), Faker::string()));
15+
assertType('array', sanitize_post(Faker::array(), Faker::string()));
16+
assertType('array', sanitize_post(Faker::strArray(), Faker::string()));
17+
assertType('array', sanitize_post(Faker::intArray(), Faker::string()));
18+
assertType('array', sanitize_post(Faker::array(Faker::string()), Faker::string()));
19+
20+
// Incorrect type of $post is returned as-is.
21+
assertType("'foo'", sanitize_post('foo', Faker::string()));
22+
assertType('123', sanitize_post(123, Faker::string()));
23+
assertType('string', sanitize_post(Faker::string(), Faker::string()));
24+
assertType('int', sanitize_post(Faker::int(), Faker::string()));
25+
assertType('bool', sanitize_post(Faker::bool(), Faker::string()));

wordpress-stubs.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132335,9 +132335,9 @@ function is_sticky($post_id = 0)
132335132335
* @return object|WP_Post|array The now sanitized post object or array (will be the
132336132336
* same type as `$post`).
132337132337
* @phpstan-param 'raw'|'edit'|'db'|'display'|'attribute'|'js' $context
132338-
* @phpstan-template T of array|object
132338+
* @phpstan-template T of mixed
132339132339
* @phpstan-param T $post
132340-
* @phpstan-return T
132340+
* @phpstan-return (T is \WP_Post ? \WP_Post : (T is object ? object : (T is array ? array : T)))
132341132341
*/
132342132342
function sanitize_post($post, $context = 'display')
132343132343
{

0 commit comments

Comments
 (0)