diff --git a/src/Psalm/Internal/Codebase/ClassLikes.php b/src/Psalm/Internal/Codebase/ClassLikes.php index c0cb23193c6..f0c45490a76 100644 --- a/src/Psalm/Internal/Codebase/ClassLikes.php +++ b/src/Psalm/Internal/Codebase/ClassLikes.php @@ -1624,6 +1624,8 @@ 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( @@ -1631,6 +1633,12 @@ private function resolveConstantType( $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 @@ 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) { diff --git a/tests/ConstantTest.php b/tests/ConstantTest.php index c83d71ae020..73855089839 100644 --- a/tests/ConstantTest.php +++ b/tests/ConstantTest.php @@ -461,13 +461,26 @@ class Foo { ], 'resolveConstArrayAsList' => [ ' $value */ @@ -475,7 +488,8 @@ function test($value): void { print_r($value); } - test(Test::VALUES);' + test(Test1::VALUES); + test(Test2::VALUES);' ], 'resolveConstantFetchViaFunction' => [ '