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

array_merge don't return list type #2642

Closed
b-vadym opened this issue Jan 16, 2020 · 3 comments
Closed

array_merge don't return list type #2642

b-vadym opened this issue Jan 16, 2020 · 3 comments

Comments

@b-vadym
Copy link
Contributor

b-vadym commented Jan 16, 2020

If use array_merge with list arguments return type should be list
https://psalm.dev/r/937292dfcd

<?php
/**
* @param list<int> $foo
*/
function foo(array $foo) : void {
    var_export($foo);
}

$foo1 = [1, 2, 3];
$foo2 = [1, 4, 5];
$fooMerged = array_merge($foo1, $foo2);

foo($foo1);
foo($foo2);
foo($fooMerged);

Actual: ERROR: InvalidArgument - 15:5 - Argument 1 of foo expects list, array{0: int(1), 1: int(2), 2: int(3), 3: int(1), 4: int(4), 5: int(5)} provided
Expected: no errors

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/937292dfcd
<?php
/**
* @param list<int> $foo
*/
function foo(array $foo) : void {
    var_export($foo);
}

$foo1 = [1, 2, 3];
$foo2 = [1, 4, 5];
$fooMerged = array_merge($foo1, $foo2);

foo($foo1);
foo($foo2);
foo($fooMerged);
Psalm output (using commit 26bf183):

ERROR: InvalidArgument - 15:5 - Argument 1 of foo expects list<int>, array{0: int(1), 1: int(2), 2: int(3), 3: int(1), 4: int(4), 5: int(5)} provided

@SignpostMarv
Copy link
Contributor

works if manually declared as list<int>: https://psalm.dev/r/d5c0079603

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/d5c0079603
<?php
/**
* @param list<int> $foo
*/
function foo(array $foo) : void {
    var_export($foo);
}

/** @var list<int> */
$foo1 = [1, 2, 3];
/** @var list<int> */
$foo2 = [1, 4, 5];
$fooMerged = array_merge($foo1, $foo2);

foo($foo1);
foo($foo2);
foo($fooMerged);
Psalm output (using commit 619ced1):

No issues!

@muglug muglug closed this as completed in 24e1d54 Jan 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants