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

Fix possibly empty array shape appearing non-empty (fixes #8048). #8051

Merged
merged 2 commits into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,15 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
$inner_value_type = null;

if ($inner_key_types) {
/**
AndrolGenhald marked this conversation as resolved.
Show resolved Hide resolved
* @psalm-suppress InvalidScalarArgument
* $inner_key_types is incorrectly inferred to be array{
* 0?: Psalm\Type\Atomic|Psalm\Type\Atomic\TInt,
* 1?: Psalm\Type\Atomic|Psalm\Type\Atomic\TInt,
* ...
* 11?: Psalm\Type\Atomic|Psalm\Type\Atomic\TInt,
* }
*/
$inner_key_type = TypeCombiner::combine($inner_key_types, $codebase, true);
}

Expand Down
6 changes: 4 additions & 2 deletions src/Psalm/Type/Atomic/TKeyedArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,13 +408,15 @@ public function getAssertionString(bool $exact = false): string
return $this->getKey();
}

public function getList(): TNonEmptyList
public function getList(): TList
{
if (!$this->is_list) {
throw new UnexpectedValueException('Object-like array must be a list for conversion');
}

return new TNonEmptyList($this->getGenericValueType());
return $this->isNonEmpty()
? new TNonEmptyList($this->getGenericValueType())
: new TList($this->getGenericValueType());
}

/**
Expand Down
11 changes: 11 additions & 0 deletions tests/TypeReconciliation/TypeAlgebraTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1420,6 +1420,17 @@ function foo(?X $x): void {
false,
'8.1',
],
'arrayShapeListCanBeEmpty' => [
'<?php
/** @param non-empty-list<mixed> $_list */
function foobar(array $_list): void {}

$list = random_int(0, 1) ? [] : ["foobar"];

foobar($list);
',
'error_message' => 'InvalidArgument',
],
];
}
}