Skip to content

Commit

Permalink
Add list identification for late-resolved constants
Browse files Browse the repository at this point in the history
Fixes #2624
  • Loading branch information
muglug committed Jan 15, 2020
1 parent 1b551b8 commit d434f7f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
14 changes: 13 additions & 1 deletion src/Psalm/Internal/Codebase/ClassLikes.php
Expand Up @@ -1624,13 +1624,21 @@ private function resolveConstantType(
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(
$entry->key,
$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);
}
Expand All @@ -1652,7 +1660,11 @@ private function resolveConstantType(
$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) {
Expand Down
20 changes: 17 additions & 3 deletions tests/ConstantTest.php
Expand Up @@ -461,21 +461,35 @@ class Foo {
],
'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
*/
function test($value): void {
print_r($value);
}
test(Test::VALUES);'
test(Test1::VALUES);
test(Test2::VALUES);'
],
'resolveConstantFetchViaFunction' => [
'<?php
Expand Down

0 comments on commit d434f7f

Please sign in to comment.