Completed
Push — develop ( 5c2bf4...eb0c60 )
by Mike
03:22
created

ProjectFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
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\File as SourceFile;
20
use phpDocumentor\Reflection\Fqsen;
21
use phpDocumentor\Reflection\PrettyPrinter;
22
use phpDocumentor\Reflection\Project as ProjectInterface;
23
use phpDocumentor\Reflection\ProjectFactory as ProjectFactoryInterface;
24
25
/**
26
 * Factory class to transform files into a project description.
27
 */
28
final class ProjectFactory implements ProjectFactoryInterface
29
{
30
    /**
31
     * @var ProjectFactoryStrategies
32
     */
33
    private $strategies;
34
35
    /**
36
     * Initializes the factory with a number of strategies.
37
     *
38
     * @param ProjectFactoryStrategy[] $strategies
39
     */
40 1
    public function __construct(array $strategies)
41
    {
42 1
        $this->strategies = new ProjectFactoryStrategies($strategies);
43 1
    }
44
45
    /**
46
     * Creates a new instance of this factory. With all default strategies.
47
     */
48
    public static function createInstance(): self
49
    {
50
        return new static(
51
            [
52
                new Factory\Argument(new PrettyPrinter()),
53
                new Factory\Class_(),
54
                new Factory\GlobalConstant(new PrettyPrinter()),
55
                new Factory\ClassConstant(new PrettyPrinter()),
56
                new Factory\DocBlock(DocBlockFactory::createInstance()),
57
                new Factory\File(NodesFactory::createInstance()),
58
                new Factory\Function_(),
59
                new Factory\Interface_(),
60
                new Factory\Method(),
61
                new Factory\Property(new PrettyPrinter()),
62
                new Factory\Trait_(),
63
            ]
64
        );
65
    }
66
67
    /**
68
     * Creates a project from the set of files.
69
     *
70
     * @param string $name
71
     * @param SourceFile[] $files
72
     * @throws Exception when no matching strategy was found.
73
     */
74 10
    public function create($name, array $files): ProjectInterface
75
    {
76 10
        $project = new Project($name);
77
78 10
        foreach ($files as $filePath) {
79 10
            $strategy = $this->strategies->findMatching($filePath);
80 9
            $file = $strategy->create($filePath, $this->strategies);
81 9
            if ($file !== null) {
82 9
                $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...
83
            }
84
        }
85
86 9
        $this->buildNamespaces($project);
87
88 9
        return $project;
89
    }
90
91
    /**
92
     * Builds the namespace tree with all elements in the project.
93
     */
94 9
    private function buildNamespaces(Project $project): void
95
    {
96 9
        foreach ($project->getFiles() as $file) {
97 9
            foreach ($file->getNamespaces() as $namespaceFqsen) {
98 7
                $namespace = $this->getNamespaceByName($project, (string) $namespaceFqsen);
99 9
                $this->buildNamespace($file, $namespace);
100
            }
101
        }
102 9
    }
103
104
    /**
105
     * Gets Namespace from the project if it exists, otherwise returns a new namepace
106
     */
107 7
    private function getNamespaceByName(Project $project, $name): Namespace_
108
    {
109 7
        $existingNamespaces = $project->getNamespaces();
110
111 7
        if (isset($existingNamespaces[$name])) {
112 1
            return $existingNamespaces[$name];
113
        }
114
115 7
        $namespace = new Namespace_(new Fqsen($name));
116 7
        $project->addNamespace($namespace);
117 7
        return $namespace;
118
    }
119
120
    /**
121
     * Adds all elements belonging to the namespace to the namespace.
122
     */
123 7
    private function buildNamespace(File $file, Namespace_ $namespace): void
124
    {
125 7
        foreach ($file->getClasses() as $class) {
126 3
            if ($namespace->getFqsen() . '\\' . $class->getName() === (string) $class->getFqsen()) {
127 3
                $namespace->addClass($class->getFqsen());
128
            }
129
        }
130
131 7
        foreach ($file->getInterfaces() as $interface) {
132 1
            if ($namespace->getFqsen() . '\\' . $interface->getName() === (string) $interface->getFqsen()) {
133 1
                $namespace->addInterface($interface->getFqsen());
134
            }
135
        }
136
137 7
        foreach ($file->getFunctions() as $function) {
138 1
            if ($namespace->getFqsen() . '\\' . $function->getName() . '()' === (string) $function->getFqsen()) {
139 1
                $namespace->addFunction($function->getFqsen());
140
            }
141
        }
142
143 7
        foreach ($file->getConstants() as $constant) {
144 1
            if ($namespace->getFqsen() . '::' . $constant->getName() === (string) $constant->getFqsen()) {
145 1
                $namespace->addConstant($constant->getFqsen());
146
            }
147
        }
148
149 7
        foreach ($file->getTraits() as $trait) {
150 1
            if ($namespace->getFqsen() . '\\' . $trait->getName() === (string) $trait->getFqsen()) {
151 1
                $namespace->addTrait($trait->getFqsen());
152
            }
153
        }
154 7
    }
155
}
156