Passed
Push — master ( f4a983...209d17 )
by Adrien
02:29
created

Utils::getTypeName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQL\Doctrine;
6
7
use Doctrine\Common\Annotations\Reader;
8
use GraphQL\Type\Definition\EnumType;
9
use GraphQL\Type\Definition\LeafType;
10
use GraphQL\Type\Definition\ScalarType;
11
use ReflectionClass;
12
13
/**
14
 * A few utils
15
 */
16
abstract class Utils
17
{
18
    /**
19
     * Get the GraphQL type name for an output type from the PHP class
20
     *
21
     * @param string $className
22
     *
23
     * @return string
24
     */
25 90
    public static function getTypeName(string $className): string
26
    {
27 90
        $parts = explode('\\', $className);
28
29 90
        return end($parts);
30
    }
31
32
    /**
33
     * Get the GraphQL type name for a Filter type from the PHP class
34
     *
35
     * @param string $className
36
     * @param EnumType|ScalarType $type
37
     *
38
     * @return string
39
     */
40 50
    public static function getOperatorTypeName(string $className, LeafType $type): string
41
    {
42 50
        return preg_replace('~Type$~', '', self::getTypeName($className)) . ucfirst($type->name);
43
    }
44
45
    /**
46
     * Return an array of all annotations found in the class hierarchy, including its traits, indexed by the class name
47
     *
48
     * @param Reader $reader
49
     * @param ReflectionClass $class
50
     * @param string $annotationName
51
     *
52
     * @return array annotations indexed by the class name where they were found
53
     */
54 18
    public static function getRecursiveClassAnnotations(Reader $reader, ReflectionClass $class, string $annotationName): array
55
    {
56 18
        $result = [];
57
58 18
        $annotation = $reader->getClassAnnotation($class, $annotationName);
59 18
        if ($annotation) {
60 18
            $result[$class->getName()] = $annotation;
61
        }
62
63 18
        foreach ($class->getTraits() as $trait) {
64 2
            $result = array_merge($result, self::getRecursiveClassAnnotations($reader, $trait, $annotationName));
65
        }
66
67 18
        $parent = $class->getParentClass();
68 18
        if ($parent) {
1 ignored issue
show
introduced by
$parent is of type ReflectionClass, thus it always evaluated to true.
Loading history...
69 16
            $result = array_merge($result, self::getRecursiveClassAnnotations($reader, $parent, $annotationName));
70
        }
71
72 18
        return $result;
73
    }
74
}
75