2019-03-01 14:57:10 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Internal\Provider;
|
|
|
|
|
2021-12-03 21:40:18 +01:00
|
|
|
use Closure;
|
2021-01-06 15:05:53 +01:00
|
|
|
use Psalm\Plugin\EventHandler\Event\FunctionExistenceProviderEvent;
|
|
|
|
use Psalm\Plugin\EventHandler\FunctionExistenceProviderInterface;
|
2021-06-08 04:55:21 +02:00
|
|
|
use Psalm\Plugin\Hook\FunctionExistenceProviderInterface as LegacyFunctionExistenceProviderInterface;
|
2019-03-01 14:57:10 +01:00
|
|
|
use Psalm\StatementsSource;
|
2021-06-08 04:55:21 +02:00
|
|
|
|
2021-01-06 15:05:53 +01:00
|
|
|
use function is_subclass_of;
|
2021-06-08 04:55:21 +02:00
|
|
|
use function strtolower;
|
2019-03-01 14:57:10 +01:00
|
|
|
|
|
|
|
class FunctionExistenceProvider
|
|
|
|
{
|
2021-01-06 15:05:53 +01:00
|
|
|
/**
|
|
|
|
* @var array<
|
|
|
|
* lowercase-string,
|
2021-12-05 18:51:26 +01:00
|
|
|
* array<Closure(FunctionExistenceProviderEvent): ?bool>
|
2021-01-06 15:05:53 +01:00
|
|
|
* >
|
|
|
|
*/
|
|
|
|
private static $handlers = [];
|
|
|
|
|
2019-03-01 14:57:10 +01:00
|
|
|
/**
|
|
|
|
* @var array<
|
2020-12-29 12:42:12 +01:00
|
|
|
* lowercase-string,
|
2021-12-04 03:37:19 +01:00
|
|
|
* array<Closure(
|
2019-03-01 14:57:10 +01:00
|
|
|
* StatementsSource,
|
|
|
|
* string
|
2021-12-05 18:51:26 +01:00
|
|
|
* ): ?bool>
|
2019-03-01 14:57:10 +01:00
|
|
|
* >
|
|
|
|
*/
|
2021-01-06 15:05:53 +01:00
|
|
|
private static $legacy_handlers = [];
|
2019-03-01 14:57:10 +01:00
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
self::$handlers = [];
|
2021-01-06 15:05:53 +01:00
|
|
|
self::$legacy_handlers = [];
|
2019-03-01 14:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-06 15:05:53 +01:00
|
|
|
* @param class-string $class
|
2019-03-01 14:57:10 +01:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function registerClass(string $class): void
|
2019-03-01 14:57:10 +01:00
|
|
|
{
|
2021-01-06 15:05:53 +01:00
|
|
|
if (is_subclass_of($class, LegacyFunctionExistenceProviderInterface::class, true)) {
|
2021-12-03 21:40:18 +01:00
|
|
|
$callable = Closure::fromCallable([$class, 'doesFunctionExist']);
|
2019-03-01 14:57:10 +01:00
|
|
|
|
2021-01-06 15:05:53 +01:00
|
|
|
foreach ($class::getFunctionIds() as $function_id) {
|
|
|
|
$this->registerLegacyClosure($function_id, $callable);
|
|
|
|
}
|
|
|
|
} elseif (is_subclass_of($class, FunctionExistenceProviderInterface::class, true)) {
|
2021-12-03 21:40:18 +01:00
|
|
|
$callable = Closure::fromCallable([$class, 'doesFunctionExist']);
|
2021-01-06 15:05:53 +01:00
|
|
|
|
|
|
|
foreach ($class::getFunctionIds() as $function_id) {
|
|
|
|
$this->registerClosure($function_id, $callable);
|
|
|
|
}
|
2019-03-01 14:57:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-06 15:05:53 +01:00
|
|
|
/**
|
|
|
|
* @param lowercase-string $function_id
|
2021-12-05 18:51:26 +01:00
|
|
|
* @param Closure(FunctionExistenceProviderEvent): ?bool $c
|
2021-01-06 15:05:53 +01:00
|
|
|
*/
|
2021-12-03 21:40:18 +01:00
|
|
|
public function registerClosure(string $function_id, Closure $c): void
|
2021-01-06 15:05:53 +01:00
|
|
|
{
|
|
|
|
self::$handlers[$function_id][] = $c;
|
|
|
|
}
|
|
|
|
|
2019-03-01 14:57:10 +01:00
|
|
|
/**
|
2020-12-29 12:42:12 +01:00
|
|
|
* @param lowercase-string $function_id
|
2021-12-04 03:37:19 +01:00
|
|
|
* @param Closure(
|
2019-03-01 14:57:10 +01:00
|
|
|
* StatementsSource,
|
|
|
|
* string
|
2021-12-05 18:51:26 +01:00
|
|
|
* ): ?bool $c
|
2019-03-01 14:57:10 +01:00
|
|
|
*/
|
2021-12-03 21:40:18 +01:00
|
|
|
public function registerLegacyClosure(string $function_id, Closure $c): void
|
2019-03-01 14:57:10 +01:00
|
|
|
{
|
2021-01-06 15:05:53 +01:00
|
|
|
self::$legacy_handlers[$function_id][] = $c;
|
2019-03-01 14:57:10 +01:00
|
|
|
}
|
|
|
|
|
2021-12-05 18:51:26 +01:00
|
|
|
public function has(string $function_id): bool
|
2019-03-01 14:57:10 +01:00
|
|
|
{
|
2021-01-06 15:05:53 +01:00
|
|
|
return isset(self::$handlers[strtolower($function_id)]) ||
|
|
|
|
isset(self::$legacy_handlers[strtolower($function_id)]);
|
2019-03-01 14:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function doesFunctionExist(
|
|
|
|
StatementsSource $statements_source,
|
|
|
|
string $function_id
|
2020-09-04 22:26:33 +02:00
|
|
|
): ?bool {
|
2021-05-01 22:56:25 +02:00
|
|
|
foreach (self::$legacy_handlers[strtolower($function_id)] ?? [] as $function_handler) {
|
|
|
|
$function_exists = $function_handler(
|
2021-01-06 15:05:53 +01:00
|
|
|
$statements_source,
|
|
|
|
$function_id
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($function_exists !== null) {
|
|
|
|
return $function_exists;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-01 22:56:25 +02:00
|
|
|
foreach (self::$handlers[strtolower($function_id)] ?? [] as $function_handler) {
|
|
|
|
$event = new FunctionExistenceProviderEvent(
|
2019-03-01 14:57:10 +01:00
|
|
|
$statements_source,
|
|
|
|
$function_id
|
|
|
|
);
|
2021-05-01 22:56:25 +02:00
|
|
|
$function_exists = $function_handler($event);
|
2019-03-01 14:57:10 +01:00
|
|
|
|
|
|
|
if ($function_exists !== null) {
|
|
|
|
return $function_exists;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|