1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-09 14:38:37 +01:00
psalm/src/Psalm/Internal/Provider/MethodParamsProvider.php

145 lines
4.2 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Internal\Provider;
2021-12-03 21:40:18 +01:00
use Closure;
use PhpParser;
use Psalm\CodeLocation;
2019-07-05 22:24:00 +02:00
use Psalm\Context;
use Psalm\Plugin\EventHandler\Event\MethodParamsProviderEvent;
use Psalm\Plugin\EventHandler\MethodParamsProviderInterface;
2021-06-08 04:55:21 +02:00
use Psalm\Plugin\Hook\MethodParamsProviderInterface as LegacyMethodParamsProviderInterface;
2019-07-05 22:24:00 +02:00
use Psalm\StatementsSource;
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 MethodParamsProvider
{
/**
* @var array<
* lowercase-string,
* array<\Closure(MethodParamsProviderEvent) : ?array<int, \Psalm\Storage\FunctionLikeParameter>>
* >
*/
private static $handlers = [];
/**
* @var array<
* lowercase-string,
* array<\Closure(
* string,
* string,
2020-10-28 17:45:26 +01:00
* ?list<PhpParser\Node\Arg>=,
* ?StatementsSource=,
* ?Context=,
* ?CodeLocation=
* ) : ?array<int, \Psalm\Storage\FunctionLikeParameter>>
* >
*/
private static $legacy_handlers = [];
public function __construct()
{
self::$handlers = [];
self::$legacy_handlers = [];
$this->registerClass(ReturnTypeProvider\PdoStatementSetFetchMode::class);
}
/**
* @param class-string $class
*/
public function registerClass(string $class): void
{
if (is_subclass_of($class, LegacyMethodParamsProviderInterface::class, true)) {
2021-12-03 21:40:18 +01:00
$callable = Closure::fromCallable([$class, 'getMethodParams']);
foreach ($class::getClassLikeNames() as $fq_classlike_name) {
$this->registerLegacyClosure($fq_classlike_name, $callable);
}
} elseif (is_subclass_of($class, MethodParamsProviderInterface::class, true)) {
2021-12-03 21:40:18 +01:00
$callable = Closure::fromCallable([$class, 'getMethodParams']);
foreach ($class::getClassLikeNames() as $fq_classlike_name) {
$this->registerClosure($fq_classlike_name, $callable);
}
}
}
/**
* @param \Closure(MethodParamsProviderEvent) : ?array<int, \Psalm\Storage\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;
}
/**
* @param \Closure(
* string,
* string,
2020-10-28 17:45:26 +01:00
* ?list<PhpParser\Node\Arg>=,
* ?StatementsSource=,
* ?Context=,
* ?CodeLocation=
* ) : ?array<int, \Psalm\Storage\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)]);
}
/**
2020-10-28 17:45:26 +01:00
* @param ?list<PhpParser\Node\Arg> $call_args
2019-07-05 22:24:00 +02:00
*
* @return ?array<int, \Psalm\Storage\FunctionLikeParameter>
*/
public function getMethodParams(
string $fq_classlike_name,
string $method_name_lowercase,
?array $call_args = null,
?StatementsSource $statements_source = null,
?Context $context = null,
?CodeLocation $code_location = null
): ?array {
foreach (self::$legacy_handlers[strtolower($fq_classlike_name)] ?? [] as $class_handler) {
$result = $class_handler(
$fq_classlike_name,
$method_name_lowercase,
$call_args,
$statements_source,
$context,
$code_location
);
if ($result !== null) {
return $result;
}
}
foreach (self::$handlers[strtolower($fq_classlike_name)] ?? [] as $class_handler) {
$event = new MethodParamsProviderEvent(
$fq_classlike_name,
$method_name_lowercase,
$call_args,
$statements_source,
$context,
$code_location
);
$result = $class_handler($event);
if ($result !== null) {
return $result;
}
}
return null;
}
}