diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ArrayFetchAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ArrayFetchAnalyzer.php index 4a3a1f5c9..753e28919 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ArrayFetchAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/ArrayFetchAnalyzer.php @@ -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); diff --git a/tests/ArrayAccessTest.php b/tests/ArrayAccessTest.php index 8bc2a488f..91ef428f9 100644 --- a/tests/ArrayAccessTest.php +++ b/tests/ArrayAccessTest.php @@ -654,6 +654,19 @@ class ArrayAccessTest extends TestCase '$x3===' => "array{b: 'value'}", ], ], + 'possiblyUndefinedArrayOffsetKeyedArray' => [ + 'code' => ' [ + '$x===' => '"a"|null', + ], + 'ignored_issues' => ['PossiblyUndefinedArrayOffset'], + ], 'domNodeListAccessible' => [ 'code' => '