psalm
psalm copied to clipboard
multiple issues in array destructuring
Issue 1:
when spreading an array with keys that are no literal, psalm does not attempt to resolve the type of the key an access the index as you would manually ( since [$a => $b] = $c is the exact same as $b = $c[$a] ).
Issue 2:
PHP allows skipping elements when destructing, but, this is only allowed when the list/array on the lhs does not have keys, psalm allows this even when keys exist, it should warn/error instead because it will cause a fatal error at runtime.
Example: https://psalm.dev/r/c9cf653295
I found these snippets:
https://psalm.dev/r/c9cf653295
<?php
$arr = ['a' => 1, 'b' => 2];
['a' => $a, 'b' => $b] = $arr;
/**
* @psalm-trace $a
* @psalm-trace $b
*/
echo $a;
echo $b;
$arr = ['a' => 1, 'b' => 2];
$ak = 'a';
$bk = 'b';
// fails to determine keys being accessed, assumes mixed values
[$ak => $a, $bk => $b] = $arr;
/**
* @psalm-trace $a
* @psalm-trace $b
*/
echo $a;
echo $b;
$arr = ['a' => 1, 'b' => 2];
// does not report an error regarding skipping items in keyed destruction
['a' => $a, /* skip */,'b' => $b] = $arr;
/**
* @psalm-trace $a
* @psalm-trace $b
*/
echo $a;
echo $b;
Psalm output (using commit 6670889):
INFO: Trace - 10:1 - $a: 1
INFO: Trace - 10:1 - $b: 2
INFO: MixedArgument - 23:6 - Argument 1 of echo cannot be mixed, expecting string
INFO: Trace - 23:1 - $a: mixed
INFO: Trace - 23:1 - $b: mixed
INFO: MixedArgument - 24:6 - Argument 1 of echo cannot be mixed, expecting string
INFO: Trace - 34:1 - $a: 1
INFO: Trace - 34:1 - $b: 2
INFO: UnusedVariable - 14:1 - $ak is never referenced or the value is not used
INFO: UnusedVariable - 15:1 - $bk is never referenced or the value is not used