Skip to content
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

Typing filter_var(FILTER_VALIDATE_INT) when specified min_range and/or max_range #7494

Closed
zonuexe opened this issue Jun 19, 2022 · 1 comment · Fixed by phpstan/phpstan-src#1443

Comments

@zonuexe
Copy link
Contributor

zonuexe commented Jun 19, 2022

Feature request

It is convenient to specify the range of filter_var and add the IntergerRangeType type to the value.

$return = filter_var($str, FILTER_VALIDATE_INT);
assertType('int|false', $return);

$return = filter_var($str, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
assertType('int<1, max>|false', $return);

$return = filter_var($str, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1], 'flags' => FILTER_NULL_ON_FAILURE]);
assertType('int<1, max>|null', $return);

$return = filter_var($str, FILTER_VALIDATE_INT, ['options' => ['max_range' => 0]]);
assertType('int<min, 0>|false', $return);

$return = filter_var($str, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1, 'max_range' => 9]]);
assertType('int<1, 9>|false', $return);

$return = filter_var(100, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1, 'max_range' => 9]]);
assertType('false', $return);

$return = filter_var(1, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1, 'max_range' => 9]]);
assertType('1', $return);

$return = filter_var(9, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1, 'max_range' => 9]]);
assertType('9', $return);

$return = filter_var($str, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1, 'max_range' => $positive_int]]);
assertType('int<1, max>|false', $return);

$return = filter_var($str, FILTER_VALIDATE_INT, ['options' => ['min_range' => $negative_int, 'max_range' => 0]]);
assertType('int<min, 0>|false', $return);

$return = filter_var($str, FILTER_VALIDATE_INT, ['options' => ['min_range' => $int, 'max_range' => $int]]);
assertType('int|false', $return);

https://phpstan.org/r/8d61b53c-d432-4cb8-befc-41662c73157e

Did PHPStan help you today? Did it make you happy in any way?

I've made some feature request, trivial fixes and taught my friends how to hack PHPStan, but strangely, it's the first time I've had the chance to add features to the PHPStan core. 😄

@zonuexe zonuexe changed the title Typing filter_var(FILTER_VALIDATE_INT) when specified range_min and/or range_max Typing filter_var(FILTER_VALIDATE_INT) when specified min_range and/or max_range Jun 20, 2022
@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants