Completed
Push — develop ( 28aa0b...3fae3c )
by Jaap
13s
created

ProjectFactory::getNamespaceByName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
ccs 7
cts 7
cp 1
crap 2
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 23. 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
    public function __construct($strategies)
38
    {
39
        $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
    }
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(
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 \phpDocumentor\Reflection\File[] $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
            $file = $strategy->create($filePath, $this->strategies);
80 8
            if ($file !== null) {
81 8
                $project->addFile($file);
82
            }
83
        }
84
85 8
        $this->buildNamespaces($project);
86
87 8
        return $project;
88
    }
89
90
    /**
91
     * Builds the namespace tree with all elements in the project.
92
     *
93
     * @param Project $project
94
     */
95 8
    private function buildNamespaces(Project $project)
96
    {
97 8
        foreach ($project->getFiles() as $file) {
98 8
            foreach ($file->getNamespaces() as $namespaceFqsen) {
99 7
                $namespace = $this->getNamespaceByName($project, (string)$namespaceFqsen);
100 8
                $this->buildNamespace($file, $namespace);
101
            }
102
        }
103 8
    }
104
105
    /**
106
     * Gets Namespace from the project if it exists, otherwise returns a new namepace
107
     *
108
     * @param Project $project
109
     * @param $name
110
     * @return Namespace_
111
     */
112 7
    private function getNamespaceByName(Project $project, $name)
113
    {
114 7
        $existingNamespaces = $project->getNamespaces();
115
116 7
        if (isset($existingNamespaces[$name])) {
117 1
            return $existingNamespaces[$name];
118
        }
119
120 7
        $namespace = new Namespace_(new Fqsen($name));
121 7
        $project->addNamespace($namespace);
122 7
        return $namespace;
123
    }
124
125
    /**
126
     * Adds all elements belonging to the namespace to the namespace.
127
     *
128
     * @param File $file
129
     * @param Namespace_ $namespace
130
     */
131 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...
132
    {
133 7
        foreach ($file->getClasses() as $class) {
134 3
            if ($namespace->getFqsen() . '\\' . $class->getName() == $class->getFqsen()) {
135 3
                $namespace->addClass($class->getFqsen());
136
            }
137
        }
138
139 7
        foreach ($file->getInterfaces() as $interface) {
140 1
            if ($namespace->getFqsen() . '\\' . $interface->getName() == $interface->getFqsen()) {
141 1
                $namespace->addInterface($interface->getFqsen());
142
            }
143
        }
144
145 7
        foreach ($file->getFunctions() as $function) {
146 1
            if ($namespace->getFqsen() . '\\' . $function->getName() . '()' == $function->getFqsen()) {
147 1
                $namespace->addFunction($function->getFqsen());
148
            }
149
        }
150
151 7
        foreach ($file->getConstants() as $constant) {
152 1
            if ($namespace->getFqsen() . '::' . $constant->getName() == $constant->getFqsen()) {
153 1
                $namespace->addConstant($constant->getFqsen());
154
            }
155
        }
156
157 7
        foreach ($file->getTraits() as $trait) {
158 1
            if ($namespace->getFqsen() . '\\' . $trait->getName() == $trait->getFqsen()) {
159 1
                $namespace->addTrait($trait->getFqsen());
160
            }
161
        }
162 7
    }
163
}
164