1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 12:24:49 +01:00

Unset should change array with keyed offsets

This commit is contained in:
Matt Brown 2021-01-13 18:10:59 -05:00 committed by Daniil Gentili
parent bd8df0e820
commit 27a5323277
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
3 changed files with 13 additions and 3 deletions

View File

@ -81,8 +81,10 @@ class UnsetAnalyzer
$atomic_root_type->sealed = false;
$root_type->addType(
$atomic_root_type->getGenericArrayType()
$atomic_root_type->getGenericArrayType(false)
);
$atomic_root_type->is_list = false;
}
} elseif ($atomic_root_type instanceof Type\Atomic\TNonEmptyArray) {
$root_type->addType(

View File

@ -246,7 +246,7 @@ class TKeyedArray extends \Psalm\Type\Atomic
return $value_type;
}
public function getGenericArrayType(): TArray
public function getGenericArrayType(bool $allow_non_empty = true): TArray
{
$key_types = [];
$value_type = null;
@ -285,7 +285,7 @@ class TKeyedArray extends \Psalm\Type\Atomic
$value_type->possibly_undefined = false;
if ($this->previous_value_type || $has_defined_keys) {
if ($allow_non_empty && ($this->previous_value_type || $has_defined_keys)) {
$array_type = new TNonEmptyArray([$key_type, $value_type]);
} else {
$array_type = new TArray([$key_type, $value_type]);

View File

@ -797,6 +797,14 @@ class RedundantConditionTest extends \Psalm\Tests\TestCase
return (int) (hexdec($x) + 1);
}',
],
'unsetArrayWithKnownOffset' => [
'<?php
function bar(string $f) : void {
$filter = rand(0, 1) ? explode(",", $f) : [$f];
unset($filter[rand(0, 1)]);
if ($filter) {}
}'
],
];
}