Skip to content

Commit

Permalink
Fix #1628 - improve behaviour of unset in loops
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed May 13, 2019
1 parent f8b4b12 commit fe0f352
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/Psalm/Internal/Type/TypeCombination.php
Expand Up @@ -272,6 +272,7 @@ public static function combineTypes(
if (!$combination->has_mixed || $combination->mixed_from_loop_isset) {
if ($combination->array_type_params
&& $combination->array_type_params[0]->allStringLiterals()
&& $combination->array_always_filled
) {
foreach ($combination->array_type_params[0]->getTypes() as $atomic_key_type) {
if ($atomic_key_type instanceof TLiteralString) {
Expand All @@ -281,6 +282,7 @@ public static function combineTypes(
}

$combination->array_type_params = [];
$combination->objectlike_sealed = false;
}

if (!$combination->array_type_params
Expand Down
8 changes: 4 additions & 4 deletions tests/ArrayAssignmentTest.php
Expand Up @@ -553,8 +553,8 @@ function fooFoo($a) {
$e[0][$int] = 3;
$e[0][$string] = 5;',
'assertions' => [
'$b' => 'array{0:array{0:int, c:int}}',
'$c' => 'array{0:array{0:int, c:int}}',
'$b' => 'array{0:array<string|int, int>}',
'$c' => 'array{0:array<int|string, int>}',
'$d' => 'array{0:array<int|string, int>}',
'$e' => 'array{0:array<string|int, int>}',
],
Expand Down Expand Up @@ -597,8 +597,8 @@ function fooFoo($a) {
$e["root"][$int] = 3;
$e["root"][$string] = 5;',
'assertions' => [
'$b' => 'array{root:array{0:int, c:int}}',
'$c' => 'array{root:array{0:int, c:int}}',
'$b' => 'array{root:array<string|int, int>}',
'$c' => 'array{root:array<int|string, int>}',
'$d' => 'array{root:array<int|string, int>}',
'$e' => 'array{root:array<string|int, int>}',
],
Expand Down
17 changes: 16 additions & 1 deletion tests/RedundantConditionTest.php
Expand Up @@ -572,7 +572,7 @@ function foo(?string $s) : string {
}
}'
],
'possiblyUpdateArrayAfterUnset' => [
'updateArrayAfterUnset' => [
'<?php
/**
* @param string[] $arr
Expand All @@ -583,6 +583,21 @@ function foo(string $s) : void {
if (count($dict)) {}
}'
],
'updateArrayAfterUnsetInLoop' => [
'<?php
/**
* @param string[] $arr
*/
function foo(array $arr) : void {
$dict = ["a" => 1, "b" => 2, "c" => 3];
foreach ($arr as $v) {
unset($dict[$v]);
}
if (count($dict)) {}
}'
],
];
}

Expand Down
7 changes: 7 additions & 0 deletions tests/TypeCombinationTest.php
Expand Up @@ -421,6 +421,13 @@ public function providerTestValidTypeCombination()
'Traversable<mixed, mixed>&Iterator',
],
],
'objectLikePlusArrayEqualsArray' => [
'array<string, int>',
[
'array<"a"|"b"|"c", 1|2|3>',
'array{a:1|2, b:2|3, c:1|3}',
],
],
];
}

Expand Down

0 comments on commit fe0f352

Please sign in to comment.