Completed
Push — develop ( 8eb671...133594 )
by Mike
19:30 queued 09:24
created

Compiler/Pass/NamespaceTreeBuilder.php (2 issues)

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
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
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);
0 ignored issues
show
Unused Code Comprehensibility introduced by Jaapio
80% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
142
        }
143 6
    }
144
}
145