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:
parent
bd8df0e820
commit
27a5323277
@ -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(
|
||||
|
@ -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]);
|
||||
|
@ -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) {}
|
||||
}'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user