1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Prevent more array<mixed,...> creation

This commit is contained in:
Matthew Brown 2020-02-22 12:12:40 -05:00
parent 952045e2ba
commit 6ad3d039df
4 changed files with 48 additions and 3 deletions

View File

@ -530,11 +530,15 @@ class ArrayAssignmentAnalyzer
} elseif (!$root_is_string) {
if ($current_dim) {
if ($current_dim_type = $statements_analyzer->node_data->getType($current_dim)) {
if ($current_dim_type->isMixed()) {
$current_dim_type = Type::getArrayKey();
}
$array_atomic_key_type = ArrayFetchAnalyzer::replaceOffsetTypeWithInts(
$current_dim_type
);
} else {
$array_atomic_key_type = Type::getMixed();
$array_atomic_key_type = Type::getArrayKey();
}
if ($offset_already_existed

View File

@ -536,7 +536,9 @@ class ArrayFetchAnalyzer
// if we're assigning to an empty array with a key offset, refashion that array
if ($in_assignment) {
if ($type->type_params[0]->isEmpty()) {
$type->type_params[0] = $offset_type;
$type->type_params[0] = $offset_type->isMixed()
? Type::getArrayKey()
: $offset_type;
}
} elseif (!$type->type_params[0]->isEmpty()) {
$expected_offset_type = $type->type_params[0]->hasMixed()
@ -954,7 +956,7 @@ class ArrayFetchAnalyzer
$new_key_type = Type::combineUnionTypes(
$generic_key_type,
$offset_type
$offset_type->isMixed() ? Type::getArrayKey() : $offset_type
);
$property_count = $type->sealed ? count($type->properties) : null;

View File

@ -1052,6 +1052,12 @@ class TypeAnalyzer
return true;
}
if ($container_type_part instanceof TArrayKey
&& $input_type_part instanceof TNumeric
) {
return true;
}
if ($container_type_part instanceof TArrayKey
&& ($input_type_part instanceof TInt
|| $input_type_part instanceof TString

View File

@ -1320,6 +1320,39 @@ class ArrayAssignmentTest extends TestCase
}
}',
],
'arrayMixedMixedNotAllowedFromObject' => [
'<?php
function foo(ArrayObject $a) : array {
$arr = [];
/**
* @psalm-suppress MixedAssignment
* @psalm-suppress MixedArrayOffset
*/
foreach ($a as $k => $v) {
$arr[$k] = $v;
}
return $arr;
}',
],
'arrayMixedMixedNotAllowedFromMixed' => [
'<?php
/** @psalm-suppress MissingParamType */
function foo($a) : array {
$arr = ["a" => "foo"];
/**
* @psalm-suppress MixedAssignment
* @psalm-suppress MixedArrayOffset
*/
foreach ($a as $k => $v) {
$arr[$k] = $v;
}
return $arr;
}',
],
];
}