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

Add list identification for late-resolved constants

Fixes #2624
This commit is contained in:
Brown 2020-01-15 11:54:01 -05:00
parent 1b551b805b
commit d434f7fb9d
2 changed files with 30 additions and 4 deletions

View File

@ -1624,6 +1624,8 @@ class ClassLikes
return new Type\Atomic\TArray([Type::getEmpty(), Type::getEmpty()]);
}
$is_list = true;
foreach ($c->entries as $i => $entry) {
if ($entry->key) {
$key_type = $this->resolveConstantType(
@ -1631,6 +1633,12 @@ class ClassLikes
$statements_analyzer,
$visited_constant_ids + [$c_id => true]
);
if (!$key_type instanceof Type\Atomic\TLiteralInt
|| $key_type->value !== $i
) {
$is_list = false;
}
} else {
$key_type = new Type\Atomic\TLiteralInt($i);
}
@ -1652,7 +1660,11 @@ class ClassLikes
$properties[$key_value] = $value_type;
}
return new Type\Atomic\ObjectLike($properties);
$objectlike = new Type\Atomic\ObjectLike($properties);
$objectlike->is_list = $is_list;
return $objectlike;
}
if ($c instanceof UnresolvedConstant\ClassConstant) {

View File

@ -461,13 +461,26 @@ class ConstantTest extends TestCase
],
'resolveConstArrayAsList' => [
'<?php
class Test {
public const VALUES = [
class Test1 {
const VALUES = [
"all",
"own"
];
}
class Credentials {
const ALL = "all";
const OWN = "own";
const NONE = "none";
}
class Test2 {
const VALUES = [
Credentials::ALL,
Credentials::OWN
];
}
/**
* @psalm-param list<"all"|"own"|"mine"> $value
*/
@ -475,7 +488,8 @@ class ConstantTest extends TestCase
print_r($value);
}
test(Test::VALUES);'
test(Test1::VALUES);
test(Test2::VALUES);'
],
'resolveConstantFetchViaFunction' => [
'<?php