Skip to content

Commit

Permalink
Merge pull request #8051 from AndrolGenhald/bugfix/8048
Browse files Browse the repository at this point in the history
Fix possibly empty array shape appearing non-empty (fixes #8048).
  • Loading branch information
orklah committed Jun 8, 2022
2 parents b113d77 + 34322b7 commit 02d5bee
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
$inner_value_type = null;

if ($inner_key_types) {
/**
* Truthy&array-shape-list doesn't reconcile correctly, will be fixed for 5.x by #8050.
* @psalm-suppress InvalidScalarArgument
*/
$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',
],
];
}
}

0 comments on commit 02d5bee

Please sign in to comment.