mirror of
https://github.com/danog/psalm.git
synced 2024-11-30 04:39:00 +01:00
Fix array_reverse implementation
This commit is contained in:
parent
0da3623372
commit
f0ddc6f3bc
@ -26,7 +26,6 @@ class ArrayReverseReturnTypeProvider implements \Psalm\Plugin\Hook\FunctionRetur
|
||||
CodeLocation $code_location
|
||||
) : Type\Union {
|
||||
$first_arg = isset($call_args[0]->value) ? $call_args[0]->value : null;
|
||||
$preserve_keys_arg = isset($call_args[1]->value) ? $call_args[1]->value : null;
|
||||
|
||||
$first_arg_array = $first_arg
|
||||
&& isset($first_arg->inferredType)
|
||||
@ -41,42 +40,10 @@ class ArrayReverseReturnTypeProvider implements \Psalm\Plugin\Hook\FunctionRetur
|
||||
return Type::getArray();
|
||||
}
|
||||
|
||||
if ($preserve_keys_arg instanceof PhpParser\Node\Expr\ConstFetch
|
||||
&& strtolower($preserve_keys_arg->name->parts[0]) === 'true'
|
||||
) {
|
||||
if ($first_arg_array instanceof Type\Atomic\TArray) {
|
||||
return new Type\Union([clone $first_arg_array]);
|
||||
}
|
||||
|
||||
return new Type\Union([$first_arg_array->getGenericArrayType()]);
|
||||
}
|
||||
|
||||
$key_type = Type::getArrayKey();
|
||||
|
||||
if (!$preserve_keys_arg
|
||||
|| ($preserve_keys_arg instanceof PhpParser\Node\Expr\ConstFetch
|
||||
&& strtolower($preserve_keys_arg->name->parts[0]) === 'false')
|
||||
) {
|
||||
$key_type = Type::getInt();
|
||||
}
|
||||
|
||||
if ($first_arg_array instanceof Type\Atomic\TArray) {
|
||||
$value_type = clone $first_arg_array->type_params[1];
|
||||
} else {
|
||||
$value_type = $first_arg_array->getGenericValueType();
|
||||
return new Type\Union([clone $first_arg_array]);
|
||||
}
|
||||
|
||||
$array_atomic_type = $first_arg_array instanceof Type\Atomic\TNonEmptyArray
|
||||
|| ($first_arg_array instanceof Type\Atomic\ObjectLike && $first_arg_array->sealed)
|
||||
? new Type\Atomic\TNonEmptyArray([
|
||||
$key_type,
|
||||
$value_type,
|
||||
])
|
||||
: new Type\Atomic\TArray([
|
||||
$key_type,
|
||||
$value_type,
|
||||
]);
|
||||
|
||||
return new Type\Union([$array_atomic_type]);
|
||||
return new Type\Union([$first_arg_array->getGenericArrayType()]);
|
||||
}
|
||||
}
|
||||
|
@ -209,14 +209,14 @@ class FunctionCallTest extends TestCase
|
||||
'<?php
|
||||
$d = array_reverse(["a", "b", 1, "d" => 4]);',
|
||||
'assertions' => [
|
||||
'$d' => 'non-empty-array<int, string|int>',
|
||||
'$d' => 'non-empty-array<string|int, string|int>',
|
||||
],
|
||||
],
|
||||
'arrayReverseDontPreserveKeyExplicitArg' => [
|
||||
'<?php
|
||||
$d = array_reverse(["a", "b", 1, "d" => 4], false);',
|
||||
'assertions' => [
|
||||
'$d' => 'non-empty-array<int, string|int>',
|
||||
'$d' => 'non-empty-array<string|int, string|int>',
|
||||
],
|
||||
],
|
||||
'arrayReversePreserveKey' => [
|
||||
|
Loading…
Reference in New Issue
Block a user