1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Merge pull request #6019 from elnoro/array-reduce-static

Possible implementation for ArrayReduceReturnTypeProvider support array callables with self and static
This commit is contained in:
Bruce Weirdan 2021-06-29 21:03:05 +03:00 committed by GitHub
commit c06f1cd2c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 1 deletions

View File

@ -217,7 +217,14 @@ class ArrayReduceReturnTypeProvider implements \Psalm\Plugin\EventHandler\Functi
[$callable_fq_class_name, $method_name] = explode('::', $mapping_function_id_part);
if (in_array($callable_fq_class_name, ['self', 'static', 'parent'], true)) {
if (in_array($callable_fq_class_name, ['self', 'static'], true)) {
$callable_fq_class_name = $statements_source->getFQCLN();
if ($callable_fq_class_name === null) {
continue;
}
}
if ($callable_fq_class_name === 'parent') {
continue;
}

View File

@ -1325,6 +1325,28 @@ class ArrayFunctionCallTest extends TestCase
'$function_call_result' => 'int',
],
],
'arrayReduceStaticMethods' => [
'<?php
$arr = [2, 3, 4, 5];
class C {
public static function multiply (int $carry, int $item) : int {
return $carry * $item;
}
public static function multiplySelf(array $arr): int {
return array_reduce($arr, [self::class, "multiply"], 1);
}
public static function multiplyStatic(array $arr): int {
return array_reduce($arr, [static::class, "multiply"], 1);
}
}
$self_call_result = C::multiplySelf($arr);
$static_call_result = C::multiplyStatic($arr);',
'assertions' => [],
],
'arrayReduceMixedReturn' => [
'<?php
$arr = [2, 3, 4, 5];