1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-10 23:18:40 +01:00
psalm/src/Psalm/Internal/Provider/FunctionParamsProvider.php

139 lines
3.9 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Internal\Provider;
2021-12-03 21:40:18 +01:00
use Closure;
2021-12-04 03:37:19 +01:00
use PhpParser\Node\Arg;
use Psalm\CodeLocation;
2019-07-05 22:24:00 +02:00
use Psalm\Context;
use Psalm\Plugin\EventHandler\Event\FunctionParamsProviderEvent;
use Psalm\Plugin\EventHandler\FunctionParamsProviderInterface;
use Psalm\Plugin\Hook\FunctionParamsProviderInterface as LegacyFunctionParamsProviderInterface;
2019-07-05 22:24:00 +02:00
use Psalm\StatementsSource;
2021-12-04 03:37:19 +01:00
use Psalm\Storage\FunctionLikeParameter;
2021-06-08 04:55:21 +02:00
use function is_subclass_of;
2021-06-08 04:55:21 +02:00
use function strtolower;
class FunctionParamsProvider
{
/**
* @var array<
* lowercase-string,
* array<Closure(FunctionParamsProviderEvent): ?array<int, FunctionLikeParameter>>
* >
*/
private static $handlers = [];
/**
* @var array<
* lowercase-string,
2021-12-04 03:37:19 +01:00
* array<Closure(
* StatementsSource,
* string,
2021-12-04 03:37:19 +01:00
* list<Arg>,
* ?Context=,
* ?CodeLocation=
* ): ?array<int, FunctionLikeParameter>>
* >
*/
private static $legacy_handlers = [];
public function __construct()
{
self::$handlers = [];
self::$legacy_handlers = [];
}
/**
* @param class-string<LegacyFunctionParamsProviderInterface>|class-string<FunctionParamsProviderInterface> $class
*/
public function registerClass(string $class): void
{
if (is_subclass_of($class, LegacyFunctionParamsProviderInterface::class, true)) {
2021-12-03 21:40:18 +01:00
$callable = Closure::fromCallable([$class, 'getFunctionParams']);
foreach ($class::getFunctionIds() as $function_id) {
$this->registerLegacyClosure($function_id, $callable);
}
} elseif (is_subclass_of($class, FunctionParamsProviderInterface::class, true)) {
2021-12-03 21:40:18 +01:00
$callable = Closure::fromCallable([$class, 'getFunctionParams']);
foreach ($class::getFunctionIds() as $function_id) {
$this->registerClosure($function_id, $callable);
}
}
}
/**
* @param Closure(FunctionParamsProviderEvent): ?array<int, FunctionLikeParameter> $c
*/
2021-12-03 21:40:18 +01:00
public function registerClosure(string $fq_classlike_name, Closure $c): void
{
self::$handlers[strtolower($fq_classlike_name)][] = $c;
}
/**
2021-12-04 03:37:19 +01:00
* @param Closure(
* StatementsSource,
* string,
2021-12-04 03:37:19 +01:00
* list<Arg>,
* ?Context=,
* ?CodeLocation=
* ): ?array<int, FunctionLikeParameter> $c
*/
2021-12-03 21:40:18 +01:00
public function registerLegacyClosure(string $fq_classlike_name, Closure $c): void
{
self::$legacy_handlers[strtolower($fq_classlike_name)][] = $c;
}
public function has(string $fq_classlike_name): bool
{
return isset(self::$handlers[strtolower($fq_classlike_name)]) ||
isset(self::$legacy_handlers[strtolower($fq_classlike_name)]);
}
/**
2021-12-04 03:37:19 +01:00
* @param list<Arg> $call_args
2019-07-05 22:24:00 +02:00
*
2021-12-04 03:37:19 +01:00
* @return ?array<int, FunctionLikeParameter>
*/
public function getFunctionParams(
StatementsSource $statements_source,
string $function_id,
array $call_args,
?Context $context = null,
?CodeLocation $code_location = null
): ?array {
foreach (self::$handlers[strtolower($function_id)] ?? [] as $class_handler) {
$event = new FunctionParamsProviderEvent(
$statements_source,
$function_id,
$call_args,
$context,
$code_location
);
$result = $class_handler($event);
if ($result) {
return $result;
}
}
foreach (self::$legacy_handlers[strtolower($function_id)] ?? [] as $class_handler) {
$result = $class_handler(
$statements_source,
$function_id,
$call_args,
$context,
$code_location
);
if ($result) {
return $result;
}
}
return null;
}
}