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 (#707)
by David
17:20
created

TreeGenerator::addToTreeByReflection()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 17
rs 9.4285
cc 3
eloc 11
nc 3
nop 1
1
<?php
2
3
/**
4
 * This file is part of the ApiGen (http://apigen.org)
5
 *
6
 * For the full copyright and license information, please view
7
 * the file LICENSE that was distributed with this source code.
8
 */
9
10
namespace ApiGen\Generator\TemplateGenerators;
11
12
use ApiGen\Configuration\Configuration;
13
use ApiGen\Configuration\ConfigurationOptions as CO;
14
use ApiGen\Contracts\Generator\TemplateGenerators\ConditionalTemplateGeneratorInterface;
15
use ApiGen\Contracts\Parser\Elements\ElementsInterface;
16
use ApiGen\Contracts\Parser\ParserStorageInterface;
17
use ApiGen\Contracts\Parser\Reflection\ClassReflectionInterface;
18
use ApiGen\Contracts\Templating\TemplateFactory\TemplateFactoryInterface;
19
use ApiGen\Tree;
20
21
class TreeGenerator implements ConditionalTemplateGeneratorInterface
22
{
23
24
    /**
25
     * @var Configuration
26
     */
27
    private $configuration;
28
29
    /**
30
     * @var TemplateFactoryInterface
31
     */
32
    private $templateFactory;
33
34
    /**
35
     * @var array
36
     */
37
    private $processed = [];
38
39
    /**
40
     * @var array[]
41
     */
42
    private $treeStorage = [
43
        ElementsInterface::CLASSES => [],
44
        ElementsInterface::INTERFACES => [],
45
        ElementsInterface::TRAITS => [],
46
        ElementsInterface::EXCEPTIONS => []
47
    ];
48
49
    /**
50
     * @var ParserStorageInterface
51
     */
52
    private $parserStorage;
53
54
55
    public function __construct(
56
        Configuration $configuration,
57
        TemplateFactoryInterface $templateFactory,
58
        ParserStorageInterface $parserStorage
59
    ) {
60
        $this->configuration = $configuration;
61
        $this->templateFactory = $templateFactory;
62
        $this->parserStorage = $parserStorage;
63
    }
64
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function generate()
70
    {
71
        $template = $this->templateFactory->createForType('tree');
0 ignored issues
show
Bug introduced by
The method createForType() does not exist on ApiGen\Contracts\Templat...emplateFactoryInterface. Did you maybe mean create()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
72
73
        $classes = $this->parserStorage->getClasses();
74
        foreach ($classes as $className => $reflection) {
75
            if ($this->canBeProcessed($reflection)) {
76
                $this->addToTreeByReflection($reflection);
77
            }
78
        }
79
80
        $this->sortTreeStorageElements();
81
82
        $template->setParameters([
83
            'classTree' => new Tree($this->treeStorage[ElementsInterface::CLASSES], $classes),
84
            'interfaceTree' => new Tree($this->treeStorage[ElementsInterface::INTERFACES], $classes),
85
            'traitTree' => new Tree($this->treeStorage[ElementsInterface::TRAITS], $classes),
86
            'exceptionTree' => new Tree($this->treeStorage[ElementsInterface::EXCEPTIONS], $classes)
87
        ]);
88
89
        $template->save();
90
    }
91
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function isAllowed()
97
    {
98
        return $this->configuration->getOption(CO::TREE);
99
    }
100
101
102
    /**
103
     * @return bool
104
     */
105
    private function canBeProcessed(ClassReflectionInterface $reflection)
106
    {
107
        if (! $reflection->isMain()) {
108
            return false;
109
        }
110
        if (! $reflection->isDocumented()) {
111
            return false;
112
        }
113
        if (isset($this->processed[$reflection->getName()])) {
114
            return false;
115
        }
116
        return true;
117
    }
118
119
120
    private function addToTreeByReflection(ClassReflectionInterface $reflection)
121
    {
122
        $line = array_values(array_reverse($reflection->getParentClasses()));
123
        $line[] = $reflection;
124
        $type = $this->getTypeByReflection($line[0]);
125
        $cursor = & $this->treeStorage[$type];
126
127
        foreach ($line as $class) {
128
            /** @var ReflectionClass $class */
129
            $name = $class->getName();
130
            $cursor = & $cursor[$name];
131
            if (! $cursor) {
132
                $cursor = [];
133
                $this->processed[$name] = true;
134
            }
135
        }
136
    }
137
138
139
    /**
140
     * @return string
141
     */
142
    private function getTypeByReflection(ClassReflectionInterface $reflection)
143
    {
144
        if ($reflection->isInterface()) {
145
            return ElementsInterface::INTERFACES;
146
147
        } elseif ($reflection->isTrait()) {
148
            return ElementsInterface::TRAITS;
149
150
        } elseif ($reflection->isException()) {
151
            return ElementsInterface::EXCEPTIONS;
152
153
        } else {
154
            return ElementsInterface::CLASSES;
155
        }
156
    }
157
158
159
    private function sortTreeStorageElements()
160
    {
161
        foreach ($this->treeStorage as $key => $elements) {
162
            ksort($elements, SORT_STRING);
163
            $this->treeStorage[$key] = $elements;
164
        }
165
    }
166
}
167