1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

fix possibly undefined array key in keyed array doesnt include null when not validated

Fix https://psalm.dev/r/b153d0d248 to return 'a'|null instead of 'a' - this is required as otherwise empty would report RedundantCondition errors now which would bring back https://github.com/vimeo/psalm/issues/2681
This commit is contained in:
kkmuffme 2024-01-13 11:18:48 +01:00
parent 63ea734f26
commit 19b1a33a20
2 changed files with 19 additions and 2 deletions

View File

@ -313,14 +313,18 @@ final class ArrayFetchAnalyzer
&& !$context->inside_unset
&& ($stmt_var_type && !$stmt_var_type->hasMixed())
) {
IssueBuffer::maybeAdd(
if (IssueBuffer::accepts(
new PossiblyUndefinedArrayOffset(
'Possibly undefined array key ' . $keyed_array_var_id
. ' on ' . $stmt_var_type->getId(),
new CodeLocation($statements_analyzer->getSource(), $stmt),
),
$statements_analyzer->getSuppressedIssues(),
);
)) {
$stmt_type = $stmt_type->getBuilder()->addType(new TNull())->freeze();
}
} elseif ($stmt_type->possibly_undefined) {
$stmt_type = $stmt_type->getBuilder()->addType(new TNull())->freeze();
}
$stmt_type = $stmt_type->setPossiblyUndefined(false);

View File

@ -654,6 +654,19 @@ class ArrayAccessTest extends TestCase
'$x3===' => "array{b: 'value'}",
],
],
'possiblyUndefinedArrayOffsetKeyedArray' => [
'code' => '<?php
$d = [];
if (!rand(0,1)) {
$d[0] = "a";
}
$x = $d[0];',
'assertions' => [
'$x===' => '"a"|null',
],
'ignored_issues' => ['PossiblyUndefinedArrayOffset'],
],
'domNodeListAccessible' => [
'code' => '<?php
$doc = new DOMDocument();