When running Psalm on this simple "random string" generator:
$letters = 'abcdefghjklmnpqrstuvwxyz';
$numbers = '23456789';
$number_count = \strlen($numbers);
$letter_count = \strlen($letters);
$pass = '';
while (\strlen($pass) < 9) {
$pass .= $letters[\random_int(0, $letter_count - 1)];
}
while (\strlen($pass) < 12) {
$pass .= $numbers[\random_int(0, $number_count - 1)];
}
I get an error (as seen here):
ERROR: InvalidArrayOffset - 15:11 - Cannot access value on variable $numbers using a int offset, expecting -8|-7|-6|-5|-4|-3|-2|-1|0|1|2|3|4|5|6|7
What I don't understand, is what I get the error on line 11, and not 4 lines earlier, when I'm fetching characters from $letters
.
I'm doing exactly the same both times, yet on the second one it seems psalm can't infer the possible return values from random_int()
, and that they will be within acceptable values. And yet it can the first time.
What I'm doing wrong, and how can I make Psalm understand the code?
I know that the code works, but I am confused why the first $pass
assignment does not trigger an error, but the second one does.
The code is fine. The issue described here is tracked at https://github.com/vimeo/psalm/issues/5458.