1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 13:51:54 +01:00

Merge pull request #8051 from AndrolGenhald/bugfix/8048

Fix possibly empty array shape appearing non-empty (fixes #8048).
This commit is contained in:
orklah 2022-06-08 19:22:12 +02:00 committed by GitHub
commit 02d5beecb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 2 deletions

View File

@ -214,6 +214,10 @@ class ArrayMergeReturnTypeProvider implements FunctionReturnTypeProviderInterfac
$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);
}

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',
],
];
}
}