mirror of
https://github.com/danog/psalm.git
synced 2024-11-26 12:24:49 +01:00
Ref #4753 - allow int literals to inform key type
This commit is contained in:
parent
a20db1ce34
commit
1780541501
@ -3755,6 +3755,10 @@ class AssertionFinder
|
||||
foreach ($key_type->getLiteralStrings() as $array_literal_type) {
|
||||
$literal_assertions[] = '=' . $array_literal_type->getId();
|
||||
}
|
||||
} elseif ($key_type->allIntLiterals() && !$key_type->possibly_undefined) {
|
||||
foreach ($key_type->getLiteralInts() as $array_literal_type) {
|
||||
$literal_assertions[] = '=' . $array_literal_type->getId();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1196,6 +1196,17 @@ class Union implements TypeNode
|
||||
return true;
|
||||
}
|
||||
|
||||
public function allIntLiterals() : bool
|
||||
{
|
||||
foreach ($this->types as $atomic_key_type) {
|
||||
if (!$atomic_key_type instanceof TLiteralInt) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function hasLiteralValue() : bool
|
||||
{
|
||||
return $this->literal_int_types
|
||||
|
@ -312,6 +312,21 @@ class ArrayKeyExistsTest extends \Psalm\Tests\TestCase
|
||||
return false;
|
||||
}'
|
||||
],
|
||||
'allowIntKeysToo' => [
|
||||
'<?php
|
||||
/**
|
||||
* @param array<1|2|3, string> $arr
|
||||
* @return 1|2|3
|
||||
*/
|
||||
function checkArrayKeyExistsInt(array $arr, int $int): int
|
||||
{
|
||||
if (array_key_exists($int, $arr)) {
|
||||
return $int;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user