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

mt_rand assumed to always return the same value #4091

Closed
GKFX opened this issue Nov 16, 2020 · 7 comments
Closed

mt_rand assumed to always return the same value #4091

GKFX opened this issue Nov 16, 2020 · 7 comments
Labels
Milestone

Comments

@GKFX
Copy link

GKFX commented Nov 16, 2020

Bug report

mt_rand returns a random number. The second if statement in the below is therefore not redundant. However PHPStan reports Comparison operation ">" between int<4, max> and 3 is always true..

<?php

if (mt_rand(0,10) > 3) {
    echo 'Fizz';
    if (mt_rand(0,10) > 3) {
        echo 'Buzz';
    }
}
echo "\n";

Code snippet that reproduces the problem

https://phpstan.org/r/43bf48f4-947d-4d96-9fc1-33e25fde3adb

Expected output

No output expected. Arguably, PHPStan should never assume that functions return the same value when called twice with the same arguments, but as this is a standard-library random number function it seems particularly important.

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Nov 16, 2020
@dktapps
Copy link
Contributor

dktapps commented Nov 16, 2020

PHPStan should never assume that functions return the same value when called twice with the same arguments

In some cases the results are entirely deterministic (pure). mt_rand() is not a pure function though.

@GKFX
Copy link
Author

GKFX commented Nov 16, 2020

PHPStan should never assume that functions return the same value when called twice with the same arguments

In some cases the results are entirely deterministic (pure). mt_rand() is not a pure function though.

Should PHPStan have an annotation for pure and impure functions then, defaulting to impure? It seems to me that assuming functions are not pure by default would be safe, but the current assumption that functions are always pure is false and causes this issue.

@dktapps
Copy link
Contributor

dktapps commented Nov 16, 2020

@dktapps
Copy link
Contributor

dktapps commented Nov 17, 2020

I guess this will be fixed next time Ondrej updates phpstorm-stubs and regenerates the function metadata.

@phpstan-bot
Copy link
Contributor

@GKFX After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-5: Comparison operation ">" between int<4, max> and 3 is always true.
+No errors

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@d4edc59

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

No branches or pull requests

4 participants