phpstan
phpstan copied to clipboard
rand() values are ignored
Bug report
PHPStan does not detect the parameter values passed to the rand() function. In my example snippet, you can see how it fails to understand that the input is always set, because the input values are either 1 or 2.
Code snippet that reproduces the problem
https://phpstan.org/r/0a3522ab-1db0-4fe5-96af-bec1b3a60ee1
Expected output
No error
Did PHPStan help you today? Did it make you happy in any way?
PHPStan is awesome, thank you!
phpstan does understand how the parameters of rand affect it's output, and you can see that in this playground:
https://phpstan.org/r/03208a7b-ec5d-4795-9f7c-3932dd793dcb
i've also added an example using the match statement that phpstan understands, because match is easier to make static guarantees about than multiple if/else statements.
analysing multiple if/else exhaustively would be quite a difficult thing to achieve and probably computationally expensive
The problem isnn't about rand(), it's about exhaustiveness checking of int<1, 2> with === in the if statement.
@mad-briller After the latest push in 1.8.x, PHPStan now reports different result with your code snippet:
@@ @@
10: Variable $input might not be defined.
12: Variable $input might not be defined.
19: Match arm is unreachable because previous comparison is always true.
-22: Dumped type: 'foo bar'|'invalid'|array{'foo bar'}
+22: Dumped type: 'foo bar'|array{'foo bar'}
PHP 7.1 – 7.4 (2 errors)
==========
Full report
PHP 8.0 – 8.1 (6 errors)
| Line | Error |
|---|---|
| 3 | Dumped type: int<1, 2> |
| 10 | Dumped type: mixed |
| 10 | Variable $input might not be defined. |
| 12 | Variable $input might not be defined. |
| 19 | Match arm is unreachable because previous comparison is always true. |
| 22 | `Dumped type: 'foo bar' |
PHP 7.1 – 7.4 (2 errors)
| Line | Error |
|---|---|
| 17 | Syntax error, unexpected T_DOUBLE_ARROW on line 17 |
| 18 | Syntax error, unexpected ',' on line 18 |
@LauraTaylorUK After the latest push in 1.9.x, PHPStan now reports different result with your code snippet:
@@ @@
-9: Variable $input might not be defined.
+No errors
@mad-briller After the latest push in 1.9.x, PHPStan now reports different result with your code snippet:
@@ @@
-PHP 8.0 – 8.1 (6 errors)
+PHP 8.0 – 8.1 (4 errors)
==========
3: Dumped type: int<1, 2>
-10: Dumped type: mixed
-10: Variable $input might not be defined.
-12: Variable $input might not be defined.
+10: Dumped type: 'foo bar'|array{'foo bar'}
19: Match arm is unreachable because previous comparison is always true.
-22: Dumped type: 'foo bar'|'invalid'|array{'foo bar'}
+22: Dumped type: 'foo bar'|array{'foo bar'}
PHP 7.1 – 7.4 (2 errors)
==========
Full report
PHP 8.0 – 8.1 (4 errors)
| Line | Error |
|---|---|
| 3 | Dumped type: int<1, 2> |
| 10 | `Dumped type: 'foo bar' |
| 19 | Match arm is unreachable because previous comparison is always true. |
| 22 | `Dumped type: 'foo bar' |
PHP 7.1 – 7.4 (2 errors)
| Line | Error |
|---|---|
| 17 | Syntax error, unexpected T_DOUBLE_ARROW on line 17 |
| 18 | Syntax error, unexpected ',' on line 18 |
@LauraTaylorUK After the latest push in 1.10.x, PHPStan now reports different result with your code snippet:
@@ @@
-9: Variable $input might not be defined.
+No errors
@mad-briller After the latest push in 1.10.x, PHPStan now reports different result with your code snippet:
@@ @@
-PHP 8.0 – 8.1 (6 errors)
+PHP 8.0 – 8.1 (4 errors)
==========
3: Dumped type: int<1, 2>
-10: Dumped type: mixed
-10: Variable $input might not be defined.
-12: Variable $input might not be defined.
+10: Dumped type: 'foo bar'|array{'foo bar'}
19: Match arm is unreachable because previous comparison is always true.
-22: Dumped type: 'foo bar'|'invalid'|array{'foo bar'}
+22: Dumped type: 'foo bar'|array{'foo bar'}
PHP 7.1 – 7.4 (2 errors)
==========
Full report
PHP 8.0 – 8.1 (4 errors)
| Line | Error |
|---|---|
| 3 | Dumped type: int<1, 2> |
| 10 | `Dumped type: 'foo bar' |
| 19 | Match arm is unreachable because previous comparison is always true. |
| 22 | `Dumped type: 'foo bar' |
PHP 7.1 – 7.4 (2 errors)
| Line | Error |
|---|---|
| 17 | Syntax error, unexpected T_DOUBLE_ARROW on line 17 |
| 18 | Syntax error, unexpected ',' on line 18 |
@LauraTaylorUK After the latest push in 1.10.x, PHPStan now reports different result with your code snippet:
@@ @@
-9: Variable $input might not be defined.
+No errors
@mad-briller After the latest push in 1.10.x, PHPStan now reports different result with your code snippet:
@@ @@
-PHP 8.0 – 8.1 (6 errors)
+PHP 8.0 – 8.1 (4 errors)
==========
3: Dumped type: int<1, 2>
-10: Dumped type: mixed
-10: Variable $input might not be defined.
-12: Variable $input might not be defined.
+10: Dumped type: 'foo bar'|array{'foo bar'}
19: Match arm is unreachable because previous comparison is always true.
-22: Dumped type: 'foo bar'|'invalid'|array{'foo bar'}
+22: Dumped type: 'foo bar'|array{'foo bar'}
PHP 7.1 – 7.4 (2 errors)
==========
Full report
PHP 8.0 – 8.1 (4 errors)
| Line | Error |
|---|---|
| 3 | Dumped type: int<1, 2> |
| 10 | `Dumped type: 'foo bar' |
| 19 | Match arm is unreachable because previous comparison is always true. |
| 22 | `Dumped type: 'foo bar' |
PHP 7.1 – 7.4 (2 errors)
| Line | Error |
|---|---|
| 17 | Syntax error, unexpected T_DOUBLE_ARROW on line 17 |
| 18 | Syntax error, unexpected ',' on line 18 |
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.