Skip to content

Commit

Permalink
Merge pull request #7732 from orklah/simplelistinferer
Browse files Browse the repository at this point in the history
allow SimpleTypeInferer to infer non empty lists
  • Loading branch information
orklah committed Feb 24, 2022
2 parents 3a85f49 + 26bfc95 commit b09811c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
Expand Up @@ -26,6 +26,7 @@
use Psalm\Type\Atomic\TLiteralString;
use Psalm\Type\Atomic\TMixed;
use Psalm\Type\Atomic\TNonEmptyArray;
use Psalm\Type\Atomic\TNonEmptyList;
use Psalm\Type\Atomic\TNonEmptyString;
use Psalm\Type\Atomic\TString;
use Psalm\Type\Union;
Expand Down Expand Up @@ -525,6 +526,12 @@ private static function inferArrayType(
return null;
}

if ($array_creation_info->all_list) {
return new Union([
new TNonEmptyList($item_value_type),
]);
}

return new Union([
new TNonEmptyArray([
$item_key_type,
Expand Down
68 changes: 68 additions & 0 deletions tests/ListTest.php
Expand Up @@ -84,6 +84,74 @@ function takesList($a): void {}
$a = [1, 1 => 2, 3];
takesList($a);',
],
'simpleTypeInfererNonEmptyList' => [
'<?php
class Foo {
public const VARS = [
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"x",
"y",
];
}
/** @param list<string> $vars */
function foo(array $vars): void {
print_r($vars);
}
foo(Foo::VARS);
',
],
];
}

Expand Down

0 comments on commit b09811c

Please sign in to comment.