Completed
Push — develop ( 5e5d58...9b9aa6 )
by Mike
04:21
created

ProjectFactory::getNamespaceByName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
ccs 7
cts 7
cp 1
crap 2
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
 * @copyright 2010-2018 Mike van Riel<[email protected]>
11
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
12
 * @link      http://phpdoc.org
13
 */
14
15
namespace phpDocumentor\Reflection\Php;
16
17
use phpDocumentor\Reflection\DocBlockFactory;
18
use phpDocumentor\Reflection\Exception;
19
use phpDocumentor\Reflection\Fqsen;
20
use phpDocumentor\Reflection\PrettyPrinter;
21
use phpDocumentor\Reflection\Project as ProjectInterface;
22
use phpDocumentor\Reflection\ProjectFactory as ProjectFactoryInterface;
23
24
/**
25
 * Factory class to transform files into a project description.
26
 */
27
final class ProjectFactory implements ProjectFactoryInterface
28
{
29
    /**
30
     * @var ProjectFactoryStrategies
31
     */
32
    private $strategies;
33
34
    /**
35
     * Initializes the factory with a number of strategies.
36
     *
37
     * @param ProjectFactoryStrategy[] $strategies
38
     */
39 1
    public function __construct(array $strategies)
40
    {
41 1
        $this->strategies = new ProjectFactoryStrategies($strategies);
42 1
    }
43
44
    /**
45
     * Creates a new instance of this factory. With all default strategies.
46
     */
47
    public static function createInstance(): self
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 File[] $files
70
     * @throws Exception when no matching strategy was found.
71
     */
72 8
    public function create($name, array $files): ProjectInterface
73
    {
74 8
        $project = new Project($name);
75
76 8
        foreach ($files as $filePath) {
77
            try {
78 8
                $strategy = $this->strategies->findMatching($filePath);
79 8
                $file = $strategy->create($filePath, $this->strategies);
80 8
                $project->addFile($file);
0 ignored issues
show
Documentation introduced by
$file is of type object<phpDocumentor\Reflection\Element>, but the function expects a object<phpDocumentor\Reflection\Php\File>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
81 8
            } catch (\Exception $exception) {
82
                // TODO: Add logging
83
                // For now; silently ignore when there is an issue with a file so that
84
                // parsing continues for other files and this is not a showstopper.
85
                // The class responsible for failing also emits a log, so it should 
86
                // not go unnoticed
87
            }
88
        }
89
90 8
        $this->buildNamespaces($project);
91
92 8
        return $project;
93
    }
94
95
    /**
96
     * Builds the namespace tree with all elements in the project.
97
     */
98 8
    private function buildNamespaces(Project $project): void
99
    {
100 8
        foreach ($project->getFiles() as $file) {
101 8
            foreach ($file->getNamespaces() as $namespaceFqsen) {
102 7
                $namespace = $this->getNamespaceByName($project, (string) $namespaceFqsen);
103 8
                $this->buildNamespace($file, $namespace);
104
            }
105
        }
106 8
    }
107
108
    /**
109
     * Gets Namespace from the project if it exists, otherwise returns a new namepace
110
     */
111 7
    private function getNamespaceByName(Project $project, $name): Namespace_
112
    {
113 7
        $existingNamespaces = $project->getNamespaces();
114
115 7
        if (isset($existingNamespaces[$name])) {
116 1
            return $existingNamespaces[$name];
117
        }
118
119 7
        $namespace = new Namespace_(new Fqsen($name));
120 7
        $project->addNamespace($namespace);
121 7
        return $namespace;
122
    }
123
124
    /**
125
     * Adds all elements belonging to the namespace to the namespace.
126
     */
127 7
    private function buildNamespace(File $file, Namespace_ $namespace): void
128
    {
129 7
        foreach ($file->getClasses() as $class) {
130 3
            if ($namespace->getFqsen() . '\\' . $class->getName() === (string) $class->getFqsen()) {
131 3
                $namespace->addClass($class->getFqsen());
132
            }
133
        }
134
135 7
        foreach ($file->getInterfaces() as $interface) {
136 1
            if ($namespace->getFqsen() . '\\' . $interface->getName() === (string) $interface->getFqsen()) {
137 1
                $namespace->addInterface($interface->getFqsen());
138
            }
139
        }
140
141 7
        foreach ($file->getFunctions() as $function) {
142 1
            if ($namespace->getFqsen() . '\\' . $function->getName() . '()' === (string) $function->getFqsen()) {
143 1
                $namespace->addFunction($function->getFqsen());
144
            }
145
        }
146
147 7
        foreach ($file->getConstants() as $constant) {
148 1
            if ($namespace->getFqsen() . '::' . $constant->getName() === (string) $constant->getFqsen()) {
149 1
                $namespace->addConstant($constant->getFqsen());
150
            }
151
        }
152
153 7
        foreach ($file->getTraits() as $trait) {
154 1
            if ($namespace->getFqsen() . '\\' . $trait->getName() === (string) $trait->getFqsen()) {
155 1
                $namespace->addTrait($trait->getFqsen());
156
            }
157
        }
158 7
    }
159
}
160