Skip to content
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

assert(!array_is_list()) changes type to be maybe list #9348

Open
othercorey opened this issue Feb 20, 2023 · 4 comments
Open

assert(!array_is_list()) changes type to be maybe list #9348

othercorey opened this issue Feb 20, 2023 · 4 comments

Comments

@othercorey
Copy link
Contributor

othercorey commented Feb 20, 2023

A parameter of type array<string, mixed is passed to another parameter of the same type array<string, mixed>. However, when introducing an assert that it is associative, it seems to think the array could now be a list.

https://psalm.dev/r/715994eafd

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/c7b80db58a
<?php

/** param array<string, mixed> $input1 */
function test(array $input1): array {
    return $input1;
}

/** param array<string, mixed> $input2 */
function test2(array $input2 = []): void {
    assert(empty($input2) || !array_is_list($input2), "Must be associative");
    
    test($input2);
}
Psalm output (using commit 8e0fd88):

No issues!

@weirdan
Copy link
Collaborator

weirdan commented Feb 20, 2023

@othercorey you omitted @ in @param, that's why you couldn't reproduce it on psalm.dev. Here's the fixed reproducer: https://psalm.dev/r/715994eafd

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/715994eafd
<?php

/** @param array<string, mixed> $input1 */
function test(array $input1): array {
    return $input1;
}

/** @param array<string, mixed> $input2 */
function test2(array $input2 = []): void {
    assert(empty($input2) || !array_is_list($input2), "Must be associative");
    
    test($input2);
}
Psalm output (using commit 8e0fd88):

ERROR: InvalidArgument - 12:10 - Argument 1 of test expects array<string, mixed>, but array<int<0, max>|string, mixed> provided

@othercorey
Copy link
Contributor Author

Thanks. I updated the description with your example so it's not confusing to read.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants