psalm icon indicating copy to clipboard operation
psalm copied to clipboard

Psalm does not report possibly undefined array key after some manipulations

Open chapa opened this issue 4 months ago • 1 comments

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.

chapa avatar Aug 14 '25 15:08 chapa

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!

psalm-github-bot[bot] avatar Aug 14 '25 15:08 psalm-github-bot[bot]