1
0
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:
orklah 2021-10-23 12:11:39 +02:00 committed by GitHub
commit 78a4c78187
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 8 deletions

View File

@ -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

View File

@ -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"];
}'
],
];
}