-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathClassAdapter.php
More file actions
646 lines (522 loc) · 18.4 KB
/
ClassAdapter.php
File metadata and controls
646 lines (522 loc) · 18.4 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
<?php
declare(strict_types=1);
namespace Typhoon\Reflection\Internal\NativeAdapter;
use Typhoon\DeclarationId\AnonymousClassId;
use Typhoon\DeclarationId\Id;
use Typhoon\DeclarationId\NamedClassId;
use Typhoon\Reflection\ClassConstantReflection;
use Typhoon\Reflection\ClassReflection;
use Typhoon\Reflection\Collection;
use Typhoon\Reflection\Exception\DeclarationNotFound;
use Typhoon\Reflection\Internal\Data;
use Typhoon\Reflection\MethodReflection;
use Typhoon\Reflection\ModifierKind;
use Typhoon\Reflection\PropertyReflection;
use Typhoon\Reflection\ReflectionCollections;
use Typhoon\Reflection\TyphoonReflector;
use function Typhoon\Reflection\Internal\get_namespace;
use function Typhoon\Reflection\Internal\get_short_name;
/**
* @internal
* @psalm-internal Typhoon\Reflection
* @template-covariant T of object
* @extends \ReflectionClass<T>
* @property-read class-string<T> $name
* @psalm-suppress PropertyNotSetInConstructor
* @psalm-import-type Properties from ReflectionCollections
* @psalm-import-type Methods from ReflectionCollections
*/
final class ClassAdapter extends \ReflectionClass
{
public const IS_READONLY_PHP_82 = 65536;
public static function normalizeNameForException(string $name): string
{
$nullBytePosition = strpos($name, "\0");
if ($nullBytePosition === false) {
return $name;
}
return substr($name, 0, $nullBytePosition);
}
/**
* @param ClassReflection<T, NamedClassId<class-string<T>>|AnonymousClassId<?class-string<T>>> $reflection
*/
private function __construct(
private readonly ClassReflection $reflection,
private readonly TyphoonReflector $reflector,
) {
unset($this->name);
}
/**
* @template TObject of object
* @param ClassReflection<TObject, NamedClassId<class-string<TObject>>|AnonymousClassId<?class-string<TObject>>> $reflection
* @return \ReflectionClass<TObject>
*/
public static function create(ClassReflection $reflection, TyphoonReflector $reflector): \ReflectionClass
{
$adapter = new self($reflection, $reflector);
if ($reflection->isEnum()) {
/**
* @psalm-suppress ArgumentTypeCoercion
* @var \ReflectionClass<TObject>
*/
return new EnumAdapter($adapter, $reflection);
}
return $adapter;
}
public function __get(string $name): mixed
{
return match ($name) {
'name' => $this->getName(),
default => new \LogicException(\sprintf('Undefined property %s::$%s', self::class, $name)),
};
}
public function __isset(string $name): bool
{
return $name === 'name';
}
public function __toString(): string
{
$this->loadNative();
return parent::__toString();
}
public function getAttributes(?string $name = null, int $flags = 0): array
{
return AttributeAdapter::fromList($this->reflection->attributes(), $name, $flags);
}
public function getConstant(string $name): mixed
{
if ($name === '') {
return false;
}
$constant = $this->reflection->constants()[$name] ?? null;
return $constant === null ? false : $constant->evaluate();
}
public function getConstants(?int $filter = null): array
{
return $this
->reflection
->constants()
->filter(static fn(ClassConstantReflection $constant): bool => $filter === null || ($constant->toNativeReflection()->getModifiers() & $filter) !== 0)
->map(static fn(ClassConstantReflection $constant): mixed => $constant->evaluate())
->toArray();
}
public function getConstructor(): ?\ReflectionMethod
{
return ($this->nativeMethods()['__construct'] ?? null)?->toNativeReflection();
}
public function getDefaultProperties(): array
{
return $this
->nativeProperties()
->filter(static fn(PropertyReflection $property): bool => $property->hasDefaultValue())
->map(static fn(PropertyReflection $property): mixed => $property->evaluateDefault())
->toArray();
}
public function getDocComment(): string|false
{
return $this->reflection->phpDoc() ?? false;
}
public function getEndLine(): int|false
{
return $this->reflection->location()?->endLine ?? false;
}
public function getExtension(): ?\ReflectionExtension
{
$extension = $this->reflection->extension();
if ($extension === null) {
return null;
}
return new \ReflectionExtension($extension);
}
public function getExtensionName(): string|false
{
return $this->reflection->extension() ?? false;
}
public function getFileName(): string|false
{
return $this->reflection->file() ?? false;
}
public function getInterfaceNames(): array
{
return array_keys($this->reflection->data[Data::Interfaces]);
}
public function getInterfaces(): array
{
$interfaces = $this->getInterfaceNames();
return array_combine($interfaces, array_map(
fn(string $name): \ReflectionClass => $this->reflector->reflect(Id::namedClass($name))->toNativeReflection(),
$interfaces,
));
}
public function getMethod(string $name): \ReflectionMethod
{
if ($name === '') {
throw new \ReflectionException(\sprintf('Method %s::() does not exist', $this->name));
}
return ($this->nativeMethods()[$name] ?? null)
?->toNativeReflection()
?? throw new \ReflectionException(\sprintf('Method %s::%s() does not exist', $this->name, $name));
}
public function getMethods(?int $filter = null): array
{
return $this
->nativeMethods()
->map(static fn(MethodReflection $method): \ReflectionMethod => $method->toNativeReflection())
->filter(static fn(\ReflectionMethod $method): bool => $filter === null || ($method->getModifiers() & $filter) !== 0)
->toList();
}
public function getModifiers(): int
{
if ($this->isEnum()) {
return self::IS_FINAL;
}
/** @var int-mask-of<\ReflectionClass::IS_*> */
return ($this->reflection->isAbstract() ? self::IS_EXPLICIT_ABSTRACT : 0)
| ($this->isFinal() ? self::IS_FINAL : 0)
| ($this->isReadonly() ? self::IS_READONLY_PHP_82 : 0);
}
public function getName(): string
{
return $this->reflection->id->name ?? throw new \ReflectionException(\sprintf(
'Runtime name of anonymous class %s is not available',
$this->reflection->id->describe(),
));
}
public function getNamespaceName(): string
{
$name = $this->reflection->id->name;
if ($name === null) {
return '';
}
return get_namespace($name);
}
public function getParentClass(): \ReflectionClass|false
{
return $this->reflection->parent()?->toNativeReflection() ?? false;
}
public function getProperties(?int $filter = null): array
{
return $this
->nativeProperties()
->map(static fn(PropertyReflection $property): \ReflectionProperty => $property->toNativeReflection())
->filter(static fn(\ReflectionProperty $property): bool => $filter === null || ($property->getModifiers() & $filter) !== 0)
->toList();
}
public function getProperty(string $name): \ReflectionProperty
{
if ($name === '') {
throw new \ReflectionException(\sprintf('Property %s::$ does not exist', $this->name));
}
return ($this->nativeProperties()[$name] ?? null)
?->toNativeReflection()
?? throw new \ReflectionException(\sprintf('Property %s::$%s does not exist', $this->name, $name));
}
public function getReflectionConstant(string $name): \ReflectionClassConstant|false
{
if ($name === '') {
return false;
}
return ($this->reflection->constants()[$name] ?? null)
?->toNativeReflection()
?? false;
}
public function getReflectionConstants(?int $filter = null): array
{
return $this
->reflection
->constants()
->map(static fn(ClassConstantReflection $constant): \ReflectionClassConstant => $constant->toNativeReflection())
->filter(static fn(\ReflectionClassConstant $constant): bool => $filter === null || ($constant->getModifiers() & $filter) !== 0)
->toList();
}
public function getShortName(): string
{
$name = $this->reflection->id->name;
if ($name === null) {
return '{anonymous-class}';
}
$shortName = get_short_name($name);
\assert($shortName !== '', 'A valid class name must not end with a backslash');
return $shortName;
}
public function getStartLine(): int|false
{
return $this->reflection->location()?->startLine ?? false;
}
public function getStaticProperties(): array
{
$this->loadNative();
return parent::getStaticProperties();
}
public function getStaticPropertyValue(string $name, mixed $default = null): mixed
{
$this->loadNative();
return parent::getStaticPropertyValue($name, $default);
}
public function getTraitAliases(): array
{
return $this->reflection->data[NativeTraitInfoKey::Key]->aliases;
}
public function getTraitNames(): array
{
/** @var list<trait-string> */
return $this->reflection->data[NativeTraitInfoKey::Key]->names;
}
public function getTraits(): array
{
$traits = [];
foreach ($this->getTraitNames() as $name) {
$traits[$name] = $this->reflector->reflect(Id::namedClass($name))->toNativeReflection();
}
return $traits;
}
public function hasConstant(string $name): bool
{
return $name !== '' && $this->reflection->constants()->offsetExists($name);
}
public function hasMethod(string $name): bool
{
return $name !== '' && $this->nativeMethods()->offsetExists($name);
}
public function hasProperty(string $name): bool
{
return $name !== '' && $this->nativeProperties()->offsetExists($name);
}
public function implementsInterface(string|\ReflectionClass $interface): bool
{
if (\is_string($interface)) {
try {
$interfaceId = Id::class($interface);
} catch (\InvalidArgumentException) {
throw new \ReflectionException(\sprintf('Interface "%s" does not exist', self::normalizeNameForException($interface)));
}
if ($interfaceId instanceof AnonymousClassId) {
throw new \ReflectionException(\sprintf('%s is not an interface', self::normalizeNameForException($interface)));
}
try {
$interfaceReflection = $this->reflector->reflect($interfaceId)->toNativeReflection();
} catch (DeclarationNotFound) {
throw new \ReflectionException(\sprintf('Interface "%s" does not exist', self::normalizeNameForException($interface)));
}
} else {
$interfaceReflection = $interface;
}
if (!$interfaceReflection->isInterface()) {
throw new \ReflectionException(\sprintf('%s is not an interface', self::normalizeNameForException($interfaceReflection->name)));
}
return $this->reflection->isInstanceOf($interfaceReflection->name);
}
public function inNamespace(): bool
{
return str_contains($this->name, '\\');
}
public function isAbstract(): bool
{
if ($this->reflection->isAbstract()) {
return true;
}
if ($this->reflection->isInterface() || $this->reflection->isTrait()) {
return $this->nativeMethods()->any(static fn(MethodReflection $method): bool => $method->isAbstract());
}
return false;
}
public function isAnonymous(): bool
{
return $this->reflection->isAnonymous();
}
public function isCloneable(): bool
{
return $this->reflection->isCloneable();
}
public function isEnum(): bool
{
return $this->reflection->isEnum();
}
public function isFinal(): bool
{
return $this->reflection->isFinal(ModifierKind::Native);
}
public function isInstance(object $object): bool
{
if ($this->isInterface()) {
return isset(class_implements($object)[$this->name]);
}
return $object::class === $this->name || isset(class_parents($object)[$this->name]);
}
public function isInstantiable(): bool
{
return $this->reflection->isClass()
&& !$this->reflection->isAbstract()
&& (($this->nativeMethods()['__construct'] ?? null)?->isPublic() ?? true);
}
public function isInterface(): bool
{
return $this->reflection->isInterface();
}
public function isInternal(): bool
{
return $this->reflection->isInternallyDefined();
}
public function isIterable(): bool
{
return ($this->reflection->isClass() || $this->reflection->isEnum())
&& !$this->reflection->isAbstract()
&& $this->reflection->isInstanceOf(\Traversable::class);
}
public function isIterateable(): bool
{
return $this->isIterable();
}
public function isReadonly(): bool
{
return $this->reflection->isReadonly(ModifierKind::Native);
}
public function isSubclassOf(string|\ReflectionClass $class): bool
{
if (\is_string($class)) {
if ($class === $this->name) {
return false;
}
try {
$classId = Id::class($class);
} catch (\InvalidArgumentException) {
throw new \ReflectionException(\sprintf('Class "%s" does not exist', self::normalizeNameForException($class)));
}
try {
$this->reflector->reflect($classId);
} catch (DeclarationNotFound) {
throw new \ReflectionException(\sprintf('Class "%s" does not exist', self::normalizeNameForException($class)));
}
return $this->reflection->isInstanceOf($class);
}
return $class->name !== $this->name && $this->reflection->isInstanceOf($class->name);
}
public function isTrait(): bool
{
return $this->reflection->isTrait();
}
public function isUserDefined(): bool
{
return !$this->isInternal();
}
public function newInstance(mixed ...$args): object
{
$this->loadNative();
return parent::newInstanceArgs($args);
}
/**
* @psalm-suppress MethodSignatureMismatch
*/
public function newInstanceArgs(array $args = []): object
{
$this->loadNative();
return parent::newInstanceArgs($args);
}
public function newInstanceWithoutConstructor(): object
{
$this->loadNative();
return parent::newInstanceWithoutConstructor();
}
public function setStaticPropertyValue(string $name, mixed $value): void
{
$this->loadNative();
parent::setStaticPropertyValue($name, $value);
}
/**
* @psalm-suppress MixedInferredReturnType, MixedReturnStatement, UndefinedMethod
*/
public function newLazyGhost(callable $initializer, int $options = 0): object
{
$this->loadNative();
return parent::newLazyGhost($initializer, $options);
}
/**
* @psalm-suppress MixedInferredReturnType, MixedReturnStatement, UndefinedMethod
*/
public function newLazyProxy(callable $factory, int $options = 0): object
{
$this->loadNative();
return parent::newLazyProxy($factory, $options);
}
/**
* @psalm-suppress UndefinedMethod
*/
public function resetAsLazyGhost(object $object, callable $initializer, int $options = 0): void
{
$this->loadNative();
parent::resetAsLazyGhost($object, $initializer, $options);
}
/**
* @psalm-suppress UndefinedMethod
*/
public function resetAsLazyProxy(object $object, callable $factory, int $options = 0): void
{
$this->loadNative();
parent::resetAsLazyProxy($object, $factory, $options);
}
/**
* @psalm-suppress MixedInferredReturnType, MixedReturnStatement, UndefinedMethod
*/
public function initializeLazyObject(object $object): object
{
$this->loadNative();
return parent::initializeLazyObject($object);
}
/**
* @psalm-suppress MixedInferredReturnType, MixedReturnStatement, UndefinedMethod
*/
public function markLazyObjectAsInitialized(object $object): object
{
$this->loadNative();
return parent::markLazyObjectAsInitialized($object);
}
/**
* @psalm-suppress MixedInferredReturnType, MixedReturnStatement, UndefinedMethod
*/
public function getLazyInitializer(object $object): ?callable
{
$this->loadNative();
return parent::getLazyInitializer($object);
}
/**
* @psalm-suppress MixedInferredReturnType, MixedReturnStatement, UndefinedMethod
*/
public function isUninitializedLazyObject(object $object): bool
{
$this->loadNative();
return parent::isUninitializedLazyObject($object);
}
/**
* @return Properties
*/
private function nativeProperties(): Collection
{
return $this
->reflection
->properties()
->filter(static fn(PropertyReflection $property): bool => $property->isNative());
}
/**
* @return Methods
*/
private function nativeMethods(): Collection
{
return $this
->reflection
->methods()
->filter(static fn(MethodReflection $method): bool => $method->isNative());
}
private bool $nativeLoaded = false;
private function loadNative(): void
{
if ($this->nativeLoaded) {
return;
}
$class = $this->reflection->id->name ?? throw new \LogicException(\sprintf(
"Cannot natively reflect %s, because it's runtime name is not available",
$this->reflection->id->describe(),
));
parent::__construct($class);
$this->nativeLoaded = true;
}
}