mirror of
https://github.com/danog/psalm.git
synced 2024-11-27 04:45:20 +01:00
Merge pull request #6718 from orklah/5651
fix literal string access on a non empty array
This commit is contained in:
commit
78a4c78187
@ -1295,14 +1295,18 @@ class ArrayFetchAnalyzer
|
||||
if ($codebase->config->ensure_array_string_offsets_exist
|
||||
&& $offset_type_contained_by_expected
|
||||
) {
|
||||
self::checkLiteralStringArrayOffset(
|
||||
$offset_type,
|
||||
$expected_offset_type,
|
||||
$array_var_id,
|
||||
$stmt,
|
||||
$context,
|
||||
$statements_analyzer
|
||||
);
|
||||
//we already know we found a match, so if the array is non-empty and the key is a literal,
|
||||
//then no need to check for PossiblyUndefinedStringArrayOffset
|
||||
if (!$type instanceof TNonEmptyArray || !$type->type_params[0]->isSingleStringLiteral()) {
|
||||
self::checkLiteralStringArrayOffset(
|
||||
$offset_type,
|
||||
$expected_offset_type,
|
||||
$array_var_id,
|
||||
$stmt,
|
||||
$context,
|
||||
$statements_analyzer
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if ($codebase->config->ensure_array_int_offsets_exist
|
||||
|
@ -1017,6 +1017,13 @@ class ArrayAccessTest extends TestCase
|
||||
'$_arr2===' => 'non-empty-array<1, 5>',
|
||||
]
|
||||
],
|
||||
'accessArrayWithSingleStringLiteralOffset' => [
|
||||
'<?php
|
||||
/** @param non-empty-array<"name", int> $p */
|
||||
function f($p): int {
|
||||
return $p["name"];
|
||||
}'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user