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

Type narrowing through array_filter doesn't work #4976

Closed
fluffycondor opened this issue Jan 11, 2021 · 3 comments
Closed

Type narrowing through array_filter doesn't work #4976

fluffycondor opened this issue Jan 11, 2021 · 3 comments
Labels

Comments

@fluffycondor
Copy link
Contributor

https://psalm.dev/r/a6ff20c81e

Expected: array<array-key, \stdClass>
Got: array<array-key, mixed>

@psalm-github-bot
Copy link

I found these snippets:

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

/** @psalm-suppress MixedAssignment */
$array = json_decode(file_get_contents('php://stdin'));

if (is_array($array) && $array === array_filter($array, function ($value): bool {
    return $value instanceof stdClass;
})) {
    /** @psalm-trace $array */;
}
Psalm output (using commit 1046477):

INFO: Trace - 9:31 - $array: array<array-key, mixed>

@muglug
Copy link
Collaborator

muglug commented Jan 11, 2021

The type should definitely get filtered here: https://psalm.dev/r/4281721dda

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/4281721dda
<?php

/** @psalm-suppress MixedAssignment */
$array = json_decode(file_get_contents('php://stdin'));

if (is_array($array)) {
    $filtered = array_filter($array, fn ($value) => $value instanceof stdClass);
    
    if ($array === $filtered) {
       	/** @psalm-trace $array */
    }
}
Psalm output (using commit 1046477):

INFO: Trace - 10:0 - $array: array<array-key, mixed>

@muglug muglug closed this as completed in 4faa6bb Jan 11, 2021
danog pushed a commit to danog/psalm that referenced this issue Jan 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants