2019-03-01 14:57:10 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Internal\Provider;
|
|
|
|
|
2019-07-05 22:24:00 +02:00
|
|
|
use const PHP_VERSION;
|
2019-03-01 14:57:10 +01:00
|
|
|
use PhpParser;
|
|
|
|
use Psalm\CodeLocation;
|
2019-07-05 22:24:00 +02:00
|
|
|
use Psalm\Context;
|
2019-03-01 14:57:10 +01:00
|
|
|
use Psalm\Plugin\Hook\MethodParamsProviderInterface;
|
2019-07-05 22:24:00 +02:00
|
|
|
use Psalm\StatementsSource;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function strtolower;
|
2019-07-05 22:24:00 +02:00
|
|
|
use function version_compare;
|
2019-03-01 14:57:10 +01:00
|
|
|
|
|
|
|
class MethodParamsProvider
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array<
|
|
|
|
* string,
|
|
|
|
* array<\Closure(
|
|
|
|
* string,
|
|
|
|
* string,
|
|
|
|
* ?array<PhpParser\Node\Arg>=,
|
|
|
|
* ?StatementsSource=,
|
|
|
|
* ?Context=,
|
|
|
|
* ?CodeLocation=
|
|
|
|
* ) : ?array<int, \Psalm\Storage\FunctionLikeParameter>>
|
|
|
|
* >
|
|
|
|
*/
|
|
|
|
private static $handlers = [];
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
self::$handlers = [];
|
2019-03-29 18:26:13 +01:00
|
|
|
|
|
|
|
$this->registerClass(ReturnTypeProvider\PdoStatementSetFetchMode::class);
|
2019-03-01 14:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param class-string<MethodParamsProviderInterface> $class
|
2019-07-05 22:24:00 +02:00
|
|
|
*
|
2019-03-01 14:57:10 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function registerClass(string $class)
|
|
|
|
{
|
2019-08-18 22:10:12 +02:00
|
|
|
$callable = \Closure::fromCallable([$class, 'getMethodParams']);
|
2019-03-01 14:57:10 +01:00
|
|
|
|
|
|
|
foreach ($class::getClassLikeNames() as $fq_classlike_name) {
|
|
|
|
/** @psalm-suppress MixedTypeCoercion */
|
|
|
|
$this->registerClosure($fq_classlike_name, $callable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \Closure(
|
|
|
|
* string,
|
|
|
|
* string,
|
|
|
|
* ?array<PhpParser\Node\Arg>=,
|
|
|
|
* ?StatementsSource=,
|
|
|
|
* ?Context=,
|
|
|
|
* ?CodeLocation=
|
|
|
|
* ) : ?array<int, \Psalm\Storage\FunctionLikeParameter> $c
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function registerClosure(string $fq_classlike_name, \Closure $c)
|
|
|
|
{
|
|
|
|
self::$handlers[strtolower($fq_classlike_name)][] = $c;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function has(string $fq_classlike_name) : bool
|
|
|
|
{
|
|
|
|
return isset(self::$handlers[strtolower($fq_classlike_name)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ?array<PhpParser\Node\Arg> $call_args
|
2019-07-05 22:24:00 +02:00
|
|
|
*
|
2019-03-01 14:57:10 +01: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
|
|
|
|
) {
|
|
|
|
foreach (self::$handlers[strtolower($fq_classlike_name)] as $class_handler) {
|
|
|
|
$result = $class_handler(
|
|
|
|
$fq_classlike_name,
|
|
|
|
$method_name_lowercase,
|
|
|
|
$call_args,
|
|
|
|
$statements_source,
|
|
|
|
$context,
|
|
|
|
$code_location
|
|
|
|
);
|
|
|
|
|
2019-06-27 17:51:56 +02:00
|
|
|
if ($result !== null) {
|
2019-03-01 14:57:10 +01:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|