Completed
Pull Request — master (#36)
by David
01:36
created

AnnotationUtils::getClassAnnotations()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 1
nop 2
1
<?php
2
3
4
namespace TheCodingMachine\GraphQL\Controllers;
5
6
7
use Doctrine\Common\Annotations\Reader;
8
use ReflectionClass;
9
10
class AnnotationUtils
11
{
12
    /**
13
     * Returns a class annotation. Finds in the parents if not found in the main class.
14
     *
15
     * @return object|null
16
     */
17
    public static function getClassAnnotation(Reader $reader, ReflectionClass $refClass, string $annotationClass)
18
    {
19
        do {
20
            $type = $reader->getClassAnnotation($refClass, $annotationClass);
21
            if ($type !== null) {
22
                return $type;
23
            }
24
            $refClass = $refClass->getParentClass();
25
        } while ($refClass);
26
        return null;
27
    }
28
29
    /**
30
     * Returns the class annotations. Finds in the parents too.
31
     *
32
     * @return object[]
33
     */
34
    public static function getClassAnnotations(Reader $reader, ReflectionClass $refClass): array
35
    {
36
        $annotations = [];
37
        do {
38
            $annotations = array_merge($reader->getClassAnnotations($refClass), $annotations);
39
            $refClass = $refClass->getParentClass();
40
        } while ($refClass);
41
        return $annotations;
42
    }
43
}