Completed
Push — master ( acc46e...6bc416 )
by David
17s
created

AnnotationUtils   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 14
dl 0
loc 32
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getClassAnnotations() 0 8 2
A getClassAnnotation() 0 10 3
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
}