addElementsOfTypeToNamespace()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 4.0015

Importance

Changes 0
Metric Value
cc 4
nc 5
nop 3
dl 0
loc 36
rs 9.344
c 0
b 0
f 0
ccs 21
cts 22
cp 0.9545
crap 4.0015
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of phpDocumentor.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author    Mike van Riel <[email protected]>
11
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
12
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
13
 * @link      http://phpdoc.org
14
 */
15
16
namespace phpDocumentor\Compiler\Pass;
17
18
use InvalidArgumentException;
19
use phpDocumentor\Compiler\CompilerPassInterface;
20
use phpDocumentor\Descriptor\Collection;
21
use phpDocumentor\Descriptor\DescriptorAbstract;
22
use phpDocumentor\Descriptor\NamespaceDescriptor;
23
use phpDocumentor\Descriptor\ProjectDescriptor;
24
use phpDocumentor\Reflection\Fqsen;
25
26
/**
27
 * Rebuilds the namespace tree from the elements found in files.
28
 *
29
 * On every compiler pass is the namespace tree rebuild to aid in the process
30
 * of incremental updates. The Files Collection in the Project Descriptor is the
31
 * only location where aliases to elements may be serialized.
32
 *
33
 * If the namespace tree were to be persisted then both locations needed to be
34
 * invalidated if a file were to change.
35
 */
36
class NamespaceTreeBuilder implements CompilerPassInterface
37
{
38
    const COMPILER_PRIORITY = 9000;
39
40 1
    public function getDescription(): string
41
    {
42 1
        return 'Build "namespaces" index and add namespaces to "elements"';
43
    }
44
45 6
    public function execute(ProjectDescriptor $project): void
46
    {
47 6
        $project->getIndexes()->get('elements', new Collection())->set('~\\', $project->getNamespace());
48 6
        $project->getIndexes()->get('namespaces', new Collection())->set('\\', $project->getNamespace());
49
50 6
        foreach ($project->getFiles() as $file) {
51 6
            $this->addElementsOfTypeToNamespace($project, $file->getConstants()->getAll(), 'constants');
52 6
            $this->addElementsOfTypeToNamespace($project, $file->getFunctions()->getAll(), 'functions');
53 6
            $this->addElementsOfTypeToNamespace($project, $file->getClasses()->getAll(), 'classes');
54 6
            $this->addElementsOfTypeToNamespace($project, $file->getInterfaces()->getAll(), 'interfaces');
55 6
            $this->addElementsOfTypeToNamespace($project, $file->getTraits()->getAll(), 'traits');
56
        }
57
58
        /** @var NamespaceDescriptor $namespace */
59 6
        foreach ($project->getIndexes()->get('namespaces')->getAll() as $namespace) {
60 6
            if ($namespace->getNamespace() !== '') {
61 6
                $this->addToParentNamespace($project, $namespace);
62
            }
63
        }
64 6
    }
65
66
    /**
67
     * Adds the given elements of a specific type to their respective Namespace Descriptors.
68
     *
69
     * This method will assign the given elements to the namespace as registered in the namespace field of that
70
     * element. If a namespace does not exist yet it will automatically be created.
71
     *
72
     * @param DescriptorAbstract[] $elements Series of elements to add to their respective namespace.
73
     * @param string $type Declares which field of the namespace will be populated with the given series of elements.
74
     *                     This name will be transformed to a getter which must exist. Out of performance
75
     *                     considerations will no effort be done to verify whether the provided type is valid.
76
     */
77 6
    protected function addElementsOfTypeToNamespace(ProjectDescriptor $project, array $elements, string $type): void
78
    {
79
        /** @var DescriptorAbstract $element */
80 6
        foreach ($elements as $element) {
81 6
            $namespaceName = (string) $element->getNamespace();
82
            //TODO: find out why this can happen. Some bug in the assembler?
83 6
            if ($namespaceName === '') {
84
                $namespaceName = '\\';
85
            }
86
87 6
            $namespace = $project->getIndexes()->get('namespaces', new Collection())->get($namespaceName);
88
89 6
            if ($namespace === null) {
90 6
                $namespace = new NamespaceDescriptor();
91 6
                $fqsen = new Fqsen($namespaceName);
92 6
                $namespace->setName($fqsen->getName());
93 6
                $namespace->setFullyQualifiedStructuralElementName($fqsen);
94 6
                $namespaceName = substr((string) $fqsen, 0, -strlen($fqsen->getName()) - 1);
95 6
                $namespace->setNamespace($namespaceName);
96 6
                $project->getIndexes()
97 6
                    ->get('namespaces')
98 6
                    ->set((string) $namespace->getFullyQualifiedStructuralElementName(), $namespace);
99 6
                $this->addToParentNamespace($project, $namespace);
100
            }
101
102
            // replace textual representation with an object representation
103 6
            $element->setNamespace($namespace);
104
105
            // add element to namespace
106 6
            $getter = 'get' . ucfirst($type);
107
108
            /** @var Collection $collection */
109 6
            $collection = $namespace->{$getter}();
110 6
            $collection->add($element);
111
        }
112 6
    }
113
114 6
    private function addToParentNamespace(ProjectDescriptor $project, NamespaceDescriptor $namespace): void
115
    {
116
        /** @var NamespaceDescriptor $parent */
117 6
        $parent = $project->getIndexes()->get('namespaces')->get($namespace->getNamespace());
118 6
        $project->getIndexes()->get('elements')->set(
119 6
            '~' . (string) $namespace->getFullyQualifiedStructuralElementName(),
120 6
            $namespace
121
        );
122
123
        try {
124 6
            if ($parent === null) {
125 6
                $parent = new NamespaceDescriptor();
126 6
                $fqsen = new Fqsen($namespace->getNamespace());
0 ignored issues
show
Bug introduced by
It seems like $namespace->getNamespace() targeting phpDocumentor\Descriptor...bstract::getNamespace() can also be of type object<phpDocumentor\Des...or\NamespaceDescriptor>; however, phpDocumentor\Reflection\Fqsen::__construct() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
127 6
                $parent->setFullyQualifiedStructuralElementName($fqsen);
128 6
                $parent->setName($fqsen->getName());
129 6
                $namespaceName = substr((string) $fqsen, 0, -strlen($parent->getName()) - 1);
130 6
                $parent->setNamespace($namespaceName === '' ? '\\' : $namespaceName);
131 6
                $project->getIndexes()
132 6
                    ->get('namespaces')
133 6
                    ->set((string) $parent->getFullyQualifiedStructuralElementName(), $parent);
134 6
                $this->addToParentNamespace($project, $parent);
135
            }
136
137 6
            $namespace->setParent($parent);
138 6
            $parent->getChildren()->set($namespace->getName(), $namespace);
139
        } catch (InvalidArgumentException $e) {
140
            //bit hacky but it works for now.
141
            //$project->getNamespace()->getChildren()->add($namespace);
142
        }
143 6
    }
144
}
145