psalm
psalm copied to clipboard
Psalm does not report possibly undefined array key after some manipulations
Example: https://psalm.dev/r/6a044dc469
The last foreach should trigger an error because of the possible execution of the unset($t['a']).
This is the case when removing everything except the last foreach.
I found these snippets:
https://psalm.dev/r/6a044dc469
<?php
/**
* @param array{a?: array<string>} $t
*/
function f(array $t): void {
$a = $t['a'] ?? [];
$t['a'] = [];
foreach ($a as $b) {
$t['a'][] = $b;
}
if (0 === count($t['a'])) {
unset($t['a']);
}
foreach ($t['a'] as $_) {
// This should not be possible, as $t['a'] is possibly undefined
}
}
Psalm output (using commit cdceda0):
No issues!