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
Push — 4.2-to-master ( ed215f )
by E
06:47
created

ElementStorage::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
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
    public function __construct(ParserStorageInterface $parserStorage, NamespaceSorterInterface $groupSorter)
66
    {
67
        $this->parserStorage = $parserStorage;
68
        $this->groupSorter = $groupSorter;
69
    }
70
71
    /**
72
     * @return mixed[]
73
     */
74
    public function getNamespaces(): array
75
    {
76
        $this->ensureCategorization();
77
        return $this->namespaces;
78
    }
79
80
    /**
81
     * @return ClassReflectionInterface[]
82
     */
83
    public function getClasses(): array
84
    {
85
        $this->ensureCategorization();
86
        return $this->classes;
87
    }
88
89
    /**
90
     * @return ClassReflectionInterface[]
91
     */
92
    public function getInterfaces(): array
93
    {
94
        $this->ensureCategorization();
95
        return $this->interfaces;
96
    }
97
98
    /**
99
     * @return ClassReflectionInterface[]
100
     */
101
    public function getTraits(): array
102
    {
103
        $this->ensureCategorization();
104
        return $this->traits;
105
    }
106
107
    /**
108
     * @return ClassReflectionInterface[]
109
     */
110
    public function getExceptions(): array
111
    {
112
        $this->ensureCategorization();
113
        return $this->exceptions;
114
    }
115
116
    /**
117
     * @return ConstantReflectionInterface[]
118
     */
119
    public function getConstants(): array
120
    {
121
        $this->ensureCategorization();
122
        return $this->constants;
123
    }
124
125
    /**
126
     * @return FunctionReflectionInterface[]
127
     */
128
    public function getFunctions(): array
129
    {
130
         $this->ensureCategorization();
131
         return $this->functions;
132
    }
133
134
    /**
135
     * @return ClassReflectionInterface[]
136
     */
137
    public function getClassElements(): array
138
    {
139
        return array_merge($this->getClasses(), $this->getTraits(), $this->getInterfaces(), $this->getExceptions());
140
    }
141
142
    /**
143
     * @return mixed[]
144
     */
145
    public function getElements(): array
146
    {
147
        $this->ensureCategorization();
148
149
        $elements = [
150
            Elements::CLASSES => $this->classes,
151
            Elements::CONSTANTS => $this->constants,
152
            Elements::FUNCTIONS => $this->functions,
153
            Elements::INTERFACES => $this->interfaces,
154
            Elements::TRAITS => $this->traits,
155
            Elements::EXCEPTIONS => $this->exceptions
156
        ];
157
        return $elements;
158
    }
159
160
    private function categorizeParsedElements(): void
161
    {
162
        foreach ($this->parserStorage->getTypes() as $type) {
163
            $elements = $this->parserStorage->getElementsByType($type);
164
            foreach ($elements as $elementName => $element) {
165
                if (! $element->isDocumented()) {
166
                    continue;
167
                }
168
169
                if ($element instanceof ConstantReflectionInterface) {
170
                    $elementType = Elements::CONSTANTS;
171
                    $this->constants[$elementName] = $element;
172
                } elseif ($element instanceof FunctionReflectionInterface) {
173
                    $elementType = Elements::FUNCTIONS;
174
                    $this->functions[$elementName] = $element;
175
                } elseif ($element->isInterface()) {
176
                    $elementType = Elements::INTERFACES;
177
                    $this->interfaces[$elementName] = $element;
178
                } elseif ($element->isTrait()) {
179
                    $elementType = Elements::TRAITS;
180
                    $this->traits[$elementName] = $element;
181
                } elseif ($element->isException()) {
182
                    $elementType = Elements::EXCEPTIONS;
183
                    $this->exceptions[$elementName] = $element;
184
                } else {
185
                    $elementType = Elements::CLASSES;
186
                    $this->classes[$elementName] = $element;
187
                }
188
189
                $this->categorizeElementToNamespace($elementType, $element);
190
            }
191
        }
192
193
        $this->sortNamespaces();
194
        $this->areElementsCategorized = true;
195
    }
196
197
    private function categorizeElementToNamespace(string $elementType, ElementReflectionInterface $element): void
198
    {
199
        $namespaceName = $element->getPseudoNamespaceName();
200
201
        $this->namespaces[$namespaceName][$elementType][$element->getShortName()] = $element;
202
    }
203
204
    private function sortNamespaces(): void
205
    {
206
        $this->namespaces = $this->groupSorter->sort($this->namespaces);
207
    }
208
209
    private function ensureCategorization(): void
210
    {
211
        if ($this->areElementsCategorized === false) {
212
            $this->categorizeParsedElements();
213
        }
214
    }
215
}
216