Completed
Push — develop ( a89061...54507c )
by Jaap
08:53
created

Compiler/Pass/NamespaceTreeBuilder.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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