-
Notifications
You must be signed in to change notification settings - Fork 653
/
PluginRegistrationSocket.php
213 lines (186 loc) · 7.12 KB
/
PluginRegistrationSocket.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
namespace Psalm;
use Psalm\Internal\Analyzer\FileAnalyzer;
use Psalm\Internal\Scanner\FileScanner;
use Psalm\Plugin\EventHandler;
use Psalm\Plugin\Hook;
use Psalm\Plugin\RegistrationInterface;
use function class_exists;
use function in_array;
use function is_a;
use function is_subclass_of;
use function sprintf;
class PluginRegistrationSocket implements RegistrationInterface
{
/** @var Config */
private $config;
/** @var Codebase */
private $codebase;
/**
* @var array<string, class-string<FileScanner>>
*/
private $additionalFileTypeScanners = [];
/**
* @var array<string, class-string<FileAnalyzer>>
*/
private $additionalFileTypeAnalyzers = [];
/**
* @var list<string>
*/
private $additionalFileExtensions = [];
/**
* @internal
*/
public function __construct(Config $config, Codebase $codebase)
{
$this->config = $config;
$this->codebase = $codebase;
}
public function addStubFile(string $file_name): void
{
$this->config->addStubFile($file_name);
}
public function registerHooksFromClass(string $handler): void
{
if (!class_exists($handler, false)) {
throw new \InvalidArgumentException('Plugins must be loaded before registration');
}
$this->config->eventDispatcher->registerClass($handler);
if (is_subclass_of($handler, Hook\PropertyExistenceProviderInterface::class) ||
is_subclass_of($handler, EventHandler\PropertyExistenceProviderInterface::class)
) {
$this->codebase->properties->property_existence_provider->registerClass($handler);
}
if (is_subclass_of($handler, Hook\PropertyVisibilityProviderInterface::class) ||
is_subclass_of($handler, EventHandler\PropertyVisibilityProviderInterface::class)
) {
$this->codebase->properties->property_visibility_provider->registerClass($handler);
}
if (is_subclass_of($handler, Hook\PropertyTypeProviderInterface::class) ||
is_subclass_of($handler, EventHandler\PropertyTypeProviderInterface::class)
) {
$this->codebase->properties->property_type_provider->registerClass($handler);
}
if (is_subclass_of($handler, Hook\MethodExistenceProviderInterface::class) ||
is_subclass_of($handler, EventHandler\MethodExistenceProviderInterface::class)
) {
$this->codebase->methods->existence_provider->registerClass($handler);
}
if (is_subclass_of($handler, Hook\MethodVisibilityProviderInterface::class) ||
is_subclass_of($handler, EventHandler\MethodVisibilityProviderInterface::class)
) {
$this->codebase->methods->visibility_provider->registerClass($handler);
}
if (is_subclass_of($handler, Hook\MethodReturnTypeProviderInterface::class) ||
is_subclass_of($handler, EventHandler\MethodReturnTypeProviderInterface::class)
) {
$this->codebase->methods->return_type_provider->registerClass($handler);
}
if (is_subclass_of($handler, Hook\MethodParamsProviderInterface::class) ||
is_subclass_of($handler, EventHandler\MethodParamsProviderInterface::class)
) {
$this->codebase->methods->params_provider->registerClass($handler);
}
if (is_subclass_of($handler, Hook\FunctionExistenceProviderInterface::class) ||
is_subclass_of($handler, EventHandler\FunctionExistenceProviderInterface::class)
) {
$this->codebase->functions->existence_provider->registerClass($handler);
}
if (is_subclass_of($handler, Hook\FunctionParamsProviderInterface::class) ||
is_subclass_of($handler, EventHandler\FunctionParamsProviderInterface::class)
) {
$this->codebase->functions->params_provider->registerClass($handler);
}
if (is_subclass_of($handler, Hook\FunctionReturnTypeProviderInterface::class) ||
is_subclass_of($handler, EventHandler\FunctionReturnTypeProviderInterface::class)
) {
$this->codebase->functions->return_type_provider->registerClass($handler);
}
}
/**
* @param string $fileExtension e.g. `'html'`
* @param class-string<FileScanner> $className
*/
public function addFileTypeScanner(string $fileExtension, string $className): void
{
if (!class_exists($className) || !is_a($className, FileScanner::class, true)) {
throw new \LogicException(
sprintf(
'Class %s must be of type %s',
$className,
FileScanner::class
),
1622727271
);
}
if (!empty($this->config->getFiletypeScanners()[$fileExtension])
|| !empty($this->additionalFileTypeScanners[$fileExtension])
) {
throw new \LogicException(
sprintf('Cannot redeclare scanner for file-type %s', $fileExtension),
1622727272
);
}
$this->additionalFileTypeScanners[$fileExtension] = $className;
$this->addFileExtension($fileExtension);
}
/**
* @return array<string, class-string<FileScanner>>
*/
public function getAdditionalFileTypeScanners(): array
{
return $this->additionalFileTypeScanners;
}
/**
* @param string $fileExtension e.g. `'html'`
* @param class-string<FileAnalyzer> $className
*/
public function addFileTypeAnalyzer(string $fileExtension, string $className): void
{
if (!class_exists($className) || !is_a($className, FileAnalyzer::class, true)) {
throw new \LogicException(
sprintf(
'Class %s must be of type %s',
$className,
FileAnalyzer::class
),
1622727281
);
}
if (!empty($this->config->getFiletypeAnalyzers()[$fileExtension])
|| !empty($this->additionalFileTypeAnalyzers[$fileExtension])
) {
throw new \LogicException(
sprintf('Cannot redeclare analyzer for file-type %s', $fileExtension),
1622727282
);
}
$this->additionalFileTypeAnalyzers[$fileExtension] = $className;
$this->addFileExtension($fileExtension);
}
/**
* @return array<string, class-string<FileAnalyzer>>
*/
public function getAdditionalFileTypeAnalyzers(): array
{
return $this->additionalFileTypeAnalyzers;
}
/**
* @return list<string> e.g. `['html', 'perl']`
*/
public function getAdditionalFileExtensions(): array
{
return $this->additionalFileExtensions;
}
/**
* @param string $fileExtension e.g. `'html'`
*/
private function addFileExtension(string $fileExtension): void
{
if (!in_array($fileExtension, $this->additionalFileExtensions, true)
&& !in_array($fileExtension, $this->config->getFileExtensions(), true)
) {
$this->additionalFileExtensions[] = $fileExtension;
}
}
}