2022-01-25 23:16:34 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Psalm\Tests\Config\Plugin\Hook;
|
|
|
|
|
|
|
|
use Psalm\Codebase;
|
|
|
|
use Psalm\Plugin\DynamicFunctionStorage;
|
|
|
|
use Psalm\Plugin\DynamicTemplateProvider;
|
2022-01-28 10:51:01 +01:00
|
|
|
use Psalm\Plugin\EventHandler\DynamicFunctionStorageProviderInterface;
|
2022-01-28 12:39:01 +01:00
|
|
|
use Psalm\Plugin\EventHandler\Event\DynamicFunctionStorageProviderEvent;
|
2022-01-25 23:16:34 +01:00
|
|
|
use Psalm\Storage\FunctionLikeParameter;
|
|
|
|
use Psalm\Type;
|
|
|
|
use Psalm\Type\Atomic\TCallable;
|
|
|
|
use Psalm\Type\Atomic\TTemplateParam;
|
|
|
|
use Psalm\Type\Union;
|
|
|
|
|
|
|
|
use function array_keys;
|
|
|
|
use function array_map;
|
|
|
|
use function count;
|
|
|
|
|
2022-01-28 10:51:01 +01:00
|
|
|
class CustomArrayMapFunctionStorageProvider implements DynamicFunctionStorageProviderInterface
|
2022-01-25 23:16:34 +01:00
|
|
|
{
|
|
|
|
public static function getFunctionIds(): array
|
|
|
|
{
|
|
|
|
return ['custom_array_map'];
|
|
|
|
}
|
|
|
|
|
2022-01-28 10:51:01 +01:00
|
|
|
public static function getFunctionStorage(DynamicFunctionStorageProviderEvent $event): ?DynamicFunctionStorage
|
2022-01-25 23:16:34 +01:00
|
|
|
{
|
|
|
|
$template_provider = $event->getTemplateProvider();
|
|
|
|
$arg_type_inferer = $event->getArgTypeInferer();
|
2022-01-26 12:14:24 +01:00
|
|
|
$call_args = $event->getArgs();
|
2022-01-25 23:16:34 +01:00
|
|
|
|
2022-12-13 21:40:19 +01:00
|
|
|
if (count($call_args) < 2) {
|
2022-01-25 23:16:34 +01:00
|
|
|
return null;
|
|
|
|
}
|
2022-12-13 21:40:19 +01:00
|
|
|
$args_count = count($call_args);
|
|
|
|
$expected_callable_args_count = $args_count - 1;
|
2022-01-25 23:16:34 +01:00
|
|
|
|
|
|
|
$last_arg = $call_args[$args_count - 1];
|
|
|
|
|
|
|
|
if (!($input_array_type = $arg_type_inferer->infer($last_arg)) ||
|
|
|
|
!($input_value_type = self::toValueType($event->getCodebase(), $input_array_type))
|
|
|
|
) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$all_expected_callables = [
|
|
|
|
self::createExpectedCallable($input_value_type, $template_provider),
|
|
|
|
...self::createRestCallables($template_provider, $expected_callable_args_count),
|
|
|
|
];
|
|
|
|
|
|
|
|
$custom_array_map_storage = new DynamicFunctionStorage();
|
|
|
|
$custom_array_map_storage->templates = self::createTemplates($template_provider, $expected_callable_args_count);
|
|
|
|
$custom_array_map_storage->return_type = self::createReturnType($all_expected_callables);
|
|
|
|
$custom_array_map_storage->params = [
|
|
|
|
...array_map(
|
|
|
|
function (TCallable $expected, int $offset) {
|
2022-10-03 11:53:05 +02:00
|
|
|
$t = new Union([$expected]);
|
|
|
|
$param = new FunctionLikeParameter('fn' . $offset, false, $t, $t);
|
2022-01-25 23:16:34 +01:00
|
|
|
$param->is_optional = false;
|
|
|
|
|
|
|
|
return $param;
|
|
|
|
},
|
|
|
|
$all_expected_callables,
|
|
|
|
array_keys($all_expected_callables)
|
|
|
|
),
|
|
|
|
self::createLastArrayMapParam($input_array_type)
|
|
|
|
];
|
|
|
|
|
|
|
|
return $custom_array_map_storage;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function createLastArrayMapParam(Union $input_array_type): FunctionLikeParameter
|
|
|
|
{
|
2022-10-03 11:53:05 +02:00
|
|
|
return new FunctionLikeParameter(
|
|
|
|
'input',
|
|
|
|
false,
|
|
|
|
$input_array_type,
|
|
|
|
$input_array_type,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
false
|
|
|
|
);
|
2022-01-25 23:16:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolves value type from array-like type:
|
|
|
|
* list<int> -> int
|
|
|
|
* list<int|string> -> int|string
|
|
|
|
*/
|
|
|
|
private static function toValueType(Codebase $codebase, Union $array_like_type): ?Union
|
|
|
|
{
|
|
|
|
$value_types = [];
|
|
|
|
|
|
|
|
foreach ($array_like_type->getAtomicTypes() as $atomic) {
|
2022-12-13 21:40:19 +01:00
|
|
|
if ($atomic instanceof Type\Atomic\TArray) {
|
2022-01-25 23:16:34 +01:00
|
|
|
$value_types[] = $atomic->type_params[1];
|
|
|
|
} elseif ($atomic instanceof Type\Atomic\TKeyedArray) {
|
|
|
|
$value_types[] = $atomic->getGenericValueType();
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Type::combineUnionTypeArray($value_types, $codebase);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function createExpectedCallable(
|
|
|
|
Union $input_type,
|
|
|
|
DynamicTemplateProvider $template_provider,
|
|
|
|
int $return_template_offset = 0
|
|
|
|
): TCallable {
|
2022-10-03 13:58:01 +02:00
|
|
|
return new TCallable(
|
|
|
|
'callable',
|
|
|
|
[new FunctionLikeParameter('a', false, $input_type, $input_type)],
|
|
|
|
new Union([
|
|
|
|
$template_provider->createTemplate('T' . $return_template_offset)
|
|
|
|
])
|
|
|
|
);
|
2022-01-25 23:16:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return list<TCallable>
|
|
|
|
*/
|
|
|
|
private static function createRestCallables(
|
|
|
|
DynamicTemplateProvider $template_provider,
|
|
|
|
int $expected_callable_args_count
|
|
|
|
): array {
|
|
|
|
$rest_callable_params = [];
|
|
|
|
|
|
|
|
for ($template_offset = 0; $template_offset < $expected_callable_args_count - 1; $template_offset++) {
|
|
|
|
$rest_callable_params[] = self::createExpectedCallable(
|
|
|
|
new Union([
|
|
|
|
$template_provider->createTemplate('T' . $template_offset)
|
|
|
|
]),
|
|
|
|
$template_provider,
|
|
|
|
$template_offset + 1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rest_callable_params;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extracts return type for custom_array_map from last callable arg.
|
|
|
|
*
|
2022-12-13 21:40:19 +01:00
|
|
|
* @param non-empty-list<TCallable> $all_expected_callables
|
2022-01-25 23:16:34 +01:00
|
|
|
*/
|
|
|
|
private static function createReturnType(array $all_expected_callables): Union
|
|
|
|
{
|
|
|
|
$last_callable_arg = $all_expected_callables[count($all_expected_callables) - 1];
|
|
|
|
|
2022-12-13 21:40:19 +01:00
|
|
|
return Type::getList($last_callable_arg->return_type ?? Type::getMixed());
|
2022-01-25 23:16:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates variadic template list for custom_array_map function.
|
|
|
|
*
|
|
|
|
* @return list<TTemplateParam>
|
|
|
|
*/
|
|
|
|
private static function createTemplates(
|
|
|
|
DynamicTemplateProvider $template_provider,
|
|
|
|
int $expected_callable_count
|
|
|
|
): array {
|
|
|
|
$template_params = [];
|
|
|
|
|
|
|
|
for ($i = 0; $i < $expected_callable_count; $i++) {
|
|
|
|
$template_params[] = $template_provider->createTemplate('T' . $i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $template_params;
|
|
|
|
}
|
|
|
|
}
|