Errors about array offsets #14790
-
|
I have an HTML form where I build a set of checkboxes from two arrays: <input type="checkbox" name="selection[0][0]" value="1">
<input type="checkbox" name="selection[0][1]" value="1">
[...]To cater with the fact that unchecked checkboxes are not sent by the browser, when I process the form I first build a normalised two-dimension array with the appropriate boolean values, and then base all my logic on that. No matter what I do, PHPStan will always complain. In the full original code:
In a simplified version I've prepared to diagnose and share: <?php declare(strict_types = 1);
$from = ['a', 'b', 'c'];
$to = ['a', 'b', 'c', 'd', 'd'];
/** @var array<int, array<int, bool>> $normalizedSelection */ // <- My attempt to fix linter errors
$normalizedSelection = [];
foreach ($from as $i => $f) {
foreach ($to as $j => $t) {
$normalizedSelection = false;
}
}
foreach ($from as $i => $f) {
foreach ($to as $j => $t) {
if (!$normalizedSelection[$i][$j]) { // <-- Both errors here
continue;
}
}
}
Is there a bug in my code? I'm reluctant to think there' a bug in PHPStan, because it looks like a very simple scenario to me. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
|
Please show the behaviour on phpstan.org/try. |
Beta Was this translation helpful? Give feedback.
These errors are only reported with
reportPossiblyNonexistentGeneralArrayOffset: true.It’s not obvious to PHPStan you’re iterating over the same keys that were used to create the array.