1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Improve check for empty array

This commit is contained in:
Brown 2020-07-15 09:49:30 -04:00
parent 0ab163fd78
commit 06ee1b71c7
3 changed files with 20 additions and 8 deletions

View File

@ -132,10 +132,11 @@ function array_flip(array $arr)
/**
* @psalm-template TKey as array-key
* @psalm-template TArray as array<TKey, mixed>
*
* @param array<TKey, mixed> $arr
* @param TArray $arr
*
* @return (TKey is empty ? null : ($arr is non-empty-array ? TKey : TKey|null))
* @return (TArray is array<empty, empty> ? null : (TArray is non-empty-array ? TKey : TKey|null))
* @psalm-pure
*/
function key($arr)
@ -144,10 +145,11 @@ function key($arr)
/**
* @psalm-template TKey as array-key
* @psalm-template TArray as array<TKey, mixed>
*
* @param array<TKey, mixed> $arr
* @param TArray $arr
*
* @return (TKey is empty ? null : ($arr is non-empty-array ? TKey : TKey|null))
* @return (TArray is array<empty, empty> ? null : (TArray is non-empty-array ? TKey : TKey|null))
* @psalm-pure
*/
function array_key_first($arr)
@ -156,10 +158,11 @@ function array_key_first($arr)
/**
* @psalm-template TKey as array-key
* @psalm-template TArray as array<TKey, mixed>
*
* @param array<TKey, mixed> $arr
* @param TArray $arr
*
* @return (TKey is empty ? null : ($arr is non-empty-array ? TKey : TKey|null))
* @return (TArray is array<empty, empty> ? null : (TArray is non-empty-array ? TKey : TKey|null))
* @psalm-pure
*/
function array_key_last($arr)

View File

@ -770,6 +770,16 @@ class ArrayFunctionCallTest extends TestCase
'$b' => 'string',
],
],
'keyNonEmptyArray' => [
'<?php
/**
* @param non-empty-array $arr
* @return array-key
*/
function foo(array $arr) {
return key($arr);
}',
],
'arrayKeyFirst' => [
'<?php
/** @return array<string, int> */

View File

@ -244,7 +244,7 @@ class EmptyTest extends \Psalm\Tests\TestCase
}
}',
],
'SKIPPED-unsetChangesComplicatedArrayEmptiness' => [
'unsetChangesComplicatedArrayEmptiness' => [
'<?php
function contains(array $data, array $needle): bool {
if (empty($data) || empty($needle)) {
@ -253,7 +253,6 @@ class EmptyTest extends \Psalm\Tests\TestCase
$stack = [];
while (!empty($needle)) {
/** @psalm-trace $key */
$key = key($needle);
$val = $needle[$key];
unset($needle[$key]);