Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Pull Request — master (#710)
by Vincent
24:34
created

GraphClass::getAnnotationReader()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 7
c 3
b 0
f 0
dl 0
loc 13
rs 10
cc 4
nc 3
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Overblog\GraphQLBundle\Config\Parser\Annotation;
6
7
use Doctrine\Common\Annotations\AnnotationException;
8
use Doctrine\Common\Annotations\AnnotationReader;
9
use Doctrine\Common\Annotations\AnnotationRegistry;
10
use ReflectionClass;
11
use ReflectionException;
12
use ReflectionMethod;
13
use ReflectionProperty;
14
use RuntimeException;
15
use function class_exists;
16
17
class GraphClass extends ReflectionClass
18
{
19
    private static ?AnnotationReader $annotationReader = null;
20
21
    protected array $annotations = [];
22
23
    protected array $propertiesExtended = [];
24
25
    public function __construct(string $className)
26
    {
27
        parent::__construct($className);
28
29
        $annotationReader = self::getAnnotationReader();
30
        $this->annotations = $annotationReader->getClassAnnotations($this);
31
32
        $reflection = $this;
33
        do {
34
            foreach ($reflection->getProperties() as $property) {
35
                if (isset($this->propertiesExtended[$property->getName()])) {
36
                    continue;
37
                }
38
                $this->propertiesExtended[$property->getName()] = $property;
39
            }
40
        } while ($reflection = $reflection->getParentClass());
41
    }
42
43
    /**
44
     * Get an array of parent class names.
45
     *
46
     * @return string[]
47
     */
48
    public function getParents(): array
49
    {
50
        $parents = [];
51
        $class = $this;
52
        while ($parent = $class->getParentClass()) {
53
            $parents[] = $parent->getName();
54
            $class = $parent;
55
        }
56
57
        return $parents;
58
    }
59
60
    /**
61
     * Get the list of methods name.
62
     *
63
     * @return string[]
64
     */
65
    public function getMethodsNames(): array
66
    {
67
        return array_map(fn (ReflectionMethod $method) => $method->getName(), $this->getMethods());
68
    }
69
70
    public function getMethodAnnotations(string $name): array
71
    {
72
        return self::getAnnotationReader()->getMethodAnnotations($this->getMethod($name));
73
    }
74
75
    public function getPropertyAnnotations(string $name): array
76
    {
77
        return self::getAnnotationReader()->getPropertyAnnotations($this->getProperty($name));
78
    }
79
80
    /**
81
     * @return ReflectionProperty[]
82
     */
83
    public function getPropertiesExtended()
84
    {
85
        return $this->propertiesExtended;
86
    }
87
88
    public function getPropertyExtended(string $name): ReflectionProperty
89
    {
90
        if (!isset($this->propertiesExtended[$name])) {
91
            throw new ReflectionException(sprintf('Missing property %s on class or parent class %s', $name, $this->getName()));
92
        }
93
94
        return $this->propertiesExtended[$name];
95
    }
96
97
    /**
98
     * @param ReflectionMethod|ReflectionProperty|null $from
99
     *
100
     * @return array
101
     */
102
    public function getAnnotations(object $from = null)
103
    {
104
        if (!$from) {
105
            return $this->annotations;
106
        }
107
108
        if ($from instanceof ReflectionMethod) {
109
            return self::getAnnotationReader()->getMethodAnnotations($from);
110
        }
111
112
        if ($from instanceof ReflectionProperty) {
113
            return self::getAnnotationReader()->getPropertyAnnotations($from);
114
        }
115
116
        throw new AnnotationException(sprintf('Unable to retrieve annotations from object of class "%s".', get_class($from)));
117
    }
118
119
    private static function getAnnotationReader(): AnnotationReader
120
    {
121
        if (null === self::$annotationReader) {
122
            if (!class_exists(AnnotationReader::class) ||
123
                !class_exists(AnnotationRegistry::class)) {
124
                throw new RuntimeException('In order to use graphql annotation, you need to require doctrine annotations');
125
            }
126
127
            AnnotationRegistry::registerLoader('class_exists');
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\Common\Annotati...istry::registerLoader() has been deprecated: This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0. ( Ignorable by Annotation )

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

127
            /** @scrutinizer ignore-deprecated */ AnnotationRegistry::registerLoader('class_exists');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
128
            self::$annotationReader = new AnnotationReader();
129
        }
130
131
        return self::$annotationReader;
132
    }
133
}
134