-
-
Notifications
You must be signed in to change notification settings - Fork 192
Expand file tree
/
Copy pathReflectionService.php
More file actions
2044 lines (1816 loc) · 80.6 KB
/
Copy pathReflectionService.php
File metadata and controls
2044 lines (1816 loc) · 80.6 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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
declare(strict_types=1);
namespace Neos\Flow\Reflection;
/*
* This file is part of the Neos.Flow package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\PhpParser;
use Doctrine\Common\Annotations\Reader;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Persistence\Proxy as DoctrineProxy;
use Neos\Cache\Backend\FreezableBackendInterface;
use Neos\Cache\Exception;
use Neos\Cache\Frontend\VariableFrontend;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Log\Utility\LogEnvironment;
use Neos\Flow\ObjectManagement\Proxy\ProxyInterface;
use Neos\Flow\Package\PackageManager;
use Neos\Flow\Persistence\RepositoryInterface;
use Neos\Flow\Reflection\Exception\ClassLoadingForReflectionFailedException;
use Neos\Flow\Reflection\Exception\ClassSchemaConstraintViolationException;
use Neos\Flow\Reflection\Exception\InvalidClassException;
use Neos\Flow\Reflection\Exception\InvalidPropertyTypeException;
use Neos\Flow\Reflection\Exception\InvalidValueObjectException;
use Neos\Utility\TypeHandling;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
/**
* A service for acquiring reflection based information in a performant way. This
* service also builds up class schema information which is used by the Flow's
* persistence layer.
*
* Reflection of classes of all active packages is triggered through the bootstrap's
* initializeReflectionService() method. In a development context, single classes
* may be re-reflected once files are modified whereas in a production context
* reflection is done once and successive requests read from the frozen caches for
* performance reasons.
*
* The list of available classes is determined by the CompiletimeObjectManager which
* also triggers the initial build of reflection data in this service.
*
* The invalidation of reflection cache entries is done by the CacheManager which
* in turn is triggered by signals sent by the file monitor.
*
* The internal representation of cache data is optimized for memory consumption and
* speed by using constants which have an integer value.
*
* @api
* @Flow\Scope("singleton")
* @Flow\Proxy(false)
*/
class ReflectionService
{
protected const VISIBILITY_PRIVATE = 1;
protected const VISIBILITY_PROTECTED = 2;
protected const VISIBILITY_PUBLIC = 3;
// Implementations of an interface
protected const DATA_INTERFACE_IMPLEMENTATIONS = 1;
// Subclasses of a class
protected const DATA_CLASS_SUBCLASSES = 3;
// Class annotations
protected const DATA_CLASS_ANNOTATIONS = 5;
protected const DATA_CLASS_ABSTRACT = 6;
protected const DATA_CLASS_FINAL = 7;
protected const DATA_CLASS_READONLY = 27;
protected const DATA_CLASS_METHODS = 8;
protected const DATA_CLASS_PROPERTIES = 9;
protected const DATA_METHOD_FINAL = 10;
protected const DATA_METHOD_STATIC = 11;
protected const DATA_METHOD_VISIBILITY = 12;
protected const DATA_METHOD_PARAMETERS = 13;
protected const DATA_METHOD_DECLARED_RETURN_TYPE = 25;
protected const DATA_PROPERTY_TAGS_VALUES = 14;
protected const DATA_PROPERTY_ANNOTATIONS = 15;
protected const DATA_PROPERTY_VISIBILITY = 24;
protected const DATA_PROPERTY_TYPE = 26;
protected const DATA_PROPERTY_PROMOTED = 28;
protected const DATA_PARAMETER_POSITION = 16;
protected const DATA_PARAMETER_OPTIONAL = 17;
protected const DATA_PARAMETER_TYPE = 18;
protected const DATA_PARAMETER_ARRAY = 19;
protected const DATA_PARAMETER_CLASS = 20;
protected const DATA_PARAMETER_ALLOWS_NULL = 21;
protected const DATA_PARAMETER_DEFAULT_VALUE = 22;
protected const DATA_PARAMETER_BY_REFERENCE = 23;
protected const DATA_PARAMETER_SCALAR_DECLARATION = 24;
protected const DATA_PARAMETER_ANNOTATIONS = 25;
protected Reader $annotationReader;
protected array $availableClassNames = [];
protected VariableFrontend $reflectionDataRuntimeCache;
protected VariableFrontend $classSchemataRuntimeCache;
protected ?LoggerInterface $logger = null;
/**
* The doctrine PHP parser which can parse "use" statements. Is initialized
* lazily when it is first needed.
* Note: Don't refer to this member directly but use getDoctrinePhpParser() to obtain an instance
*/
protected ?PhpParser $doctrinePhpParser = null;
/**
* a cache which stores the use statements reflected for a particular class
* (only relevant for un-expanded "var" and "param" annotations)
*/
protected array $useStatementsForClassCache;
protected array $settings = [];
/**
* Array of annotation classnames and the names of classes which are annotated with them
*/
protected array $annotatedClasses = [];
/**
* Array of method annotations and the classes and methods which are annotated with them
*/
protected array $classesByMethodAnnotations = [];
/**
* Schemata of all classes which can be persisted
*
* @var array<string, ClassSchema|false>
*/
protected array $classSchemata = [];
/**
* An array of class names which are currently being forgotten by forgetClass(). Acts as a safeguard against infinite loops.
*/
protected array $classesCurrentlyBeingForgotten = [];
/**
* Array with reflection information indexed by class name
*/
protected array $classReflectionData = [];
/**
* Array with updated reflection information (e.g. in Development context after classes have changed)
*/
protected array $updatedReflectionData = [];
protected bool $initialized = false;
/**
* A runtime cache for reflected method annotations to speed up repeating checks.
*/
protected array $methodAnnotationsRuntimeCache = [];
public function setReflectionDataRuntimeCache(VariableFrontend $cache): void
{
$this->reflectionDataRuntimeCache = $cache;
}
public function setClassSchemataRuntimeCache(VariableFrontend $cache): void
{
$this->classSchemataRuntimeCache = $cache;
}
public function injectSettings(array $settings): void
{
$this->settings = $settings['reflection'];
}
public function injectLogger(LoggerInterface $logger): void
{
$this->logger = $logger;
}
protected function getDoctrinePhpParser(): PhpParser
{
if ($this->doctrinePhpParser === null) {
$this->doctrinePhpParser = new PhpParser();
}
return $this->doctrinePhpParser;
}
/**
* Initialize the reflection service lazily
*
* This method must be run only after all dependencies have been injected.
*/
protected function initialize(): void
{
$classNames = $this->reflectionDataRuntimeCache->get('__classNames');
if (is_array($classNames)) {
$this->classReflectionData = $classNames;
$this->annotatedClasses = $this->reflectionDataRuntimeCache->get('__annotatedClasses');
$this->classesByMethodAnnotations = $this->reflectionDataRuntimeCache->get('__classesByMethodAnnotations');
}
$this->annotationReader = new AnnotationReader();
foreach ($this->settings['ignoredTags'] as $tagName => $ignoreFlag) {
if ($ignoreFlag === true) {
AnnotationReader::addGlobalIgnoredName($tagName);
}
}
$this->initialized = true;
}
/**
* Builds the reflection data cache during compile time.
*
* This method is called by the Compile Time Object Manager which also determines
* the list of classes to consider for reflection.
*
* @param array $availableClassNames
* @throws ClassLoadingForReflectionFailedException
* @throws ClassSchemaConstraintViolationException
* @throws Exception
* @throws InvalidClassException
* @throws InvalidPropertyTypeException
* @throws InvalidValueObjectException
* @throws \ReflectionException
*/
public function buildReflectionData(array $availableClassNames): void
{
if (!$this->initialized) {
$this->initialize();
}
$this->availableClassNames = $availableClassNames;
$this->forgetChangedClasses();
$this->reflectEmergedClasses();
}
/**
* Tells if the specified class is known to this reflection service and
* reflection information is available.
*
* @param class-string $className
*
* @api
*/
public function isClassReflected(string $className): bool
{
if (!$this->initialized) {
$this->initialize();
}
$className = $this->cleanClassName($className);
return isset($this->classReflectionData[$className]) && is_array($this->classReflectionData[$className]);
}
/**
* Returns the names of all classes known to this reflection service.
*
* @api
*/
public function getAllClassNames(): array
{
if (!$this->initialized) {
$this->initialize();
}
$classNames = array_keys($this->classReflectionData);
sort($classNames);
return $classNames;
}
/**
* Searches for and returns the class name of the default implementation of the given
* interface name. If no class implementing the interface was found or more than one
* implementation was found in the package defining the interface, false is returned.
*
* @param class-string $interfaceName
* @return string|bool
* @throws ClassLoadingForReflectionFailedException
* @throws InvalidClassException
* @throws \ReflectionException
* @api
*/
public function getDefaultImplementationClassNameForInterface(string $interfaceName): string|bool
{
if (interface_exists($interfaceName) === false) {
throw new \InvalidArgumentException('"' . $interfaceName . '" does not exist or is not the name of an interface.', 1238769559);
}
$interfaceName = $this->prepareClassReflectionForUsage($interfaceName);
$classNamesFound = isset($this->classReflectionData[$interfaceName][self::DATA_INTERFACE_IMPLEMENTATIONS]) ? array_keys($this->classReflectionData[$interfaceName][self::DATA_INTERFACE_IMPLEMENTATIONS]) : [];
if (count($classNamesFound) === 1) {
return $classNamesFound[0];
}
if (!isset($this->classReflectionData[ProxyInterface::class][self::DATA_INTERFACE_IMPLEMENTATIONS]) || count($classNamesFound) !== 2) {
return false;
}
if (isset($this->classReflectionData[ProxyInterface::class][self::DATA_INTERFACE_IMPLEMENTATIONS][$classNamesFound[0]])) {
return $classNamesFound[0];
}
if (isset($this->classReflectionData[ProxyInterface::class][self::DATA_INTERFACE_IMPLEMENTATIONS][$classNamesFound[1]])) {
return $classNamesFound[1];
}
return false;
}
/**
* Searches for and returns all class names of implementations of the given object type
* (interface name). If no class implementing the interface was found, an empty array is returned.
*
* @template T of object
* @param class-string<T> $interfaceName
* @return list<class-string<T>>
* @throws ClassLoadingForReflectionFailedException
* @throws InvalidClassException
* @throws \ReflectionException
* @api
*/
public function getAllImplementationClassNamesForInterface(string $interfaceName): array
{
if (interface_exists($interfaceName) === false) {
throw new \InvalidArgumentException('"' . $interfaceName . '" does not exist or is not the name of an interface.', 1238769560);
}
$interfaceName = $this->prepareClassReflectionForUsage($interfaceName);
return (isset($this->classReflectionData[$interfaceName][self::DATA_INTERFACE_IMPLEMENTATIONS])) ? array_keys($this->classReflectionData[$interfaceName][self::DATA_INTERFACE_IMPLEMENTATIONS]) : [];
}
/**
* Searches for and returns all names of classes inheriting the specified class.
* If no class inheriting the given class was found, an empty array is returned.
*
* @param class-string $className
* @return array<class-string>
* @throws ClassLoadingForReflectionFailedException
* @throws InvalidClassException
* @throws \ReflectionException
* @api
*/
public function getAllSubClassNamesForClass(string $className): array
{
if (class_exists($className) === false) {
throw new \InvalidArgumentException('"' . $className . '" does not exist or is not the name of a class.', 1257168042);
}
$className = $this->prepareClassReflectionForUsage($className);
return (isset($this->classReflectionData[$className][self::DATA_CLASS_SUBCLASSES])) ? array_keys($this->classReflectionData[$className][self::DATA_CLASS_SUBCLASSES]) : [];
}
/**
* Searches for and returns all names of classes which are tagged by the specified
* annotation. If no classes were found, an empty array is returned.
*/
public function getClassNamesByAnnotation(string $annotationClassName): array
{
if (!$this->initialized) {
$this->initialize();
}
$annotationClassName = $this->cleanClassName($annotationClassName);
return (isset($this->annotatedClasses[$annotationClassName]) ? array_keys($this->annotatedClasses[$annotationClassName]) : []);
}
/**
* Tells if the specified class has the given annotation
*
* @api
*/
public function isClassAnnotatedWith(string $className, string $annotationClassName): bool
{
if (!$this->initialized) {
$this->initialize();
}
$className = $this->cleanClassName($className);
$annotationClassName = $this->cleanClassName($annotationClassName);
return (isset($this->annotatedClasses[$annotationClassName][$className]));
}
/**
* Returns the specified class annotations or an empty array
*
* @param class-string $className
* @param null|class-string $annotationClassName
* @return array<object>
* @throws ClassLoadingForReflectionFailedException
* @throws InvalidClassException
* @throws \ReflectionException
*/
public function getClassAnnotations(string $className, string|null $annotationClassName = null): array
{
$className = $this->prepareClassReflectionForUsage($className);
$annotationClassName = $annotationClassName === null ? null : $this->cleanClassName($annotationClassName);
if (!isset($this->classReflectionData[$className][self::DATA_CLASS_ANNOTATIONS])) {
return [];
}
if ($annotationClassName === null) {
return $this->classReflectionData[$className][self::DATA_CLASS_ANNOTATIONS];
}
$annotations = [];
foreach ($this->classReflectionData[$className][self::DATA_CLASS_ANNOTATIONS] as $annotation) {
if ($annotation instanceof $annotationClassName) {
$annotations[] = $annotation;
}
}
return $annotations;
}
/**
* Returns the specified class annotation or NULL.
*
* If multiple annotations are set on the target you will
* get the first instance of them.
*
* @param class-string $className
* @param class-string $annotationClassName
* @return object|null
* @throws ClassLoadingForReflectionFailedException
* @throws InvalidClassException
* @throws \ReflectionException
*/
public function getClassAnnotation(string $className, string $annotationClassName): ?object
{
if (!$this->initialized) {
$this->initialize();
}
$annotations = $this->getClassAnnotations($className, $annotationClassName);
return $annotations === [] ? null : reset($annotations);
}
/**
* Tells if the specified class implements the given interface
*
* @throws ClassLoadingForReflectionFailedException
* @throws InvalidClassException
* @throws \ReflectionException
* @api
*/
public function isClassImplementationOf(string $className, string $interfaceName): bool
{
$className = $this->prepareClassReflectionForUsage($className);
$interfaceName = $this->cleanClassName($interfaceName);
$this->loadOrReflectClassIfNecessary($interfaceName);
return (isset($this->classReflectionData[$interfaceName][self::DATA_INTERFACE_IMPLEMENTATIONS][$className]));
}
/**
* Tells if the specified class is abstract or not
*
* @param class-string $className
* @throws ClassLoadingForReflectionFailedException
* @throws InvalidClassException
* @throws \ReflectionException
* @api
*/
public function isClassAbstract(string $className): bool
{
$className = $this->prepareClassReflectionForUsage($className);
return isset($this->classReflectionData[$className][self::DATA_CLASS_ABSTRACT]);
}
/**
* Tells if the specified class is final or not
*
* @param class-string $className Name of the class to analyze
* @throws ClassLoadingForReflectionFailedException
* @throws InvalidClassException
* @throws \ReflectionException
* @api
*/
public function isClassFinal(string $className): bool
{
$className = $this->prepareClassReflectionForUsage($className);
return isset($this->classReflectionData[$className][self::DATA_CLASS_FINAL]);
}
/**
* Tells if the specified class is readonly or not
*
* @param string $className Name of the class to analyze
* @return bool true if the class is readonly, otherwise false
* @throws ClassLoadingForReflectionFailedException
* @throws InvalidClassException
* @throws \ReflectionException
* @api
*/
public function isClassReadonly(string $className): bool
{
$className = $this->prepareClassReflectionForUsage($className);
return isset($this->classReflectionData[$className][self::DATA_CLASS_READONLY]);
}
/**
* Tells if the class is unconfigurable or not
*
* @api
*/
public function isClassUnconfigurable(string $className): bool
{
$className = $this->cleanClassName($className);
return $this->classReflectionData[$className] === [];
}
/**
* Returns all class names of classes containing at least one method annotated
* with the given annotation class
*
* @api
*/
public function getClassesContainingMethodsAnnotatedWith(string $annotationClassName): array
{
if (!$this->initialized) {
$this->initialize();
}
return isset($this->classesByMethodAnnotations[$annotationClassName]) ? array_keys($this->classesByMethodAnnotations[$annotationClassName]) : [];
}
/**
* Returns all names of methods of the given class that are annotated with the given annotation class
*
* @api
*/
public function getMethodsAnnotatedWith(string $className, string $annotationClassName): array
{
if (!$this->initialized) {
$this->initialize();
}
return $this->classesByMethodAnnotations[$annotationClassName][$className] ?? [];
}
/**
* Tells if the specified method is final or not
*
* @param string $className
* @param string $methodName
* @return bool
* @throws ClassLoadingForReflectionFailedException
* @throws InvalidClassException
* @throws \ReflectionException
* @api
*/
public function isMethodFinal(string $className, string $methodName): bool
{
$className = $this->prepareClassReflectionForUsage($className);
return isset($this->classReflectionData[$className][self::DATA_CLASS_METHODS][$methodName][self::DATA_METHOD_FINAL]);
}
/**
* Tells if the specified method is declared as static or not
*
* @param class-string $className
* @throws ClassLoadingForReflectionFailedException
* @throws InvalidClassException
* @throws \ReflectionException
* @api
*/
public function isMethodStatic(string $className, string $methodName): bool
{
$className = $this->prepareClassReflectionForUsage($className);
return isset($this->classReflectionData[$className][self::DATA_CLASS_METHODS][$methodName][self::DATA_METHOD_STATIC]);
}
/**
* @param class-string $className
* @throws ClassLoadingForReflectionFailedException
* @throws InvalidClassException
* @throws \ReflectionException
* @api
*/
public function isMethodPublic(string $className, string $methodName): bool
{
$className = $this->prepareClassReflectionForUsage($className);
return (isset($this->classReflectionData[$className][self::DATA_CLASS_METHODS][$methodName][self::DATA_METHOD_VISIBILITY]) && $this->classReflectionData[$className][self::DATA_CLASS_METHODS][$methodName][self::DATA_METHOD_VISIBILITY] === self::VISIBILITY_PUBLIC);
}
/**
* @param class-string $className
* @throws ClassLoadingForReflectionFailedException
* @throws InvalidClassException
* @throws \ReflectionException
* @api
*/
public function isMethodProtected(string $className, string $methodName): bool
{
$className = $this->prepareClassReflectionForUsage($className);
return (isset($this->classReflectionData[$className][self::DATA_CLASS_METHODS][$methodName][self::DATA_METHOD_VISIBILITY]) && $this->classReflectionData[$className][self::DATA_CLASS_METHODS][$methodName][self::DATA_METHOD_VISIBILITY] === self::VISIBILITY_PROTECTED);
}
/**
* @param class-string $className
* @throws ClassLoadingForReflectionFailedException
* @throws InvalidClassException
* @throws \ReflectionException
* @api
*/
public function isMethodPrivate(string $className, string $methodName): bool
{
$className = $this->prepareClassReflectionForUsage($className);
return (isset($this->classReflectionData[$className][self::DATA_CLASS_METHODS][$methodName][self::DATA_METHOD_VISIBILITY]) && $this->classReflectionData[$className][self::DATA_CLASS_METHODS][$methodName][self::DATA_METHOD_VISIBILITY] === self::VISIBILITY_PRIVATE);
}
/**
* Tells if the specified method is tagged with the given tag
*
* @throws \ReflectionException
* @api
*/
public function isMethodTaggedWith(string $className, string $methodName, string $tag): bool
{
if (!$this->initialized) {
$this->initialize();
}
$method = new MethodReflection($this->cleanClassName($className), $methodName);
$tagsValues = $method->getTagsValues();
return isset($tagsValues[$tag]);
}
/**
* Tells if a specific PHP attribute is to be ignored for reflection
*/
public function isAttributeIgnored(string $attributeName): bool
{
return $attributeName === 'ReturnTypeWillChange' && !class_exists($attributeName);
}
/**
* Tells if the specified method has the given annotation
*
* @param class-string $className
* @param string $methodName
* @param class-string $annotationClassName
* @return bool
* @throws \ReflectionException
* @api
*/
public function isMethodAnnotatedWith(string $className, string $methodName, string $annotationClassName): bool
{
return $this->getMethodAnnotations($className, $methodName, $annotationClassName) !== [];
}
/**
* Returns the specified method annotations or an empty array
*
* @param class-string $className
* @param class-string|null $annotationClassName
* @return array<object>
*
* @throws \ReflectionException
* @api
*
*/
public function getMethodAnnotations(string $className, string $methodName, string|null $annotationClassName = null): array
{
$className = $this->cleanClassName($className);
$annotationClassName = $annotationClassName === null ? null : $this->cleanClassName($annotationClassName);
$methodAnnotations = $this->methodAnnotationsRuntimeCache[$className][$methodName] ?? null;
$annotations = [];
if ($methodAnnotations === null) {
if (!$this->initialized) {
$this->initialize();
}
$method = new MethodReflection($className, $methodName);
$methodAnnotations = $this->annotationReader->getMethodAnnotations($method);
foreach ($method->getAttributes() as $attribute) {
if ($this->isAttributeIgnored($attribute->getName())) {
continue;
}
$methodAnnotations[] = $attribute->newInstance();
}
$this->methodAnnotationsRuntimeCache[$className][$methodName] = $methodAnnotations;
}
if ($annotationClassName === null) {
return $methodAnnotations;
}
foreach ($methodAnnotations as $annotation) {
if ($annotation instanceof $annotationClassName) {
$annotations[] = $annotation;
}
}
return $annotations;
}
/**
* Returns the specified method annotation or NULL.
*
* If multiple annotations are set on the target you will
* get the first instance of them.
*
* @throws \ReflectionException
*/
public function getMethodAnnotation(string $className, string $methodName, string $annotationClassName): ?object
{
if (!$this->initialized) {
$this->initialize();
}
$annotations = $this->getMethodAnnotations($className, $methodName, $annotationClassName);
return $annotations === [] ? null : reset($annotations);
}
/**
* Returns the names of all properties of the specified class
*
* @param class-string $className
* @return array<string>
* @throws ClassLoadingForReflectionFailedException
* @throws InvalidClassException
* @throws \ReflectionException
* @api
*/
public function getClassPropertyNames(string $className): array
{
$className = $this->prepareClassReflectionForUsage($className);
return isset($this->classReflectionData[$className][self::DATA_CLASS_PROPERTIES]) ? array_keys($this->classReflectionData[$className][self::DATA_CLASS_PROPERTIES]) : [];
}
/**
* Wrapper for method_exists() which tells if the given method exists.
*
* @param class-string $className
* @throws ClassLoadingForReflectionFailedException
* @throws InvalidClassException
* @throws \ReflectionException
* @api
*/
public function hasMethod(string $className, string $methodName): bool
{
$className = $this->prepareClassReflectionForUsage($className);
return isset($this->classReflectionData[$className][self::DATA_CLASS_METHODS][$methodName]);
}
/**
* Returns all tags and their values the specified method is tagged with
*
* @throws \ReflectionException
* @deprecated since 8.4
*/
public function getMethodTagsValues(string $className, string $methodName): array
{
if (!$this->initialized) {
$this->initialize();
}
$className = $this->cleanClassName($className);
return (new MethodReflection($className, $methodName))->getTagsValues();
}
/**
* Returns an array of parameters of the given method. Each entry contains
* additional information about the parameter position, type hint etc.
*
* @param class-string $className
* @return array An array of parameter names and additional information or an empty array of no parameters were found
* @throws ClassLoadingForReflectionFailedException
* @throws InvalidClassException
* @throws \ReflectionException
* @api
*/
public function getMethodParameters(string $className, string $methodName): array
{
$className = $this->prepareClassReflectionForUsage($className);
if (!isset($this->classReflectionData[$className][self::DATA_CLASS_METHODS][$methodName][self::DATA_METHOD_PARAMETERS])) {
return [];
}
return $this->convertParameterDataToArray($this->classReflectionData[$className][self::DATA_CLASS_METHODS][$methodName][self::DATA_METHOD_PARAMETERS]);
}
/**
* Returns the declared return type of a method (for PHP < 7.0 this will always return null)
*
* @param class-string $className
* @return string|null The declared return type of the method or null if none was declared
* @throws ClassLoadingForReflectionFailedException
* @throws InvalidClassException
* @throws \ReflectionException
*/
public function getMethodDeclaredReturnType(string $className, string $methodName): ?string
{
$className = $this->prepareClassReflectionForUsage($className);
return $this->classReflectionData[$className][self::DATA_CLASS_METHODS][$methodName][self::DATA_METHOD_DECLARED_RETURN_TYPE] ?? null;
}
/**
* Searches for and returns all names of class properties which are tagged by the specified tag.
* If no properties were found, an empty array is returned.
*
* @param class-string $className
* @return array<string>
* @throws ClassLoadingForReflectionFailedException
* @throws InvalidClassException
* @throws \ReflectionException
* @api
*
*/
public function getPropertyNamesByTag(string $className, string $tag): array
{
$className = $this->prepareClassReflectionForUsage($className);
if (!isset($this->classReflectionData[$className][self::DATA_CLASS_PROPERTIES])) {
return [];
}
$propertyNames = [];
foreach ($this->classReflectionData[$className][self::DATA_CLASS_PROPERTIES] as $propertyName => $propertyData) {
if (isset($propertyData[self::DATA_PROPERTY_TAGS_VALUES][$tag])) {
$propertyNames[$propertyName] = true;
}
}
return array_keys($propertyNames);
}
/**
* Returns all tags and their values the specified class property is tagged with
*
* @param class-string $className
* @param string $propertyName
* @return array
* @throws ClassLoadingForReflectionFailedException
* @throws InvalidClassException
* @throws \ReflectionException
* @api
*/
public function getPropertyTagsValues(string $className, string $propertyName): array
{
$className = $this->prepareClassReflectionForUsage($className);
if (!isset($this->classReflectionData[$className][self::DATA_CLASS_PROPERTIES][$propertyName])) {
return [];
}
return $this->classReflectionData[$className][self::DATA_CLASS_PROPERTIES][$propertyName][self::DATA_PROPERTY_TAGS_VALUES] ?? [];
}
/**
* Returns the values of the specified class property tag
*
* @param class-string $className
* @param string $propertyName
* @param string $tag
* @return array
* @throws ClassLoadingForReflectionFailedException
* @throws InvalidClassException
* @throws \ReflectionException
* @api
*
*/
public function getPropertyTagValues(string $className, string $propertyName, string $tag): array
{
$className = $this->prepareClassReflectionForUsage($className);
if (!isset($this->classReflectionData[$className][self::DATA_CLASS_PROPERTIES][$propertyName])) {
return [];
}
return $this->classReflectionData[$className][self::DATA_CLASS_PROPERTIES][$propertyName][self::DATA_PROPERTY_TAGS_VALUES][$tag] ?? [];
}
/**
* Returns the property type
*
* @param class-string $className
* @param string $propertyName
* @return string|null
* @throws ClassLoadingForReflectionFailedException
* @throws InvalidClassException
* @throws \ReflectionException
*/
public function getPropertyType(string $className, string $propertyName): ?string
{
$className = $this->prepareClassReflectionForUsage($className);
return $this->classReflectionData[$className][self::DATA_CLASS_PROPERTIES][$propertyName][self::DATA_PROPERTY_TYPE] ?? null;
}
/**
* @param class-string $className
* @param string $propertyName
* @return bool
* @throws ClassLoadingForReflectionFailedException
* @throws InvalidClassException
* @throws \ReflectionException
* @api
*/
public function isPropertyPrivate(string $className, string $propertyName): bool
{
$className = $this->prepareClassReflectionForUsage($className);
return (isset($this->classReflectionData[$className][self::DATA_CLASS_PROPERTIES][$propertyName][self::DATA_PROPERTY_VISIBILITY])
&& $this->classReflectionData[$className][self::DATA_CLASS_PROPERTIES][$propertyName][self::DATA_PROPERTY_VISIBILITY] === self::VISIBILITY_PRIVATE);
}
/**
* Tells if the specified property is promoted
*
* @api
*/
public function isPropertyPromoted(string $className, string $propertyName): bool
{
$className = $this->prepareClassReflectionForUsage($className);
return isset($this->classReflectionData[$className][self::DATA_CLASS_PROPERTIES][$propertyName][self::DATA_PROPERTY_PROMOTED]);
}
/**
* Tells if the specified class property is tagged with the given tag
*
* @param class-string $className
* @param string $propertyName
* @param string $tag
* @return bool
* @throws ClassLoadingForReflectionFailedException
* @throws InvalidClassException
* @throws \ReflectionException
* @api
*/
public function isPropertyTaggedWith(string $className, string $propertyName, string $tag): bool
{
$className = $this->prepareClassReflectionForUsage($className);
return isset($this->classReflectionData[$className][self::DATA_CLASS_PROPERTIES][$propertyName][self::DATA_PROPERTY_TAGS_VALUES][$tag]);
}
/**
* Tells if the specified property has the given annotation
*
* @param class-string $className
* @param string $propertyName
* @param class-string $annotationClassName
* @return bool
* @throws ClassLoadingForReflectionFailedException
* @throws InvalidClassException
* @throws \ReflectionException
* @api
*/
public function isPropertyAnnotatedWith(string $className, string $propertyName, string $annotationClassName): bool
{
$className = $this->prepareClassReflectionForUsage($className);
return isset($this->classReflectionData[$className][self::DATA_CLASS_PROPERTIES][$propertyName][self::DATA_PROPERTY_ANNOTATIONS][$annotationClassName]);
}
/**
* Searches for and returns all names of class properties which are marked by the
* specified annotation. If no properties were found, an empty array is returned.
*
* @param class-string $className
* @param class-string $annotationClassName
* @return array<string>
* @throws ClassLoadingForReflectionFailedException
* @throws InvalidClassException
* @throws \ReflectionException
* @api
*/
public function getPropertyNamesByAnnotation(string $className, string $annotationClassName): array
{
$className = $this->prepareClassReflectionForUsage($className);
if (!isset($this->classReflectionData[$className][self::DATA_CLASS_PROPERTIES])) {
return [];
}
$propertyNames = [];
foreach ($this->classReflectionData[$className][self::DATA_CLASS_PROPERTIES] as $propertyName => $propertyData) {
if (isset($propertyData[self::DATA_PROPERTY_ANNOTATIONS][$annotationClassName])) {
$propertyNames[$propertyName] = true;
}
}
return array_keys($propertyNames);
}
/**
* Returns the specified property annotations or an empty array
*
* @param class-string $className
* @param string $propertyName
* @param class-string|null $annotationClassName
* @return array<object>
* @throws ClassLoadingForReflectionFailedException
* @throws InvalidClassException
* @throws \ReflectionException
* @api
*/
public function getPropertyAnnotations(string $className, string $propertyName, ?string $annotationClassName = null): array
{
$className = $this->prepareClassReflectionForUsage($className);
if (!isset($this->classReflectionData[$className][self::DATA_CLASS_PROPERTIES][$propertyName][self::DATA_PROPERTY_ANNOTATIONS])) {
return [];
}
if ($annotationClassName === null) {