2323 */
2424final class ParamReader implements ParamReaderInterface
2525{
26+ /**
27+ * @var Reader|null
28+ */
2629 private $ annotationReader ;
2730
28- public function __construct (Reader $ annotationReader )
31+ public function __construct (? Reader $ annotationReader = null )
2932 {
3033 $ this ->annotationReader = $ annotationReader ;
3134 }
@@ -50,7 +53,17 @@ public function read(\ReflectionClass $reflection, string $method): array
5053 */
5154 public function getParamsFromMethod (\ReflectionMethod $ method ): array
5255 {
53- $ annotations = $ this ->annotationReader ->getMethodAnnotations ($ method );
56+ $ annotations = [];
57+ if (\PHP_VERSION_ID >= 80000 ) {
58+ $ annotations = $ this ->getParamsFromAttributes ($ method );
59+ }
60+
61+ if (null !== $ this ->annotationReader ) {
62+ $ annotations = array_merge (
63+ $ annotations ,
64+ $ this ->annotationReader ->getMethodAnnotations ($ method ) ?? []
65+ );
66+ }
5467
5568 return $ this ->getParamsFromAnnotationArray ($ annotations );
5669 }
@@ -60,7 +73,17 @@ public function getParamsFromMethod(\ReflectionMethod $method): array
6073 */
6174 public function getParamsFromClass (\ReflectionClass $ class ): array
6275 {
63- $ annotations = $ this ->annotationReader ->getClassAnnotations ($ class );
76+ $ annotations = [];
77+ if (\PHP_VERSION_ID >= 80000 ) {
78+ $ annotations = $ this ->getParamsFromAttributes ($ class );
79+ }
80+
81+ if (null !== $ this ->annotationReader ) {
82+ $ annotations = array_merge (
83+ $ annotations ,
84+ $ this ->annotationReader ->getClassAnnotations ($ class ) ?? []
85+ );
86+ }
6487
6588 return $ this ->getParamsFromAnnotationArray ($ annotations );
6689 }
@@ -79,4 +102,20 @@ private function getParamsFromAnnotationArray(array $annotations): array
79102
80103 return $ params ;
81104 }
105+
106+ /**
107+ * @param \ReflectionClass|\ReflectionMethod $reflection
108+ *
109+ * @return ParamInterface[]
110+ */
111+ private function getParamsFromAttributes ($ reflection ): array
112+ {
113+ $ params = [];
114+ foreach ($ reflection ->getAttributes (ParamInterface::class, \ReflectionAttribute::IS_INSTANCEOF ) as $ attribute ) {
115+ $ param = $ attribute ->newInstance ();
116+ $ params [$ param ->getName ()] = $ param ;
117+ }
118+
119+ return $ params ;
120+ }
82121}
0 commit comments