Completed
Pull Request — develop (#127)
by Chuck
04:03
created

ProjectFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
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 File[] $files
69
     * @throws Exception when no matching strategy was found.
70
     */
71 9
    public function create(string $name, array $files): ProjectInterface
72
    {
73 9
        $project = new Project($name);
74
75 9
        foreach ($files as $filePath) {
76 9
            $strategy = $this->strategies->findMatching($filePath);
77 8
            $file = $strategy->create($filePath, $this->strategies);
78 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...
79
        }
80
81 8
        $this->buildNamespaces($project);
82
83 8
        return $project;
84
    }
85
86
    /**
87
     * Builds the namespace tree with all elements in the project.
88
     */
89 8
    private function buildNamespaces(Project $project): void
90
    {
91 8
        foreach ($project->getFiles() as $file) {
92 8
            foreach ($file->getNamespaces() as $namespaceFqsen) {
93 7
                $namespace = $this->getNamespaceByName($project, (string) $namespaceFqsen);
94 8
                $this->buildNamespace($file, $namespace);
95
            }
96
        }
97 8
    }
98
99
    /**
100
     * Gets Namespace from the project if it exists, otherwise returns a new namepace
101
     */
102 7
    private function getNamespaceByName(Project $project, $name): Namespace_
103
    {
104 7
        $existingNamespaces = $project->getNamespaces();
105
106 7
        if (isset($existingNamespaces[$name])) {
107 1
            return $existingNamespaces[$name];
108
        }
109
110 7
        $namespace = new Namespace_(new Fqsen($name));
111 7
        $project->addNamespace($namespace);
112 7
        return $namespace;
113
    }
114
115
    /**
116
     * Adds all elements belonging to the namespace to the namespace.
117
     */
118 7
    private function buildNamespace(File $file, Namespace_ $namespace): void
119
    {
120 7
        foreach ($file->getClasses() as $class) {
121 3
            if ($namespace->getFqsen() . '\\' . $class->getName() === (string) $class->getFqsen()) {
122 3
                $namespace->addClass($class->getFqsen());
123
            }
124
        }
125
126 7
        foreach ($file->getInterfaces() as $interface) {
127 1
            if ($namespace->getFqsen() . '\\' . $interface->getName() === (string) $interface->getFqsen()) {
128 1
                $namespace->addInterface($interface->getFqsen());
129
            }
130
        }
131
132 7
        foreach ($file->getFunctions() as $function) {
133 1
            if ($namespace->getFqsen() . '\\' . $function->getName() . '()' === (string) $function->getFqsen()) {
134 1
                $namespace->addFunction($function->getFqsen());
135
            }
136
        }
137
138 7
        foreach ($file->getConstants() as $constant) {
139 1
            if ($namespace->getFqsen() . '::' . $constant->getName() === (string) $constant->getFqsen()) {
140 1
                $namespace->addConstant($constant->getFqsen());
141
            }
142
        }
143
144 7
        foreach ($file->getTraits() as $trait) {
145 1
            if ($namespace->getFqsen() . '\\' . $trait->getName() === (string) $trait->getFqsen()) {
146 1
                $namespace->addTrait($trait->getFqsen());
147
            }
148
        }
149 7
    }
150
}
151