Passed
Push — develop ( 51016a...a9d0b3 )
by Mathieu
02:23
created

AbstractReader::getMethodAnnotation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Neimheadh\SonataAnnotationBundle\Reader;
4
5
use Doctrine\Common\Annotations\Reader;
6
use Neimheadh\SonataAnnotationBundle\Annotation\AnnotationInterface;
7
use Neimheadh\SonataAnnotationBundle\Annotation\AssociationFieldInterface;
8
use Neimheadh\SonataAnnotationBundle\Annotation\RouteAnnotationInterface;
9
use Neimheadh\SonataAnnotationBundle\Exception\MissingAnnotationArgumentException;
10
use ReflectionClass;
11
use ReflectionMethod;
12
use ReflectionProperty;
13
14
/**
15
 * Annotation reader.
16
 */
17
abstract class AbstractReader
18
{
19
20
    /**
21
     * Doctrine annotation reader.
22
     *
23
     * @var Reader
24
     */
25
    protected Reader $annotationReader;
26
27
    /**
28
     * @param Reader $annotationReader Doctrine annotation reader.
29
     */
30
    public function __construct(Reader $annotationReader)
31
    {
32
        $this->annotationReader = $annotationReader;
33
    }
34
35
    /**
36
     * Check the given annotation is well-defined.
37
     *
38
     * @param AnnotationInterface $annotation The annotation.
39
     * @param ReflectionClass     $class      Class having the annotation.
40
     *
41
     * @return void
42
     */
43
    protected function checkAnnotationIntegrity(
44
        AnnotationInterface $annotation,
45
        ReflectionClass $class
46
    ): void {
47
        if ($annotation instanceof AssociationFieldInterface
48
            && !isset($annotation->field)) {
49
            throw new MissingAnnotationArgumentException(
50
                $annotation,
51
                'field',
52
            );
53
        }
54
55
        if ($annotation instanceof RouteAnnotationInterface
56
            && !isset($annotation->name)) {
57
            throw new MissingAnnotationArgumentException(
58
                $annotation,
59
                'name',
60
                $class
61
            );
62
        }
63
    }
64
65
    /**
66
     * Get the field name for the given annotation.
67
     *
68
     * @param string $baseName   Annotation base name.
69
     * @param object $annotation Annotation object.
70
     *
71
     * @return string
72
     */
73
    protected function getAnnotationFieldName(
74
        string $baseName,
75
        object $annotation
76
    ): string {
77
        if ($annotation instanceof AssociationFieldInterface) {
78
            return sprintf('%s.%s', $baseName, $annotation->field);
79
        }
80
81
        return $baseName;
82
    }
83
84
    /**
85
     * Get a specific annotation for the given entity class.
86
     *
87
     * @param ReflectionClass $class      Entity class.
88
     * @param string          $annotation Annotation class.
89
     *
90
     * @return object|AnnotationInterface|null
91
     */
92
    protected function getClassAnnotation(
93
        ReflectionClass $class,
94
        string $annotation
95
    ): ?object {
96
        return $this->annotationReader->getClassAnnotation($class, $annotation);
97
    }
98
99
    /**
100
     * Get the list of annotations for a given entity class.
101
     *
102
     * @param ReflectionClass $class              Entity class.
103
     * @param ?string         $annotationClass    If given, filter annotation
104
     *                                            having the specified class.
105
     *
106
     * @return array<object|AnnotationInterface>
107
     */
108
    protected function getClassAnnotations(
109
        ReflectionClass $class,
110
        ?string $annotationClass = null
111
    ): array {
112
        $annotations = $annotationClass === null
113
            ? $this->annotationReader->getClassAnnotations($class)
114
            : array_filter(
115
                $this->annotationReader->getClassAnnotations($class),
116
                fn(
117
                    object $annotation
118
                ) => $annotation instanceof $annotationClass
119
            );
120
121
        array_walk(
122
            $annotations,
123
            function (object $annotation) use ($class) {
124
                $annotation instanceof AnnotationInterface
125
                && $this->checkAnnotationIntegrity(
126
                    $annotation,
127
                    $class
128
                );
129
            }
130
        );
131
132
        return $annotations;
133
    }
134
135
    /**
136
     * Get class methods annotations.
137
     *
138
     * @param ReflectionClass $class           Entity class.
139
     * @param ?string         $annotationClass Annotation class.
140
     *
141
     * @return array<string, object|AnnotationInterface[]>
142
     */
143
    protected function getClassMethodsAnnotations(
144
        ReflectionClass $class,
145
        ?string $annotationClass = null
146
    ): array {
147
        $annotations = [];
148
149
        if ($annotationClass !== null) {
150
            foreach ($class->getMethods() as $method) {
151
                $annotations[$method->getName()] = $this->getMethodAnnotations(
152
                    $method,
153
                    $annotationClass
154
                );
155
            }
156
        }
157
158
        return $annotations;
159
    }
160
161
    /**
162
     * Get class properties annotations.
163
     *
164
     * @param ReflectionClass $class           Entity class.
165
     * @param ?string         $annotationClass Annotation class.
166
     *
167
     * @return array<string, object|AnnotationInterface[]>
168
     */
169
    protected function getClassPropertiesAnnotations(
170
        ReflectionClass $class,
171
        ?string $annotationClass = null
172
    ): array {
173
        $annotations = [];
174
175
        foreach ($class->getProperties() as $property) {
176
            $annotations[$property->getName()] = $this->getPropertyAnnotations(
177
                $property,
178
                $annotationClass
179
            );
180
        }
181
182
        return $annotations;
183
    }
184
185
    /**
186
     * Get the list of annotations for a given method.
187
     *
188
     * @param ReflectionMethod $method          Method.
189
     * @param string|null      $annotationClass Filter annotation having the
190
     *                                          specified class.
191
     *
192
     * @return array<object|AnnotationInterface>
193
     */
194
    protected function getMethodAnnotations(
195
        ReflectionMethod $method,
196
        string $annotationClass = null
197
    ): array {
198
        $annotations = $annotationClass === null
199
            ? $this->annotationReader->getMethodAnnotations($method)
200
            : array_filter(
201
                $this->annotationReader->getMethodAnnotations($method),
202
                fn(
203
                    object $annotation
204
                ) => $annotation instanceof $annotationClass
205
            );
206
207
        array_walk(
208
            $annotations,
209
            function (object $annotation) use ($method) {
210
                $annotation instanceof AnnotationInterface
211
                && $this->checkAnnotationIntegrity(
212
                    $annotation,
213
                    $method->getDeclaringClass()
214
                );
215
            }
216
        );
217
218
        return $annotations;
219
    }
220
221
    /**
222
     * Get the list of annotations for a given property.
223
     *
224
     * @param ReflectionProperty $property        Property.
225
     * @param ?string            $annotationClass If given, filter annotation
226
     *                                            having the specified class.
227
     *
228
     * @return array<object|AnnotationInterface>
229
     */
230
    protected function getPropertyAnnotations(
231
        ReflectionProperty $property,
232
        ?string $annotationClass = null
233
    ): array {
234
        $annotations = $annotationClass === null
235
            ? $this->annotationReader->getPropertyAnnotations($property)
236
            : array_filter(
237
                $this->annotationReader->getPropertyAnnotations($property),
238
                fn(
239
                    object $annotation
240
                ) => $annotation instanceof $annotationClass
241
            );
242
243
        array_walk(
244
            $annotations,
245
            function (object $annotation) use ($property) {
246
                $annotation instanceof AnnotationInterface
247
                && $this->checkAnnotationIntegrity(
248
                    $annotation,
249
                    $property->getDeclaringClass()
250
                );
251
            }
252
        );
253
254
        return $annotations;
255
    }
256
257
}