psalm
psalm copied to clipboard
False positive with intersection array shape type (used to work with Psalm 5.13)
Hello,
I just tried upgrading from Psalm 5.13 to 5.23.1 and get a new false positive on my code.
https://psalm.dev/r/7e3d196de1
In the loop I check for some required attributes, afterwards the type information for these attributes is corrupted. As you can see in the second function, without the loop psalm will not report this issue, but the loop will not change the array.
I found these snippets:
https://psalm.dev/r/7e3d196de1
<?php
/**
* @psalm-type HttpOptions = array{
* headers?: array<string, string | list<string>>,
* }
*
* @psalm-type SerializedAccount = HttpOptions & array{discoveryUri: string, baseUrl?: ?string}
*/
/**
* @psalm-param SerializedAccount $props
*/
function constructFromArray(array $props): string
{
$requiredProps = [ 'discoveryUri' ];
foreach ($requiredProps as $prop) {
if (!isset($props[$prop])) {
throw new \Exception("x");
}
}
return $props["discoveryUri"];
}
/**
* @psalm-param SerializedAccount $props
*/
function constructFromArray2(array $props): string
{
return $props["discoveryUri"];
}
Psalm output (using commit ef3b018):
ERROR: InvalidReturnStatement - 24:12 - The inferred type 'array<string, list<string>|string>|string' does not match the declared return type 'string' for constructFromArray
ERROR: InvalidReturnType - 14:44 - The declared return type 'string' for constructFromArray is incorrect, got 'array<string, list<string>|string>|string'