Skip to content
Permalink
Browse files

Track list-ness of constant arrays

Fixes #2621
  • Loading branch information
muglug committed Jan 14, 2020
1 parent c79a8ec commit 1ccababcb72f5854381955e1b919b8f5268816d5
Showing with 29 additions and 0 deletions.
  1. +11 −0 src/Psalm/Internal/Analyzer/StatementsAnalyzer.php
  2. +18 −0 tests/ConstantTest.php
@@ -1811,6 +1811,8 @@ public static function getSimpleType(

$can_create_objectlike = true;

$is_list = true;

foreach ($stmt->items as $int_offset => $item) {
if ($item === null) {
continue;
@@ -1874,7 +1876,15 @@ public static function getSimpleType(
} else {
$can_create_objectlike = false;
}

if ($item->key
&& (!$item->key instanceof PhpParser\Node\Scalar\LNumber
|| $item->key->value !== $int_offset)
) {
$is_list = false;
}
} else {
$is_list = false;
$dim_type = $single_item_key_type;

if (!$dim_type) {
@@ -1924,6 +1934,7 @@ public static function getSimpleType(
) {
$objectlike = new Type\Atomic\ObjectLike($property_types, $class_strings);
$objectlike->sealed = true;
$objectlike->is_list = $is_list;
return new Type\Union([$objectlike]);
}

@@ -459,6 +459,24 @@ class Foo {
public const BAR = "bar";
}'
],
'resolveConstArrayAsList' => [
'<?php
class Test {
public const VALUES = [
"all",
"own"
];
}
/**
* @psalm-param list<"all"|"own"|"mine"> $value
*/
function test($value): void {
print_r($value);
}
test(Test::VALUES);'
],
];
}

0 comments on commit 1ccabab

Please sign in to comment.
You can’t perform that action at this time.