2019-03-01 14:57:10 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Internal\Provider;
|
|
|
|
|
|
|
|
use PhpParser;
|
2019-07-05 22:24:00 +02:00
|
|
|
use Psalm\Plugin\Hook\FunctionExistenceProviderInterface;
|
2019-03-01 14:57:10 +01:00
|
|
|
use Psalm\StatementsSource;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function strtolower;
|
2019-03-01 14:57:10 +01:00
|
|
|
|
|
|
|
class FunctionExistenceProvider
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array<
|
|
|
|
* string,
|
|
|
|
* array<\Closure(
|
|
|
|
* StatementsSource,
|
|
|
|
* string
|
|
|
|
* ) : ?bool>
|
|
|
|
* >
|
|
|
|
*/
|
|
|
|
private static $handlers = [];
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
self::$handlers = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param class-string<FunctionExistenceProviderInterface> $class
|
2019-07-05 22:24:00 +02:00
|
|
|
*
|
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
|
|
|
{
|
2019-08-18 22:10:12 +02:00
|
|
|
$callable = \Closure::fromCallable([$class, 'doesFunctionExist']);
|
2019-03-01 14:57:10 +01:00
|
|
|
|
|
|
|
foreach ($class::getFunctionIds() as $function_id) {
|
|
|
|
$this->registerClosure($function_id, $callable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* /**
|
|
|
|
* @param \Closure(
|
|
|
|
* StatementsSource,
|
|
|
|
* string
|
|
|
|
* ) : ?bool $c
|
|
|
|
*
|
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function registerClosure(string $function_id, \Closure $c): void
|
2019-03-01 14:57:10 +01:00
|
|
|
{
|
|
|
|
self::$handlers[$function_id][] = $c;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function has(string $function_id) : bool
|
|
|
|
{
|
|
|
|
return isset(self::$handlers[strtolower($function_id)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<PhpParser\Node\Arg> $call_args
|
2019-07-05 22:24:00 +02:00
|
|
|
*
|
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 {
|
2019-03-01 14:57:10 +01:00
|
|
|
foreach (self::$handlers[strtolower($function_id)] as $function_handler) {
|
|
|
|
$function_exists = $function_handler(
|
|
|
|
$statements_source,
|
|
|
|
$function_id
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($function_exists !== null) {
|
|
|
|
return $function_exists;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|