-
Notifications
You must be signed in to change notification settings - Fork 651
/
MethodParamsProvider.php
148 lines (130 loc) · 4.25 KB
/
MethodParamsProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
namespace Psalm\Internal\Provider;
use Closure;
use PhpParser\Node\Arg;
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\Internal\Provider\ReturnTypeProvider\PdoStatementSetFetchMode;
use Psalm\Plugin\EventHandler\Event\MethodParamsProviderEvent;
use Psalm\Plugin\EventHandler\MethodParamsProviderInterface;
use Psalm\Plugin\Hook\MethodParamsProviderInterface as LegacyMethodParamsProviderInterface;
use Psalm\StatementsSource;
use Psalm\Storage\FunctionLikeParameter;
use function array_values;
use function is_subclass_of;
use function strtolower;
class MethodParamsProvider
{
/**
* @var array<
* lowercase-string,
* array<Closure(MethodParamsProviderEvent): ?array<int, FunctionLikeParameter>>
* >
*/
private static $handlers = [];
/**
* @var array<
* lowercase-string,
* array<Closure(
* string,
* string,
* ?list<Arg>=,
* ?StatementsSource=,
* ?Context=,
* ?CodeLocation=
* ): ?array<int, FunctionLikeParameter>>
* >
*/
private static $legacy_handlers = [];
public function __construct()
{
self::$handlers = [];
self::$legacy_handlers = [];
$this->registerClass(PdoStatementSetFetchMode::class);
}
/**
* @param class-string $class
*/
public function registerClass(string $class): void
{
if (is_subclass_of($class, LegacyMethodParamsProviderInterface::class, true)) {
$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)) {
$callable = Closure::fromCallable([$class, 'getMethodParams']);
foreach ($class::getClassLikeNames() as $fq_classlike_name) {
$this->registerClosure($fq_classlike_name, $callable);
}
}
}
/**
* @param Closure(MethodParamsProviderEvent): ?array<int, FunctionLikeParameter> $c
*/
public function registerClosure(string $fq_classlike_name, Closure $c): void
{
self::$handlers[strtolower($fq_classlike_name)][] = $c;
}
/**
* @param Closure(
* string,
* string,
* ?list<Arg>=,
* ?StatementsSource=,
* ?Context=,
* ?CodeLocation=
* ): ?array<int, FunctionLikeParameter> $c
*/
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)]);
}
/**
* @param ?list<Arg> $call_args
*
* @return ?list<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 array_values($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 array_values($result);
}
}
return null;
}
}