1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Humbug\PhpScoper; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use function array_key_exists; |
9
|
|
|
use function array_keys; |
10
|
|
|
use function array_values; |
11
|
|
|
use function gettype; |
12
|
|
|
use function is_array; |
13
|
|
|
use function is_bool; |
14
|
|
|
use function is_string; |
15
|
|
|
use function Safe\sprintf; |
16
|
|
|
|
17
|
|
|
final class ConfigurationWhitelistFactory |
18
|
|
|
{ |
19
|
|
|
private RegexChecker $regexChecker; |
20
|
|
|
|
21
|
|
|
public function __construct(RegexChecker $regexChecker) |
22
|
|
|
{ |
23
|
|
|
$this->regexChecker = $regexChecker; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function createWhitelist(array $config): Whitelist |
27
|
|
|
{ |
28
|
|
|
[ |
29
|
|
|
$excludedNamespaceRegexes, |
30
|
|
|
$excludedNamespaceNames, |
31
|
|
|
] = $this->retrieveExcludedNamespaces($config); |
32
|
|
|
|
33
|
|
|
$exposedElements = self::retrieveExposedElements($config); |
34
|
|
|
|
35
|
|
|
$exposeGlobalConstants = self::retrieveExposeGlobalSymbol( |
36
|
|
|
$config, |
37
|
|
|
ConfigurationKeys::EXPOSE_GLOBAL_CONSTANTS_KEYWORD, |
38
|
|
|
); |
39
|
|
|
$exposeGlobalClasses = self::retrieveExposeGlobalSymbol( |
40
|
|
|
$config, |
41
|
|
|
ConfigurationKeys::EXPOSE_GLOBAL_CLASSES_KEYWORD, |
42
|
|
|
); |
43
|
|
|
$exposeGlobalFunctions = self::retrieveExposeGlobalSymbol( |
44
|
|
|
$config, |
45
|
|
|
ConfigurationKeys::EXPOSE_GLOBAL_FUNCTIONS_KEYWORD, |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
return Whitelist::create( |
49
|
|
|
$exposeGlobalConstants, |
50
|
|
|
$exposeGlobalClasses, |
51
|
|
|
$exposeGlobalFunctions, |
52
|
|
|
$excludedNamespaceRegexes, |
53
|
|
|
$excludedNamespaceNames, |
54
|
|
|
...$exposedElements, |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return array{string[], string[]} |
|
|
|
|
60
|
|
|
*/ |
61
|
|
|
private function retrieveExcludedNamespaces(array $config): array |
62
|
|
|
{ |
63
|
|
|
$key = ConfigurationKeys::EXCLUDE_NAMESPACES_KEYWORD; |
64
|
|
|
|
65
|
|
|
if (!array_key_exists($key, $config)) { |
66
|
|
|
return [[], []]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$regexesAndNamespaceNames = $config[$key]; |
70
|
|
|
|
71
|
|
|
if (!is_array($regexesAndNamespaceNames)) { |
72
|
|
|
throw new InvalidArgumentException( |
73
|
|
|
sprintf( |
74
|
|
|
'Expected "%s" to be an array of strings, got "%s" instead.', |
75
|
|
|
$key, |
76
|
|
|
gettype($regexesAndNamespaceNames), |
77
|
|
|
), |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// Store the strings in the keys for avoiding a unique check later on |
82
|
|
|
$regexes = []; |
83
|
|
|
$namespaceNames = []; |
84
|
|
|
|
85
|
|
|
foreach ($regexesAndNamespaceNames as $index => $regexOrNamespaceName) { |
86
|
|
|
if (!is_string($regexOrNamespaceName)) { |
87
|
|
|
throw new InvalidArgumentException( |
88
|
|
|
sprintf( |
89
|
|
|
'Expected "%s" to be an array of strings, got "%s" for the element with the index "%s".', |
90
|
|
|
$key, |
91
|
|
|
gettype($regexOrNamespaceName), |
92
|
|
|
$index, |
93
|
|
|
), |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if (!$this->regexChecker->isRegexLike($regexOrNamespaceName)) { |
98
|
|
|
$namespaceNames[$regexOrNamespaceName] = null; |
99
|
|
|
|
100
|
|
|
continue; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$errorMessage = $this->regexChecker->validateRegex($regexOrNamespaceName); |
104
|
|
|
|
105
|
|
|
if (null !== $errorMessage) { |
106
|
|
|
throw new InvalidArgumentException( |
107
|
|
|
sprintf( |
108
|
|
|
'Expected "%s" to be an array of valid regexes. The element "%s" with the index "%s" is not: %s.', |
109
|
|
|
$key, |
110
|
|
|
$regexOrNamespaceName, |
111
|
|
|
$index, |
112
|
|
|
$errorMessage, |
113
|
|
|
), |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$regexes[$regexOrNamespaceName] = null; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return [ |
121
|
|
|
array_keys($regexes), |
122
|
|
|
array_keys($namespaceNames), |
123
|
|
|
]; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* return list<string> |
128
|
|
|
*/ |
129
|
|
|
private static function retrieveExposedElements(array $config): array |
130
|
|
|
{ |
131
|
|
|
$key = ConfigurationKeys::WHITELIST_KEYWORD; |
132
|
|
|
|
133
|
|
|
if (!array_key_exists($key, $config)) { |
134
|
|
|
return []; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$whitelist = $config[$key]; |
138
|
|
|
|
139
|
|
|
if (!is_array($whitelist)) { |
140
|
|
|
throw new InvalidArgumentException( |
141
|
|
|
sprintf( |
142
|
|
|
'Expected "%s" to be an array of strings, found "%s" instead.', |
143
|
|
|
$key, |
144
|
|
|
gettype($whitelist), |
145
|
|
|
), |
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
foreach ($whitelist as $index => $className) { |
150
|
|
|
if (is_string($className)) { |
151
|
|
|
continue; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
throw new InvalidArgumentException( |
155
|
|
|
sprintf( |
156
|
|
|
'Expected whitelist to be an array of string, the "%d" element is not.', |
157
|
|
|
$index, |
158
|
|
|
), |
159
|
|
|
); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return array_values($whitelist); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
private static function retrieveExposeGlobalSymbol(array $config, string $key): bool |
166
|
|
|
{ |
167
|
|
|
if (!array_key_exists($key, $config)) { |
168
|
|
|
return false; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$value = $config[$key]; |
172
|
|
|
|
173
|
|
|
if (!is_bool($value)) { |
174
|
|
|
throw new InvalidArgumentException( |
175
|
|
|
sprintf( |
176
|
|
|
'Expected %s to be a boolean, found "%s" instead.', |
177
|
|
|
$key, |
178
|
|
|
gettype($value), |
179
|
|
|
), |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $value; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|