Skip to content

Commit

Permalink
Merge pull request #6170 from Jack97/array-slice-template-params
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan authored Jul 24, 2021
2 parents 3caceb7 + 1c0de36 commit ffbdaa8
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,50 +18,69 @@ public static function getFunctionReturnType(FunctionReturnTypeProviderEvent $ev
{
$statements_source = $event->getStatementsSource();
$call_args = $event->getCallArgs();

if (!$statements_source instanceof \Psalm\Internal\Analyzer\StatementsAnalyzer) {
return Type::getMixed();
}

$first_arg = isset($call_args[0]->value) ? $call_args[0]->value : null;

$first_arg_array = $first_arg
&& ($first_arg_type = $statements_source->node_data->getType($first_arg))
&& $first_arg_type->hasType('array')
&& ($array_atomic_type = $first_arg_type->getAtomicTypes()['array'])
&& ($array_atomic_type instanceof Type\Atomic\TArray
|| $array_atomic_type instanceof Type\Atomic\TKeyedArray
|| $array_atomic_type instanceof Type\Atomic\TList)
? $array_atomic_type
: null;

if (!$first_arg_array) {
if (!$first_arg) {
return Type::getArray();
}

$dont_preserve_int_keys = !isset($call_args[3]->value)
|| (($third_arg_type = $statements_source->node_data->getType($call_args[3]->value))
&& ((string) $third_arg_type === 'false'));

$already_cloned = false;
$first_arg_type = $statements_source->node_data->getType($first_arg);

if ($first_arg_array instanceof Type\Atomic\TKeyedArray) {
$already_cloned = true;
$first_arg_array = $first_arg_array->getGenericArrayType();
if (!$first_arg_type) {
return Type::getArray();
}

if ($first_arg_array instanceof Type\Atomic\TArray) {
if (!$already_cloned) {
$first_arg_array = clone $first_arg_array;
$atomic_types = $first_arg_type->getAtomicTypes();

$return_atomic_type = null;

while ($atomic_type = \array_shift($atomic_types)) {
if ($atomic_type instanceof Type\Atomic\TTemplateParam) {
$atomic_types = \array_merge($atomic_types, $atomic_type->as->getAtomicTypes());
continue;
}

$already_cloned = false;

if ($atomic_type instanceof Type\Atomic\TKeyedArray) {
$already_cloned = true;
$atomic_type = $atomic_type->getGenericArrayType();
}

if ($atomic_type instanceof Type\Atomic\TArray) {
if (!$already_cloned) {
$atomic_type = clone $atomic_type;
}

$return_atomic_type = new Type\Atomic\TArray($atomic_type->type_params);
continue;
}

if ($atomic_type instanceof Type\Atomic\TList) {
$return_atomic_type = new Type\Atomic\TArray([Type::getInt(), clone $atomic_type->type_param]);
continue;
}
$array_type = new Type\Atomic\TArray($first_arg_array->type_params);
} else {
$array_type = new Type\Atomic\TArray([Type::getInt(), clone $first_arg_array->type_param]);

return Type::getArray();
}

if ($dont_preserve_int_keys && $array_type->type_params[0]->isInt()) {
$array_type = new Type\Atomic\TList($array_type->type_params[1]);
if (!$return_atomic_type) {
throw new \UnexpectedValueException('This should never happen');
}

$dont_preserve_int_keys = !isset($call_args[3]->value)
|| (($third_arg_type = $statements_source->node_data->getType($call_args[3]->value))
&& ((string) $third_arg_type === 'false'));

if ($dont_preserve_int_keys && $return_atomic_type->type_params[0]->isInt()) {
$return_atomic_type = new Type\Atomic\TList($return_atomic_type->type_params[1]);
}

return new Type\Union([$array_type]);
return new Type\Union([$return_atomic_type]);
}
}
28 changes: 28 additions & 0 deletions tests/ReturnTypeProvider/ArraySliceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Psalm\Tests\ReturnTypeProvider;

use Psalm\Tests\TestCase;
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;

class ArraySliceTest extends TestCase
{
use ValidCodeAnalysisTestTrait;

public function providerValidCodeParse(): iterable
{
yield 'arraySliceWithTemplatedArrayParameter' => [
'<?php
/**
* @template T as string[]
* @param T $a
* @return string[]
*/
function f(array $a): array
{
return array_slice($a, 1);
}
',
];
}
}

0 comments on commit ffbdaa8

Please sign in to comment.