Passed
Push — master ( ca13db...21415a )
by Adrien
04:36
created

Reader::getAttributeInstances()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 17
rs 9.9666
c 1
b 0
f 0
eloc 9
ccs 9
cts 9
cp 1
cc 3
nc 3
nop 2
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQL\Doctrine\Attribute\Reader;
6
7
use GraphQL\Doctrine\Attribute\ApiAttribute;
8
use GraphQL\Doctrine\Exception;
9
use ReflectionClass;
10
use ReflectionMethod;
11
use ReflectionParameter;
12
use ReflectionProperty;
13
14
/**
15
 * API attribute reader.
16
 */
17
final class Reader
18
{
19
    /**
20
     * Return an array of all attributes found in the class hierarchy, including its traits, indexed by the class name.
21
     *
22
     * @template T of ApiAttribute
23
     *
24
     * @param class-string<T> $attributeName
25
     *
26
     * @return array<class-string, T[]> attributes indexed by the class name where they were found
1 ignored issue
show
Documentation Bug introduced by
The doc comment array<class-string, T[]> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string, T[]>.
Loading history...
27
     */
28 28
    public function getRecursiveClassAttributes(ReflectionClass $class, string $attributeName): array
29
    {
30 28
        $result = [];
31
32 28
        $attributes = $this->getAttributeInstances($class, $attributeName);
33 28
        if ($attributes) {
34 28
            $result[$class->getName()] = $attributes;
35
        }
36
37 28
        foreach ($class->getTraits() as $trait) {
38 2
            $result = array_merge($result, self::getRecursiveClassAttributes($trait, $attributeName));
0 ignored issues
show
Bug Best Practice introduced by
The method GraphQL\Doctrine\Attribu...ursiveClassAttributes() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
            $result = array_merge($result, self::/** @scrutinizer ignore-call */ getRecursiveClassAttributes($trait, $attributeName));
Loading history...
39
        }
40
41 28
        $parent = $class->getParentClass();
42 28
        if ($parent) {
43 26
            $result = array_merge($result, self::getRecursiveClassAttributes($parent, $attributeName));
44
        }
45
46 28
        return $result;
47
    }
48
49
    /**
50
     * @template T of ApiAttribute
51
     *
52
     * @param class-string<T> $attributeName
53
     *
54
     * @return null|T
55
     */
56 43
    public function getAttribute(ReflectionClass|ReflectionProperty|ReflectionMethod|ReflectionParameter $element, string $attributeName): ?ApiAttribute
57
    {
58 43
        $attributes = $this->getAttributeInstances($element, $attributeName);
59
60 41
        return reset($attributes) ?: null;
61
    }
62
63
    /**
64
     * @template T of ApiAttribute
65
     *
66
     * @param class-string<T> $attributeName
67
     *
68
     * @return T[]
69
     */
70 54
    private function getAttributeInstances(ReflectionClass|ReflectionMethod|ReflectionParameter|ReflectionProperty $element, string $attributeName): array
71
    {
72 54
        if (!is_subclass_of($attributeName, ApiAttribute::class)) {
73 1
            throw new Exception(self::class . ' cannot be used for attribute than are not part of `ecodev/graphql-doctrine`.');
74
        }
75
76 53
        $attributes = $element->getAttributes($attributeName);
77 53
        $instances = [];
78
79 53
        foreach ($attributes as $attribute) {
80 48
            $instance = $attribute->newInstance();
81
            assert($instance instanceof ApiAttribute);
82
83 47
            $instances[] = $instance;
84
        }
85
86 52
        return $instances;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $instances returns an array which contains values of type GraphQL\Doctrine\Attribute\ApiAttribute which are incompatible with the documented value type GraphQL\Doctrine\Attribute\Reader\T.
Loading history...
87
    }
88
}
89