GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#837)
by E
07:31 queued 05:03
created

ElementStorage::getConstants()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace ApiGen\Parser\Elements;
4
5
use ApiGen\Contracts\Parser\Elements\ElementStorageInterface;
6
use ApiGen\Contracts\Parser\Elements\NamespaceSorterInterface;
7
use ApiGen\Contracts\Parser\ParserStorageInterface;
8
use ApiGen\Contracts\Parser\Reflection\ClassReflectionInterface;
9
use ApiGen\Contracts\Parser\Reflection\ConstantReflectionInterface;
10
use ApiGen\Contracts\Parser\Reflection\ElementReflectionInterface;
11
use ApiGen\Contracts\Parser\Reflection\FunctionReflectionInterface;
12
13
final class ElementStorage implements ElementStorageInterface
14
{
15
    /**
16
     * @var mixed[]
17
     */
18
    private $namespaces = [];
19
20
    /**
21
     * @var ClassReflectionInterface[]
22
     */
23
    private $classes = [];
24
25
    /**
26
     * @var ClassReflectionInterface[]
27
     */
28
    private $interfaces = [];
29
30
    /**
31
     * @var ClassReflectionInterface[]
32
     */
33
    private $traits = [];
34
35
    /**
36
     * @var ClassReflectionInterface[]
37
     */
38
    private $exceptions = [];
39
40
    /**
41
     * @var ConstantReflectionInterface[]
42
     */
43
    private $constants = [];
44
45
    /**
46
     * @var FunctionReflectionInterface[]
47
     */
48
    private $functions = [];
49
50
    /**
51
     * @var bool
52
     */
53
    private $areElementsCategorized = false;
54
55
    /**
56
     * @var ParserStorageInterface
57
     */
58
    private $parserStorage;
59
60
    /**
61
     * @var NamespaceSorterInterface
62
     */
63
    private $groupSorter;
64
65 34
    public function __construct(ParserStorageInterface $parserStorage, NamespaceSorterInterface $groupSorter)
66
    {
67 34
        $this->parserStorage = $parserStorage;
68 34
        $this->groupSorter = $groupSorter;
69 34
    }
70
71
    /**
72
     * @return mixed[]
73
     */
74 17
    public function getNamespaces(): array
75
    {
76 17
        $this->ensureCategorization();
77 17
        return $this->namespaces;
78
    }
79
80
    /**
81
     * @return ClassReflectionInterface[]
82
     */
83 16
    public function getClasses(): array
84
    {
85 16
        $this->ensureCategorization();
86 16
        return $this->classes;
87
    }
88
89
    /**
90
     * @return ClassReflectionInterface[]
91
     */
92 16
    public function getInterfaces(): array
93
    {
94 16
        $this->ensureCategorization();
95 16
        return $this->interfaces;
96
    }
97
98
    /**
99
     * @return ClassReflectionInterface[]
100
     */
101 16
    public function getTraits(): array
102
    {
103 16
        $this->ensureCategorization();
104 16
        return $this->traits;
105
    }
106
107
    /**
108
     * @return ClassReflectionInterface[]
109
     */
110 16
    public function getExceptions(): array
111
    {
112 16
        $this->ensureCategorization();
113 16
        return $this->exceptions;
114
    }
115
116
    /**
117
     * @return ConstantReflectionInterface[]
118
     */
119 16
    public function getConstants(): array
120
    {
121 16
        $this->ensureCategorization();
122 16
        return $this->constants;
123
    }
124
125
    /**
126
     * @return FunctionReflectionInterface[]
127
     */
128 16
    public function getFunctions(): array
129
    {
130 16
         $this->ensureCategorization();
131 16
         return $this->functions;
132
    }
133
134
    /**
135
     * @return ClassReflectionInterface[]
136
     */
137 2
    public function getClassElements(): array
138
    {
139 2
        return array_merge($this->getClasses(), $this->getTraits(), $this->getInterfaces(), $this->getExceptions());
140
    }
141
142
    /**
143
     * @return mixed[]
144
     */
145 16
    public function getElements(): array
146
    {
147 16
        $this->ensureCategorization();
148
149
        $elements = [
150 16
            Elements::CLASSES => $this->classes,
151 16
            Elements::CONSTANTS => $this->constants,
152 16
            Elements::FUNCTIONS => $this->functions,
153 16
            Elements::INTERFACES => $this->interfaces,
154 16
            Elements::TRAITS => $this->traits,
155 16
            Elements::EXCEPTIONS => $this->exceptions
156
        ];
157 16
        return $elements;
158
    }
159
160 20
    private function categorizeParsedElements(): void
161
    {
162 20
        foreach ($this->parserStorage->getTypes() as $type) {
163 20
            $elements = $this->parserStorage->getElementsByType($type);
164 20
            foreach ($elements as $elementName => $element) {
165 9
                if (! $element->isDocumented()) {
166 1
                    continue;
167
                }
168
169 9
                if ($element instanceof ConstantReflectionInterface) {
170 1
                    $elementType = Elements::CONSTANTS;
171 1
                    $this->constants[$elementName] = $element;
172
                } elseif ($element instanceof FunctionReflectionInterface) {
173 1
                    $elementType = Elements::FUNCTIONS;
174 1
                    $this->functions[$elementName] = $element;
175 9
                } elseif ($element->isInterface()) {
176 1
                    $elementType = Elements::INTERFACES;
177 1
                    $this->interfaces[$elementName] = $element;
178 9
                } elseif ($element->isTrait()) {
179 1
                    $elementType = Elements::TRAITS;
180 1
                    $this->traits[$elementName] = $element;
181 9
                } elseif ($element->isException()) {
182 1
                    $elementType = Elements::EXCEPTIONS;
183 1
                    $this->exceptions[$elementName] = $element;
184
                } else {
185 9
                    $elementType = Elements::CLASSES;
186 9
                    $this->classes[$elementName] = $element;
187
                }
188
189 20
                $this->categorizeElementToNamespace($elementType, $element);
190
            }
191
        }
192
193 20
        $this->sortNamespaces();
194 20
        $this->areElementsCategorized = true;
195 20
    }
196
197 9
    private function categorizeElementToNamespace(string $elementType, ElementReflectionInterface $element): void
198
    {
199 9
        $namespaceName = $element->getPseudoNamespaceName();
200
201 9
        $this->namespaces[$namespaceName][$elementType][$element->getShortName()] = $element;
202 9
    }
203
204 20
    private function sortNamespaces(): void
205
    {
206 20
        $this->namespaces = $this->groupSorter->sort($this->namespaces);
207 20
    }
208
209 20
    private function ensureCategorization(): void
210
    {
211 20
        if ($this->areElementsCategorized === false) {
212 20
            $this->categorizeParsedElements();
213
        }
214 20
    }
215
}
216