New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
array_is_list makes the type less specific. #8046
Comments
I found these snippets: https://psalm.dev/r/ffab419792<?php
/**
* @param array{0: string, 1: string[]} $arr
*/
function test1(array $arr): string {
[$a0, $a1] = $arr;
return $a0 . implode('', $a1);
}
/**
* @param array{0: string, 1: string[]} $arr
*/
function test2(array $arr): string {
if (!array_is_list($arr)) {
throw new Exception();
}
[$a0, $a1] = $arr;
return $a0 . implode('', $a1);
}
|
I'm leaving this open as an enhancement, but your check in Now that destructuring with keys is supported (since PHP 7.1, the same version the short |
It seems meaningless to you, because you do not know the logic. In this case, I have to make sure that the data passed from the external source has 3 elements with keys 0, 1, 2 in ascending order. I do it like this: if (!array_is_list($arr) || count($arr) !== 3) {
throw new InvalidFormatException();
} |
It is meaningless as given in the example, I was just trying to be helpful by pointing it out since it can be somewhat unintuitive. If you have other code where it's not meaningless that's fine, it is something that I think is worth improving in Psalm. |
If I'm not mistaken, the fix should be here:
We're trying to reconcile array{0: string, 1: string[]} with list<mixed> , we retrieved the "GenericArrayType" (should look like array<0|1, string|string[]> I believe), then, we're creating a TNonEmptyList who'll be rendered as non-empty-list<string|string[]> . All that is correct, but we lost that we had 2 definitely set keys. TNonEmptyList has a $count property who should be set to 2 in that case.
|
I actually went a step further to make this possible. |
I found these snippets: https://psalm.dev/r/ec9a06bb45<?php
/**
* @param mixed $arr
* @psalm-assert-if-true list $arr
*/
function is_list($arr): bool
{
return is_array($arr) && array_is_list($arr);
}
/** @var array{0: int, 1: bool, 2: string} */
$list = [1, true, "string"];
assert(is_list($list));
function takesString(string $_str): void {}
takesString($list[2]);
|
https://psalm.dev/r/ffab419792
The test1 function passes the test successfully.
Errors are generated for the test2 function because
array_is_list
turns the$arr
type into anon-empty-list<array<array-key, string>|string>
.The text was updated successfully, but these errors were encountered: