generated from yiisoft/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDefinitionValidator.php
More file actions
354 lines (324 loc) · 11.3 KB
/
DefinitionValidator.php
File metadata and controls
354 lines (324 loc) · 11.3 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php
declare(strict_types=1);
namespace Yiisoft\Definitions\Helpers;
use ReflectionClass;
use ReflectionException;
use ReflectionProperty;
use Yiisoft\Definitions\ArrayDefinition;
use Yiisoft\Definitions\Contract\DefinitionInterface;
use Yiisoft\Definitions\Contract\ReferenceInterface;
use Yiisoft\Definitions\Exception\InvalidConfigException;
use function count;
use function in_array;
use function is_array;
use function is_callable;
use function is_object;
use function is_string;
use function sprintf;
/**
* Definition validator checks if definition is valid.
*/
final class DefinitionValidator
{
/**
* Validates that definition is valid. Throws exception otherwise.
*
* @param mixed $definition Definition to validate.
*
* @throws InvalidConfigException If definition is not valid.
* @throws ReflectionException
*/
public static function validate(mixed $definition, ?string $id = null): void
{
// Reference or ready object
if (is_object($definition) && self::isValidObject($definition)) {
return;
}
// Class
if (is_string($definition)) {
self::validateString($definition);
return;
}
// Callable definition
if (is_callable($definition, true)) {
return;
}
// Array definition
if (is_array($definition)) {
self::validateArrayDefinition($definition, $id);
return;
}
throw new InvalidConfigException(
'Invalid definition: '
. ($definition === '' ? 'empty string.' : var_export($definition, true)),
);
}
/**
* Validates that array definition is valid. Throws exception otherwise.
*
* @param array $definition Array definition to validate.
*
* @throws InvalidConfigException If definition is not valid.
* @throws ReflectionException
*/
public static function validateArrayDefinition(array $definition, ?string $id = null): void
{
/** @var class-string $className */
$className = $definition[ArrayDefinition::CLASS_NAME] ?? $id ?? throw new InvalidConfigException(
'Invalid definition: no class name specified.',
);
self::validateString($className);
$classReflection = new ReflectionClass($className);
$classPublicMethods = [];
foreach ($classReflection->getMethods() as $reflectionMethod) {
if ($reflectionMethod->isPublic() && !self::isMagicMethod($reflectionMethod->getName())) {
$classPublicMethods[] = $reflectionMethod->getName();
}
}
$classPublicProperties = [];
foreach ($classReflection->getProperties() as $reflectionProperty) {
if (self::isPublicWritableProperty($reflectionProperty)) {
$classPublicProperties[] = $reflectionProperty->getName();
}
}
foreach ($definition as $key => $value) {
if (!is_string($key)) {
throw ExceptionHelper::invalidArrayDefinitionKey($key);
}
// Class
if ($key === ArrayDefinition::CLASS_NAME) {
continue;
}
// Constructor arguments
if ($key === ArrayDefinition::CONSTRUCTOR) {
self::validateConstructor($value);
continue;
}
// Methods and properties
if ((count($methodArray = explode('()', $key)) === 2) && !empty($methodArray[0])) {
self::validateMethod($methodArray[0], $classReflection, $classPublicMethods, $className, $value);
continue;
}
if (str_starts_with($key, '$')) {
self::validateProperty($key, $classReflection, $classPublicProperties, $className);
continue;
}
$possibleOptionsMessage = self::generatePossibleMessage(
$key,
$classPublicMethods,
$classPublicProperties,
$classReflection,
$className,
);
throw new InvalidConfigException(
"Invalid definition: key \"$key\" is not allowed. $possibleOptionsMessage",
);
}
}
/**
* Deny `DefinitionInterface`, exclude `ReferenceInterface`
*/
private static function isValidObject(object $value): bool
{
return !($value instanceof DefinitionInterface) || $value instanceof ReferenceInterface;
}
private static function generatePossibleMessage(
string $key,
array $classPublicMethods,
array $classPublicProperties,
ReflectionClass $classReflection,
string $className,
): string {
$parsedKey = trim(
strtr($key, [
'()' => '',
'$' => '',
]),
);
if (in_array($parsedKey, $classPublicMethods, true)) {
return sprintf(
'Did you mean "%s"?',
$parsedKey . '()',
);
}
if (in_array($parsedKey, $classPublicProperties, true)) {
return sprintf(
'Did you mean "%s"?',
'$' . $parsedKey,
);
}
if ($classReflection->hasMethod($parsedKey)) {
return sprintf(
'Method "%s" must be public to be able to be called.',
$className . '::' . $parsedKey . '()',
);
}
if ($classReflection->hasProperty($parsedKey)) {
return sprintf(
'Property "%s" must be public to be able to be called.',
$className . '::$' . $parsedKey,
);
}
return 'The key may be a call of a method or a setting of a property.';
}
/**
* @param string[] $classPublicMethods
*
* @throws InvalidConfigException
*/
private static function validateMethod(
string $methodName,
ReflectionClass $classReflection,
array $classPublicMethods,
string $className,
mixed $value,
): void {
if (!$classReflection->hasMethod($methodName)) {
if ($classReflection->hasMethod('__call') || $classReflection->hasMethod('__callStatic')) {
/**
* Magic method may intercept the call, but reflection does not know about it.
*/
return;
}
$possiblePropertiesMessage = $classPublicMethods === []
? 'No public methods available to call.'
: sprintf(
'Possible methods to call: %s',
'"' . implode('", "', $classPublicMethods) . '"',
);
throw new InvalidConfigException(
sprintf(
'Invalid definition: class "%s" does not have the public method with name "%s". ' . $possiblePropertiesMessage,
$className,
$methodName,
),
);
}
if (!in_array($methodName, $classPublicMethods, true)) {
throw new InvalidConfigException(
sprintf(
'Invalid definition: method "%s" must be public.',
$className . '::' . $methodName . '()',
),
);
}
if (!is_array($value)) {
throw ExceptionHelper::incorrectArrayDefinitionMethodArguments($methodName . '()', $value);
}
}
/**
* @param string[] $classPublicProperties
*
* @throws InvalidConfigException
*/
private static function validateProperty(
string $key,
ReflectionClass $classReflection,
array $classPublicProperties,
string $className,
): void {
$parsedKey = substr($key, 1);
if (!$classReflection->hasProperty($parsedKey)) {
if ($classReflection->hasMethod('__set')) {
/**
* Magic method may intercept the call, but reflection does not know about it.
*/
return;
}
if ($classPublicProperties === []) {
$message = sprintf(
'Invalid definition: class "%s" does not have any public properties.',
$className,
);
} else {
$message = sprintf(
'Invalid definition: class "%s" does not have the public property with name "%s". Possible properties to set: %s.',
$className,
$parsedKey,
'"' . implode('", "', $classPublicProperties) . '"',
);
}
throw new InvalidConfigException($message);
} elseif (!in_array($parsedKey, $classPublicProperties, true)) {
throw new InvalidConfigException(
sprintf(
'Invalid definition: property "%s" must be public and writable.',
$className . '::' . $key,
),
);
}
}
/**
* @throws InvalidConfigException
*/
private static function validateConstructor(mixed $value): void
{
if (!is_array($value)) {
throw ExceptionHelper::incorrectArrayDefinitionConstructorArguments($value);
}
foreach ($value as $argument) {
if (is_object($argument) && !self::isValidObject($argument)) {
throw new InvalidConfigException(
'Only references are allowed in constructor arguments, a definition object was provided: ' .
var_export($argument, true),
);
}
}
}
private static function isMagicMethod(string $getName): bool
{
return in_array($getName, [
'__construct',
'__destruct',
'__call',
'__callStatic',
'__get',
'__set',
'__isset',
'__unset',
'__sleep',
'__wakeup',
'__serialize',
'__unserialize',
'__toString',
'__invoke',
'__set_state',
'__clone',
'__debugInfo',
], true);
}
/**
* @throws InvalidConfigException
*/
private static function validateString(mixed $class): void
{
if (!is_string($class)) {
throw new InvalidConfigException(
sprintf(
'Invalid definition: class name must be a non-empty string, got %s.',
get_debug_type($class),
),
);
}
if (trim($class) === '') {
throw new InvalidConfigException('Invalid definition: class name must be a non-empty string.');
}
}
private static function isPublicWritableProperty(ReflectionProperty $property): bool
{
if (!$property->isPublic()) {
return false;
}
if ($property->isReadOnly()) {
return false;
}
if (PHP_VERSION_ID < 80400) {
return true;
}
$modifiers = $property->getModifiers();
/**
* @psalm-suppress UndefinedConstant, MixedOperand Needs for PHP 8.3 or lower
*/
return ($modifiers & (ReflectionProperty::IS_PRIVATE_SET | ReflectionProperty::IS_PROTECTED_SET)) === 0;
}
}