Skip to content

Commit f652cf0

Browse files
committed
Merge branch 'fix_array_key_exists_negation' into fix_literal_union_key
2 parents 1a46565 + ef16989 commit f652cf0

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

src/Psalm/Internal/Analyzer/Statements/Block/IfElse/IfAnalyzer.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,11 @@
3434
use function array_keys;
3535
use function array_merge;
3636
use function array_reduce;
37-
use function array_unique;
3837
use function count;
3938
use function in_array;
4039
use function preg_match;
4140
use function preg_quote;
4241
use function spl_object_id;
43-
use function strpos;
44-
use function substr;
4542

4643
/**
4744
* @internal
@@ -272,20 +269,6 @@ public static function analyze(
272269
array_keys($if_scope->negated_types),
273270
);
274271

275-
$extra_vars_to_update = [];
276-
277-
// if there's an object-like array in there, we also need to update the root array variable
278-
foreach ($vars_to_update as $var_id) {
279-
$bracked_pos = strpos($var_id, '[');
280-
if ($bracked_pos !== false) {
281-
$extra_vars_to_update[] = substr($var_id, 0, $bracked_pos);
282-
}
283-
}
284-
285-
if ($extra_vars_to_update) {
286-
$vars_to_update = array_unique(array_merge($extra_vars_to_update, $vars_to_update));
287-
}
288-
289272
$outer_context->update(
290273
$old_if_context,
291274
$if_context,

src/Psalm/Type/Reconciler.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Psalm\Issue\TypeDoesNotContainType;
2121
use Psalm\IssueBuffer;
2222
use Psalm\Storage\Assertion;
23+
use Psalm\Storage\Assertion\ArrayKeyDoesNotExist;
2324
use Psalm\Storage\Assertion\ArrayKeyExists;
2425
use Psalm\Storage\Assertion\Empty_;
2526
use Psalm\Storage\Assertion\Falsy;
@@ -197,7 +198,9 @@ public static function reconcileKeyedTypes(
197198
$is_equality = $is_equality
198199
&& $new_type_part_part instanceof IsIdentical;
199200

200-
$has_inverted_isset = $has_inverted_isset || $new_type_part_part instanceof IsNotIsset;
201+
$has_inverted_isset = $has_inverted_isset
202+
|| $new_type_part_part instanceof IsNotIsset
203+
|| $new_type_part_part instanceof ArrayKeyDoesNotExist;
201204

202205
$has_count_check = $has_count_check
203206
|| $new_type_part_part instanceof NonEmptyCountable;

tests/TypeReconciliation/ArrayKeyExistsTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,28 @@ function three(array $a): void {
4646
echo $a["a"];
4747
echo $a["b"];
4848
}',
49+
],
50+
'arrayKeyExistsNegation' => [
51+
'code' => '<?php
52+
function getMethodName(array $data = []): void {
53+
if (\array_key_exists("custom_name", $data) && $data["custom_name"] !== null) {
54+
}
55+
/** @psalm-check-type-exact $data = array<array-key, mixed> */
56+
}
57+
',
58+
],
59+
'arrayKeyExistsNoSideEffects' => [
60+
'code' => '<?php
61+
function getMethodName(array $ddata = []): void {
62+
if (\array_key_exists("redirect", $ddata)) {
63+
return;
64+
}
65+
if (random_int(0, 1)) {
66+
$ddata["type"] = "test";
67+
}
68+
/** @psalm-check-type-exact $ddata = array<array-key, mixed> */
69+
}
70+
',
4971
],
5072
'arrayKeyExistsTwice' => [
5173
'code' => '<?php

0 commit comments

Comments
 (0)