Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow SimpleTypeInferer to infer non empty lists #7732

Merged
merged 1 commit into from Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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