1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

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

This commit is contained in:
AndrolGenhald 2022-06-02 13:59:00 -05:00
parent b5a0c5926b
commit c271b1245e
3 changed files with 24 additions and 2 deletions

View File

@ -214,6 +214,15 @@ class ArrayMergeReturnTypeProvider implements FunctionReturnTypeProviderInterfac
$inner_value_type = null;
if ($inner_key_types) {
/**
* @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);
}

View File

@ -408,13 +408,15 @@ class TKeyedArray extends Atomic
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());
}
/**

View File

@ -1420,6 +1420,17 @@ class TypeAlgebraTest extends TestCase
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',
],
];
}
}