1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-03 10:07:52 +01:00
This commit is contained in:
Daniil Gentili 2022-11-26 14:57:23 +01:00
parent 415d10365f
commit 61cce1b854

View File

@ -1150,12 +1150,13 @@ class ArrayFetchAnalyzer
$offset_type = self::replaceOffsetTypeWithInts($offset_type->freeze())->getBuilder();
if ($type instanceof TList
if ($type instanceof TKeyedArray
&& $type->is_list
&& (($in_assignment && $stmt->dim)
|| $original_type instanceof TTemplateParam
|| !$offset_type->isInt())
) {
$temp = new TArray([Type::getInt(), $type->type_param]);
$temp = $type->getGenericArrayType();
self::handleArrayAccessOnTArray(
$statements_analyzer,
$codebase,
@ -1187,22 +1188,6 @@ class ArrayFetchAnalyzer
$original_type,
$has_valid_offset
);
} elseif ($type instanceof TList) {
self::handleArrayAccessOnList(
$statements_analyzer,
$codebase,
$stmt,
$type,
$offset_type,
$extended_var_id,
$key_values,
$context,
$in_assignment,
$expected_offset_types,
$replacement_type,
$array_access_type,
$has_valid_offset
);
} elseif ($type instanceof TClassStringMap) {
self::handleArrayAccessOnClassStringMap(
$codebase,
@ -1693,77 +1678,6 @@ class ArrayFetchAnalyzer
}
}
/**
* @param list<string> $expected_offset_types
* @param list<TLiteralString|TLiteralInt> $key_values
* @param-out TList $type
*/
private static function handleArrayAccessOnList(
StatementsAnalyzer $statements_analyzer,
Codebase $codebase,
PhpParser\Node\Expr\ArrayDimFetch $stmt,
TList &$type,
MutableUnion $offset_type,
?string $extended_var_id,
array $key_values,
Context $context,
bool $in_assignment,
array &$expected_offset_types,
?Union $replacement_type,
?Union &$array_access_type,
bool &$has_valid_offset
): void {
// if we're assigning to an empty array with a key offset, refashion that array
if (!$in_assignment) {
if (!$type instanceof TNonEmptyList
|| (count($key_values) === 1
&& $key_values[0] instanceof TLiteralInt
&& $key_values[0]->value > 0
&& $key_values[0]->value > ($type->count - 1)
&& $key_values[0]->value > ($type->min_count - 1))
) {
$expected_offset_type = Type::getInt();
if ($codebase->config->ensure_array_int_offsets_exist) {
self::checkLiteralIntArrayOffset(
$offset_type,
$expected_offset_type,
$extended_var_id,
$stmt,
$context,
$statements_analyzer
);
}
$has_valid_offset = true;
} elseif (count($key_values) === 1
&& $key_values[0] instanceof TLiteralInt
&& $key_values[0]->value < 0
) {
$expected_offset_types[] = 'positive-int';
$has_valid_offset = false;
} else {
$has_valid_offset = true;
}
}
if ($in_assignment && $type instanceof TNonEmptyList && $type->count !== null) {
$type = $type->setCount($type->count+1);
}
if ($in_assignment && $replacement_type) {
$type = $type->setTypeParam(Type::combineUnionTypes(
$type->type_param,
$replacement_type,
$codebase
));
}
$array_access_type = Type::combineUnionTypes(
$array_access_type,
$type->type_param
);
}
private static function handleArrayAccessOnNamedObject(
StatementsAnalyzer $statements_analyzer,
PhpParser\Node\Expr\ArrayDimFetch $stmt,