-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathSymbolsConfigurationFactory.php
291 lines (249 loc) · 8.22 KB
/
SymbolsConfigurationFactory.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
declare(strict_types=1);
/*
* This file is part of the humbug/php-scoper package.
*
* Copyright (c) 2017 Théo FIDRY <theo.fidry@gmail.com>,
* Pádraic Brady <padraic.brady@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Humbug\PhpScoper\Configuration;
use Humbug\PhpScoper\Symbol\NamespaceRegistry;
use Humbug\PhpScoper\Symbol\SymbolRegistry;
use InvalidArgumentException;
use function array_key_exists;
use function array_keys;
use function get_debug_type;
use function gettype;
use function is_array;
use function is_bool;
use function is_string;
use function sprintf;
use function str_contains;
use function strrpos;
use function substr;
final readonly class SymbolsConfigurationFactory
{
public function __construct(private RegexChecker $regexChecker)
{
}
/**
* @param array<array-key, mixed> $config
*/
public function createSymbolsConfiguration(array $config): SymbolsConfiguration
{
[
$excludedNamespaceNames,
$excludedNamespaceRegexes,
] = $this->retrieveElements(
$config,
ConfigurationKeys::EXCLUDE_NAMESPACES_KEYWORD,
);
[
$exposedNamespaceNames,
$exposedNamespaceRegexes,
] = $this->retrieveElements(
$config,
ConfigurationKeys::EXPOSE_NAMESPACES_KEYWORD,
);
$exposeGlobalConstants = self::retrieveExposeGlobalSymbol(
$config,
ConfigurationKeys::EXPOSE_GLOBAL_CONSTANTS_KEYWORD,
);
$exposeGlobalClasses = self::retrieveExposeGlobalSymbol(
$config,
ConfigurationKeys::EXPOSE_GLOBAL_CLASSES_KEYWORD,
);
$exposeGlobalFunctions = self::retrieveExposeGlobalSymbol(
$config,
ConfigurationKeys::EXPOSE_GLOBAL_FUNCTIONS_KEYWORD,
);
[$exposedClassNames, $exposedClassRegexes] = $this->retrieveElements(
$config,
ConfigurationKeys::EXPOSE_CLASSES_SYMBOLS_KEYWORD,
);
[$exposedFunctionNames, $exposedFunctionRegexes] = $this->retrieveElements(
$config,
ConfigurationKeys::EXPOSE_FUNCTIONS_SYMBOLS_KEYWORD,
);
[$exposedConstantNames, $exposedConstantRegexes] = $this->retrieveElements(
$config,
ConfigurationKeys::EXPOSE_CONSTANTS_SYMBOLS_KEYWORD,
);
$excludedClasses = SymbolRegistry::create(
...$this->retrieveElements(
$config,
ConfigurationKeys::CLASSES_INTERNAL_SYMBOLS_KEYWORD,
),
);
$excludedFunctions = SymbolRegistry::create(
...$this->retrieveElements(
$config,
ConfigurationKeys::FUNCTIONS_INTERNAL_SYMBOLS_KEYWORD,
),
);
$excludedConstants = SymbolRegistry::createForConstants(
...$this->retrieveElements(
$config,
ConfigurationKeys::CONSTANTS_INTERNAL_SYMBOLS_KEYWORD,
),
);
return SymbolsConfiguration::create(
$exposeGlobalConstants,
$exposeGlobalClasses,
$exposeGlobalFunctions,
NamespaceRegistry::create(
$excludedNamespaceNames,
$excludedNamespaceRegexes,
),
NamespaceRegistry::create(
$exposedNamespaceNames,
$exposedNamespaceRegexes,
),
SymbolRegistry::create(
$exposedClassNames,
$exposedClassRegexes,
),
SymbolRegistry::create(
$exposedFunctionNames,
$exposedFunctionRegexes,
),
SymbolRegistry::createForConstants(
$exposedConstantNames,
$exposedConstantRegexes,
),
$excludedClasses,
$excludedFunctions,
$excludedConstants,
);
}
/**
* @param array<array-key, mixed> $config
*/
private static function retrieveExposeGlobalSymbol(array $config, string $key): bool
{
if (!array_key_exists($key, $config)) {
return true;
}
$value = $config[$key];
if (!is_bool($value)) {
throw new InvalidArgumentException(
sprintf(
'Expected %s to be a boolean, found "%s" instead.',
$key,
gettype($value),
),
);
}
return $value;
}
/**
* @param array<array-key, mixed> $config
* @return array{list<string>, list<string>}
*/
private function retrieveElements(array $config, string $key): array
{
if (!array_key_exists($key, $config)) {
return [[], []];
}
$symbolNamesAndRegexes = $config[$key];
self::assertIsArrayOfStrings($symbolNamesAndRegexes, $key);
// Store the strings in the keys for avoiding a unique check later on
$names = [];
$regexes = [];
foreach ($symbolNamesAndRegexes as $index => $nameOrRegex) {
if (!$this->regexChecker->isRegexLike($nameOrRegex)) {
$names[$nameOrRegex] = null;
continue;
}
$regex = $this->getRegex($nameOrRegex, $key, $index);
$regexes[$regex] = null;
}
return [
array_keys($names),
array_keys($regexes),
];
}
private function getRegex(string $regex, string $key, int|string $index): string
{
$this->assertValidRegex($regex, $key, (string) $index);
$errorMessage = $this->regexChecker->validateRegex($regex);
if (null !== $errorMessage) {
throw new InvalidArgumentException(
sprintf(
'Expected "%s" to be an array of valid regexes. The element "%s" with the index "%s" is not: %s.',
$key,
$regex,
$index,
$errorMessage,
),
);
}
$flags = self::getRegexFlags($regex);
if (!str_contains($flags, 'i')) {
// Ensure namespace comparisons are always case-insensitive
$regex .= 'i';
}
return $regex;
}
/**
* @param non-empty-string $regex
*/
private static function getRegexFlags(string $regex): string
{
$separator = $regex[0];
$lastSeparatorPosition = strrpos($regex, $separator);
if (false === $lastSeparatorPosition) {
return '';
}
return substr($regex, $lastSeparatorPosition);
}
/**
* @phpstan-assert string[] $value
*/
private static function assertIsArrayOfStrings(mixed $value, string $key): void
{
if (!is_array($value)) {
throw new InvalidArgumentException(
sprintf(
'Expected "%s" to be an array of strings, found "%s" instead.',
$key,
get_debug_type($value),
),
);
}
foreach ($value as $index => $element) {
if (is_string($element)) {
continue;
}
throw new InvalidArgumentException(
sprintf(
'Expected "%s" to be an array of strings, found "%s" for the element with the index "%s".',
$key,
get_debug_type($element),
$index,
),
);
}
}
/**
* @phpstan-assert non-empty-string $regex
*/
private function assertValidRegex(string $regex, string $key, string $index): void
{
$errorMessage = $this->regexChecker->validateRegex($regex);
if (null !== $errorMessage) {
throw new InvalidArgumentException(
sprintf(
'Expected "%s" to be an array of valid regexes. The element "%s" with the index "%s" is not: %s.',
$key,
$regex,
$index,
$errorMessage,
),
);
}
}
}