Skip to content

Commit

Permalink
Unset should change array with keyed offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Jan 13, 2021
1 parent d10a068 commit ef4afd7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/Psalm/Internal/Analyzer/Statements/UnsetAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ public static function analyze(
$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(
Expand Down
4 changes: 2 additions & 2 deletions src/Psalm/Type/Atomic/TKeyedArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public function getGenericValueType(): Union
return $value_type;
}

public function getGenericArrayType(): TArray
public function getGenericArrayType(bool $allow_non_empty = true): TArray
{
$key_types = [];
$value_type = null;
Expand Down Expand Up @@ -285,7 +285,7 @@ public function getGenericArrayType(): TArray

$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]);
Expand Down
8 changes: 8 additions & 0 deletions tests/TypeReconciliation/RedundantConditionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,14 @@ function x(string $x): int {
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) {}
}'
],
];
}

Expand Down

0 comments on commit ef4afd7

Please sign in to comment.