Skip to content

Add conditional return type for get_user() and get_user_by() #263

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions functionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,6 @@
'WP_Widget_Factory::unregister' => [null, 'widget' => 'class-string<\WP_Widget>|\WP_Widget'],
'Custom_Image_Header::show_header_selector' => [null, 'type' => "'default'|'uploaded'"],
'Custom_Image_Header::set_header_image' => [null, 'choice' => 'string|array{attachment_id: int<1, max>, url: string, width: int<0, max>, height: int<0, max>}'],
'get_user' => ['($user_id is int<min, 0> ? false : \WP_User|false)'],
'get_user_by' => ["(\$field is 'id'|'ID' ? (\$value is int<min, 0> ? false : \WP_User|false) : \WP_User|false)"],
];
2 changes: 2 additions & 0 deletions tests/TypeInferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/get_taxonomies.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/get_term.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/get_taxonomies_for_attachments.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/get_user.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/get_user_by.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/has_filter.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/is_new_day.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/is_wp_error.php');
Expand Down
16 changes: 16 additions & 0 deletions tests/data/get_user.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace PhpStubs\WordPress\Core\Tests;

use function get_user;
use function PHPStan\Testing\assertType;

assertType('WP_User|false', get_user(Faker::int()));
assertType('WP_User|false', get_user(Faker::positiveInt()));
assertType('WP_User|false', get_user(1));

assertType('false', get_user(Faker::nonPositiveInt()));
assertType('false', get_user(-1));
assertType('false', get_user(0));
22 changes: 22 additions & 0 deletions tests/data/get_user_by.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace PhpStubs\WordPress\Core\Tests;

use function get_user_by;
use function PHPStan\Testing\assertType;

assertType('WP_User|false', get_user_by('id', Faker::int()));
assertType('WP_User|false', get_user_by('id', Faker::positiveInt()));
assertType('WP_User|false', get_user_by('id', 1));
assertType('WP_User|false', get_user_by('ID', Faker::int()));
assertType('WP_User|false', get_user_by('ID', Faker::positiveInt()));
assertType('WP_User|false', get_user_by('ID', 1));

assertType('false', get_user_by('id', Faker::nonPositiveInt()));
assertType('false', get_user_by('id', -1));
assertType('false', get_user_by('id', 0));
assertType('false', get_user_by('ID', Faker::nonPositiveInt()));
assertType('false', get_user_by('ID', -1));
assertType('false', get_user_by('ID', 0));
2 changes: 2 additions & 0 deletions wordpress-stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -127773,6 +127773,7 @@ function get_userdata($user_id)
* @param string $field The field to retrieve the user with. id | ID | slug | email | login.
* @param int|string $value A value for $field. A user ID, slug, email address, or login name.
* @return WP_User|false WP_User object on success, false on failure.
* @phpstan-return ($field is 'id'|'ID' ? ($value is int<min, 0> ? false : \WP_User|false) : \WP_User|false)
*/
function get_user_by($field, $value)
{
Expand Down Expand Up @@ -141395,6 +141396,7 @@ function delete_user_option($user_id, $option_name, $is_global = \false)
* @param int $user_id User ID.
*
* @return WP_User|false WP_User object on success, false on failure.
* @phpstan-return ($user_id is int<min, 0> ? false : \WP_User|false)
*/
function get_user($user_id)
{
Expand Down