phpstan icon indicating copy to clipboard operation
phpstan copied to clipboard

rand() values are ignored

Open LauraTaylorUK opened this issue 3 years ago • 3 comments
trafficstars

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!

LauraTaylorUK avatar Jun 29 '22 08:06 LauraTaylorUK

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

mad-briller avatar Jul 05 '22 18:07 mad-briller

The problem isnn't about rand(), it's about exhaustiveness checking of int<1, 2> with === in the if statement.

ondrejmirtes avatar Sep 21 '22 11:09 ondrejmirtes

@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

phpstan-bot avatar Sep 21 '22 14:09 phpstan-bot

@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

phpstan-bot avatar Jan 10 '23 14:01 phpstan-bot

@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

phpstan-bot avatar Jan 10 '23 14:01 phpstan-bot

@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

phpstan-bot avatar Jan 10 '23 14:01 phpstan-bot

@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

phpstan-bot avatar Jan 10 '23 14:01 phpstan-bot

@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

phpstan-bot avatar Jan 10 '23 14:01 phpstan-bot

@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

phpstan-bot avatar Jan 10 '23 14:01 phpstan-bot

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[bot] avatar Feb 11 '23 00:02 github-actions[bot]