1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Fix #2223 - make sure lists are handled in more places

This commit is contained in:
Matthew Brown 2019-10-10 20:16:43 -04:00
parent 3658771bff
commit 03c39cbe7c
7 changed files with 39 additions and 14 deletions

View File

@ -120,6 +120,12 @@ class FunctionAnalyzer extends FunctionLikeAnalyzer
? new Type\Atomic\TLiteralInt($atomic_types['array']->count) ? new Type\Atomic\TLiteralInt($atomic_types['array']->count)
: new Type\Atomic\TInt : new Type\Atomic\TInt
]); ]);
} elseif ($atomic_types['array'] instanceof Type\Atomic\TNonEmptyList) {
return new Type\Union([
$atomic_types['array']->count !== null
? new Type\Atomic\TLiteralInt($atomic_types['array']->count)
: new Type\Atomic\TInt
]);
} elseif ($atomic_types['array'] instanceof Type\Atomic\ObjectLike } elseif ($atomic_types['array'] instanceof Type\Atomic\ObjectLike
&& $atomic_types['array']->sealed && $atomic_types['array']->sealed
) { ) {
@ -307,6 +313,10 @@ class FunctionAnalyzer extends FunctionLikeAnalyzer
if ($array_type instanceof Type\Atomic\TArray) { if ($array_type instanceof Type\Atomic\TArray) {
return clone $array_type->type_params[1]; return clone $array_type->type_params[1];
} }
if ($array_type instanceof Type\Atomic\TList) {
return clone $array_type->type_param;
}
} elseif ($first_arg->inferredType->hasScalarType() && } elseif ($first_arg->inferredType->hasScalarType() &&
isset($call_args[1]) && isset($call_args[1]) &&
($second_arg = $call_args[1]->value) && ($second_arg = $call_args[1]->value) &&

View File

@ -518,7 +518,8 @@ class ArrayAssignmentAnalyzer
$atomic_root_types = $new_child_type->getTypes(); $atomic_root_types = $new_child_type->getTypes();
if (isset($atomic_root_types['array']) if (isset($atomic_root_types['array'])
&& $atomic_root_types['array'] instanceof TNonEmptyArray && ($atomic_root_types['array'] instanceof TNonEmptyArray
|| $atomic_root_types['array'] instanceof TNonEmptyList)
&& $atomic_root_types['array']->count !== null && $atomic_root_types['array']->count !== null
) { ) {
$atomic_root_types['array']->count++; $atomic_root_types['array']->count++;

View File

@ -816,7 +816,7 @@ class CallAnalyzer
if (isset($array_arg->inferredType) && $array_arg->inferredType->hasArray()) { if (isset($array_arg->inferredType) && $array_arg->inferredType->hasArray()) {
/** /**
* @psalm-suppress PossiblyUndefinedArrayOffset * @psalm-suppress PossiblyUndefinedArrayOffset
* @var TArray|ObjectLike * @var TArray|ObjectLike|TList
*/ */
$array_type = $array_arg->inferredType->getTypes()['array']; $array_type = $array_arg->inferredType->getTypes()['array'];
@ -961,7 +961,7 @@ class CallAnalyzer
) { ) {
/** /**
* @psalm-suppress PossiblyUndefinedArrayOffset * @psalm-suppress PossiblyUndefinedArrayOffset
* @var TArray|ObjectLike * @var TArray|ObjectLike|TList
*/ */
$array_type = $array_arg->inferredType->getTypes()['array']; $array_type = $array_arg->inferredType->getTypes()['array'];
@ -971,7 +971,7 @@ class CallAnalyzer
/** /**
* @psalm-suppress PossiblyUndefinedArrayOffset * @psalm-suppress PossiblyUndefinedArrayOffset
* @var TArray|ObjectLike * @var TArray|ObjectLike|TList
*/ */
$replacement_array_type = $replacement_arg->inferredType->getTypes()['array']; $replacement_array_type = $replacement_arg->inferredType->getTypes()['array'];
@ -1435,11 +1435,11 @@ class CallAnalyzer
) { ) {
/** /**
* @psalm-suppress PossiblyUndefinedArrayOffset * @psalm-suppress PossiblyUndefinedArrayOffset
* @var TArray * @var TList
*/ */
$array_type = $param_type->getTypes()['array']; $array_type = $param_type->getTypes()['array'];
$param_type = $array_type->type_params[1]; $param_type = $array_type->type_param;
} }
if ($param_type && !$param_type->hasMixed()) { if ($param_type && !$param_type->hasMixed()) {

View File

@ -183,12 +183,14 @@ class ArrayFetchAnalyzer
) { ) {
/** /**
* @psalm-suppress PossiblyUndefinedArrayOffset * @psalm-suppress PossiblyUndefinedArrayOffset
* @var TArray|ObjectLike * @var TArray|ObjectLike|TList
*/ */
$array_type = $stmt->var->inferredType->getTypes()['array']; $array_type = $stmt->var->inferredType->getTypes()['array'];
if ($array_type instanceof TArray) { if ($array_type instanceof TArray) {
$const_array_key_type = $array_type->type_params[0]; $const_array_key_type = $array_type->type_params[0];
} elseif ($array_type instanceof TList) {
$const_array_key_type = $array_type->type_param;
} else { } else {
$const_array_key_type = $array_type->getGenericKeyType(); $const_array_key_type = $array_type->getGenericKeyType();
} }

View File

@ -135,15 +135,17 @@ class CallMap
if ($arg_type->hasArray()) { if ($arg_type->hasArray()) {
/** /**
* @psalm-suppress PossiblyUndefinedArrayOffset * @psalm-suppress PossiblyUndefinedArrayOffset
* @var Type\Atomic\TArray|Type\Atomic\ObjectLike * @var Type\Atomic\TArray|Type\Atomic\ObjectLike|Type\Atomic\TList
*/ */
$array_atomic_type = $arg_type->getTypes()['array']; $array_atomic_type = $arg_type->getTypes()['array'];
if ($array_atomic_type instanceof Type\Atomic\ObjectLike) { if ($array_atomic_type instanceof Type\Atomic\ObjectLike) {
$array_atomic_type = $array_atomic_type->getGenericArrayType(); $arg_type = $array_atomic_type->getGenericValueType();
} elseif ($array_atomic_type instanceof Type\Atomic\TList) {
$arg_type = $array_atomic_type->type_param;
} else {
$arg_type = $array_atomic_type->type_params[1];
} }
$arg_type = $array_atomic_type->type_params[1];
} }
} }

View File

@ -203,7 +203,7 @@ class PhpStormMetaScanner
if ($call_arg_type->hasArray()) { if ($call_arg_type->hasArray()) {
/** /**
* @psalm-suppress PossiblyUndefinedArrayOffset * @psalm-suppress PossiblyUndefinedArrayOffset
* @var Type\Atomic\TArray|Type\Atomic\ObjectLike * @var Type\Atomic\TArray|Type\Atomic\ObjectLike|Type\Atomic\TList
*/ */
$array_atomic_type = $call_arg_type->getTypes()['array']; $array_atomic_type = $call_arg_type->getTypes()['array'];
@ -211,6 +211,10 @@ class PhpStormMetaScanner
return $array_atomic_type->getGenericValueType(); return $array_atomic_type->getGenericValueType();
} }
if ($array_atomic_type instanceof Type\Atomic\TList) {
return $array_atomic_type->type_param;
}
return clone $array_atomic_type->type_params[1]; return clone $array_atomic_type->type_params[1];
} }
} }
@ -334,7 +338,7 @@ class PhpStormMetaScanner
if ($call_arg_type->hasArray()) { if ($call_arg_type->hasArray()) {
/** /**
* @psalm-suppress PossiblyUndefinedArrayOffset * @psalm-suppress PossiblyUndefinedArrayOffset
* @var Type\Atomic\TArray|Type\Atomic\ObjectLike * @var Type\Atomic\TArray|Type\Atomic\ObjectLike|Type\Atomic\TList
*/ */
$array_atomic_type = $call_arg_type->getTypes()['array']; $array_atomic_type = $call_arg_type->getTypes()['array'];
@ -342,6 +346,10 @@ class PhpStormMetaScanner
return $array_atomic_type->getGenericValueType(); return $array_atomic_type->getGenericValueType();
} }
if ($array_atomic_type instanceof Type\Atomic\TList) {
return $array_atomic_type->type_param;
}
return clone $array_atomic_type->type_params[1]; return clone $array_atomic_type->type_params[1];
} }
} }

View File

@ -2853,12 +2853,14 @@ class ReflectorVisitor extends PhpParser\NodeVisitorAbstract implements PhpParse
if (!$docblock_param_variadic && $storage_param->is_variadic && $new_param_type->hasArray()) { if (!$docblock_param_variadic && $storage_param->is_variadic && $new_param_type->hasArray()) {
/** /**
* @psalm-suppress PossiblyUndefinedArrayOffset * @psalm-suppress PossiblyUndefinedArrayOffset
* @var Type\Atomic\TArray|Type\Atomic\ObjectLike * @var Type\Atomic\TArray|Type\Atomic\ObjectLike|Type\Atomic\TList
*/ */
$array_type = $new_param_type->getTypes()['array']; $array_type = $new_param_type->getTypes()['array'];
if ($array_type instanceof Type\Atomic\ObjectLike) { if ($array_type instanceof Type\Atomic\ObjectLike) {
$new_param_type = $array_type->getGenericValueType(); $new_param_type = $array_type->getGenericValueType();
} elseif ($array_type instanceof Type\Atomic\TList) {
$new_param_type = $array_type->type_param;
} else { } else {
$new_param_type = $array_type->type_params[1]; $new_param_type = $array_type->type_params[1];
} }