Completed
Pull Request — develop (#89)
by Jaap
02:41
created

ProjectFactory::buildNamespaces()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6667
cc 3
eloc 5
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 * This file is part of phpDocumentor.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @copyright 2010-2015 Mike van Riel<[email protected]>
9
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
10
 * @link      http://phpdoc.org
11
 */
12
13
namespace phpDocumentor\Reflection\Php;
14
15
use phpDocumentor\Reflection\DocBlockFactory;
16
use phpDocumentor\Reflection\Exception;
17
use phpDocumentor\Reflection\Fqsen;
18
use phpDocumentor\Reflection\PrettyPrinter;
19
use phpDocumentor\Reflection\ProjectFactory as ProjectFactoryInterface;
20
use phpDocumentor\Reflection\Php\Factory as Factory;
21
22
/**
23
 * Factory class to transform files into a project description.
24
 */
25
final class ProjectFactory implements ProjectFactoryInterface
0 ignored issues
show
Complexity introduced by
The class ProjectFactory has a coupling between objects value of 22. Consider to reduce the number of dependencies under 13.
Loading history...
26
{
27
    /**
28
     * @var ProjectFactoryStrategies[]
29
     */
30
    private $strategies;
31
32
    /**
33
     * Initializes the factory with a number of strategies.
34
     *
35
     * @param ProjectFactoryStrategy[] $strategies
36
     */
37 1
    public function __construct($strategies)
38
    {
39 1
        $this->strategies = new ProjectFactoryStrategies($strategies);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \phpDocumentor\Refle...Strategies($strategies) of type object<phpDocumentor\Ref...ojectFactoryStrategies> is incompatible with the declared type array<integer,object<php...jectFactoryStrategies>> of property $strategies.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
40 1
    }
41
42
    /**
43
     * Creates a new instance of this factory. With all default strategies.
44
     *
45
     * @return static;
0 ignored issues
show
Documentation introduced by
The doc-type static; could not be parsed: Expected "|" or "end of type", but got ";" at position 6. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
46
     */
47
    public static function createInstance()
48
    {
49
       return new static(
0 ignored issues
show
Coding Style introduced by
It seems like the identation of this line is off (expected at least 8 spaces, but found 7).
Loading history...
50
            [
51
                new Factory\Argument(new PrettyPrinter()),
52
                new Factory\Class_(),
53
                new Factory\Constant(new PrettyPrinter()),
54
                new Factory\DocBlock(DocBlockFactory::createInstance()),
55
                new Factory\File(NodesFactory::createInstance()),
56
                new Factory\Function_(),
57
                new Factory\Interface_(),
58
                new Factory\Method(),
59
                new Factory\Property(new PrettyPrinter()),
60
                new Factory\Trait_(),
61
            ]
62
        );
63
    }
64
65
    /**
66
     * Creates a project from the set of files.
67
     *
68
     * @param string $name
69
     * @param string[] $files
70
     * @return Project
71
     * @throws Exception when no matching strategy was found.
72
     */
73 9
    public function create($name, array $files)
74
    {
75 9
        $project = new Project($name);
76
77 9
        foreach ($files as $filePath) {
78 9
            $strategy = $this->strategies->findMatching($filePath);
0 ignored issues
show
Bug introduced by
The method findMatching cannot be called on $this->strategies (of type array<integer,object<php...jectFactoryStrategies>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
79 8
            $project->addFile($strategy->create($filePath, $this->strategies));
80
        }
81
82 8
        $this->buildNamespaces($project);
83
84 8
        return $project;
85
    }
86
87
    /**
88
     * Builds the namespace tree with all elements in the project.
89
     *
90
     * @param Project $project
91
     */
92 8
    private function buildNamespaces(Project $project)
93
    {
94 8
        foreach ($project->getFiles() as $file) {
95 8
            foreach ($file->getNamespaces() as $namespaceFqsen) {
96 7
                $namespace = $this->getNamespaceByName($project, (string)$namespaceFqsen);
97 8
                $this->buildNamespace($file, $namespace);
98
            }
99
        }
100 8
    }
101
102
    /**
103
     * Gets Namespace from the project if it exists, otherwise returns a new namepace
104
     *
105
     * @param Project $project
106
     * @param $name
107
     * @return Namespace_
108
     */
109 7
    private function getNamespaceByName(Project $project, $name)
110
    {
111 7
        $existingNamespaces = $project->getNamespaces();
112
113 7
        if (isset($existingNamespaces[$name])) {
114 1
            return $existingNamespaces[$name];
115
        }
116
117 7
        $namespace = new Namespace_(new Fqsen($name));
118 7
        $project->addNamespace($namespace);
119 7
        return $namespace;
120
    }
121
122
    /**
123
     * Adds all elements belonging to the namespace to the namespace.
124
     *
125
     * @param File $file
126
     * @param Namespace_ $namespace
127
     */
128 7
    private function buildNamespace(File $file, Namespace_ $namespace)
0 ignored issues
show
Complexity introduced by
This operation has 243 execution paths which exceeds the configured maximum of 200.

A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.

You can also find more information in the “Code” section of your repository.

Loading history...
129
    {
130 7
        foreach ($file->getClasses() as $class) {
131 3
            if ($namespace->getFqsen() . '\\' . $class->getName() == $class->getFqsen()) {
132 3
                $namespace->addClass($class->getFqsen());
133
            }
134
        }
135
136 7
        foreach ($file->getInterfaces() as $interface) {
137 1
            if ($namespace->getFqsen() . '\\' . $interface->getName() == $interface->getFqsen()) {
138 1
                $namespace->addInterface($interface->getFqsen());
139
            }
140
        }
141
142 7
        foreach ($file->getFunctions() as $function) {
143 1
            if ($namespace->getFqsen() . '\\' . $function->getName() . '()' == $function->getFqsen()) {
144 1
                $namespace->addFunction($function->getFqsen());
145
            }
146
        }
147
148 7
        foreach ($file->getConstants() as $constant) {
149 1
            if ($namespace->getFqsen() . '::' . $constant->getName() == $constant->getFqsen()) {
150 1
                $namespace->addConstant($constant->getFqsen());
151
            }
152
        }
153
154 7
        foreach ($file->getTraits() as $trait) {
155 1
            if ($namespace->getFqsen() . '\\' . $trait->getName() == $trait->getFqsen()) {
156 1
                $namespace->addTrait($trait->getFqsen());
157
            }
158
        }
159 7
    }
160
}
161