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

Parameter with integer range in phpdoc raises error during check even with "treatPhpDocTypesAsCertain" set to false #7075

Closed
lcdennison opened this issue Apr 21, 2022 · 6 comments · Fixed by phpstan/phpstan-src#2191
Labels
Milestone

Comments

@lcdennison
Copy link

Bug report

Using 1.5.7.

I have treatPhpDocTypesAsCertain: false in my phpstan config, and that directive is working in other scenarios. However, when I apply an integer range to a parameter, and then try to check that condition (so I can throw an Exception), phpstan is saying the condition is always false. Simple example:

/**
 * @param int<1,max> $number
 */
function foo(int $number): void {
    if ($number < 1) { // error raised because condition is "always false"
        throw new \Exception('Number cannot be less than 1');
    }
}

Shouldn't this error be suppressed by treatPhpDocTypesAsCertain set to false? Or is there some other way to handle this case?

Code snippet that reproduces the problem

https://phpstan.org/r/be1acdf0-5115-40e8-b235-0fe01c94f075

Expected output

I expect no error to be raised.

@ondrejmirtes
Copy link
Member

This is easy to fix. These nodes (Smaller etc.) need the same handling in MutatingScope::resolveType as for example Identical has here: https://github.com/phpstan/phpstan-src/blob/796c9d5f85736de214cad0ff7058eec83967429c/src/Analyser/MutatingScope.php#L882-L888

@thg2k
Copy link

thg2k commented Aug 15, 2022

Same story, I ran into this problem:

https://phpstan.org/r/ace27b7e-79a6-40cc-8888-e27b49a46073

@thg2k
Copy link

thg2k commented Aug 16, 2022

I made I first attempt at this. Although it works for fixing the OP issue, I still would like to do a bit more of refactoring. Let's see how it goes.

@phpstan-bot
Copy link
Contributor

@lcdennison After the latest push in 1.10.x, PHPStan now reports different result with your code snippet:

@@ @@
-7: Comparison operation "<" between int<1, max> and 1 is always false.
+No errors

@phpstan-bot
Copy link
Contributor

@thg2k After the latest push in 1.10.x, PHPStan now reports different result with your code snippet:

@@ @@
-5: Comparison operation ">" between int<0, 100> and 100 is always false.
+No errors

@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 Feb 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants