1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Fix #4624 - allow in_array to work with list arrays

This commit is contained in:
Matt Brown 2020-11-19 09:26:41 -05:00
parent 7c02fa76d1
commit 08ae85a735
2 changed files with 33 additions and 8 deletions

View File

@ -2262,19 +2262,24 @@ class AssertionFinder
foreach ($second_arg_type->getAtomicTypes() as $atomic_type) {
if ($atomic_type instanceof Type\Atomic\TArray
|| $atomic_type instanceof Type\Atomic\TKeyedArray
|| $atomic_type instanceof Type\Atomic\TList
) {
if ($atomic_type instanceof Type\Atomic\TKeyedArray) {
$atomic_type = $atomic_type->getGenericArrayType();
if ($atomic_type instanceof Type\Atomic\TList) {
$key_type = $atomic_type->type_param;
} elseif ($atomic_type instanceof Type\Atomic\TKeyedArray) {
$key_type = $atomic_type->getGenericKeyType();
} else {
$key_type = $atomic_type->type_params[1];
}
$array_literal_types = array_merge(
$atomic_type->type_params[1]->getLiteralStrings(),
$atomic_type->type_params[1]->getLiteralInts(),
$atomic_type->type_params[1]->getLiteralFloats()
$key_type->getLiteralStrings(),
$key_type->getLiteralInts(),
$key_type->getLiteralFloats()
);
if ($array_literal_types
&& count($atomic_type->type_params[1]->getAtomicTypes())
&& count($key_type->getAtomicTypes())
) {
$literal_assertions = [];
@ -2282,11 +2287,11 @@ class AssertionFinder
$literal_assertions[] = '=' . $array_literal_type->getId();
}
if ($atomic_type->type_params[1]->isFalsable()) {
if ($key_type->isFalsable()) {
$literal_assertions[] = 'false';
}
if ($atomic_type->type_params[1]->isNullable()) {
if ($key_type->isNullable()) {
$literal_assertions[] = 'null';
}

View File

@ -781,6 +781,26 @@ class ValueTest extends \Psalm\Tests\TestCase
if (false === ($a > 1)){}
}'
],
'returnFromUnionLiteral' => [
'<?php
/**
* @return list<"a1"|"a2">
*/
function getSupportedConsts() {
return ["a1", "a2"];
}
function foo(mixed $file) : string {
if (in_array($file, getSupportedConsts(), true)) {
return $file;
}
return "";
}',
[],
[],
'8.0'
],
];
}