1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 12:24:49 +01:00

Fix #4848 - use better inference when incorrect array keys are passed

This commit is contained in:
Matt Brown 2020-12-16 08:07:57 -05:00 committed by Daniil Gentili
parent c417769674
commit e87617e598
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7

View File

@ -126,14 +126,17 @@ class ArrayAnalyzer
}
if ($item_key_type) {
$bad_types = [];
$good_types = [];
foreach ($item_key_type->getAtomicTypes() as $atomic_key_type) {
if (!$atomic_key_type instanceof Type\Atomic\TString &&
!$atomic_key_type instanceof Type\Atomic\TInt &&
!$atomic_key_type instanceof Type\Atomic\TArrayKey &&
!$atomic_key_type instanceof Type\Atomic\TMixed &&
!(
$atomic_key_type instanceof Type\Atomic\TObjectWithProperties &&
isset($atomic_key_type->methods['__toString'])
if (!$atomic_key_type instanceof Type\Atomic\TString
&& !$atomic_key_type instanceof Type\Atomic\TInt
&& !$atomic_key_type instanceof Type\Atomic\TArrayKey
&& !$atomic_key_type instanceof Type\Atomic\TMixed
&& !(
$atomic_key_type instanceof Type\Atomic\TObjectWithProperties
&& isset($atomic_key_type->methods['__toString'])
)
) {
if (IssueBuffer::accepts(
@ -143,10 +146,30 @@ class ArrayAnalyzer
),
$statements_analyzer->getSuppressedIssues()
)) {
$item_key_type = Type::getArrayKey();
$bad_types[] = $atomic_key_type;
if ($atomic_key_type instanceof Type\Atomic\TFalse) {
$good_types[] = new Type\Atomic\TLiteralInt(0);
} elseif ($atomic_key_type instanceof Type\Atomic\TTrue) {
$good_types[] = new Type\Atomic\TLiteralInt(1);
} elseif ($atomic_key_type instanceof Type\Atomic\TBool) {
$good_types[] = new Type\Atomic\TLiteralInt(0);
$good_types[] = new Type\Atomic\TLiteralInt(1);
} elseif ($atomic_key_type instanceof Type\Atomic\TFloat) {
$good_types[] = new Type\Atomic\TInt;
} else {
$good_types[] = new Type\Atomic\TArrayKey;
}
}
}
}
if ($bad_types && $good_types) {
$item_key_type->substitute(
TypeCombiner::combine($bad_types, $codebase),
TypeCombiner::combine($good_types, $codebase)
);
}
}
$array_type = new Type\Atomic\TNonEmptyArray([