ProjectDescriptorBuilder::setMarkers()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
 * @author    Mike van Riel <[email protected]>
11
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
12
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
13
 * @link      http://phpdoc.org
14
 */
15
16
namespace phpDocumentor\Descriptor;
17
18
use phpDocumentor\Descriptor\Builder\AssemblerFactory;
19
use phpDocumentor\Descriptor\Builder\AssemblerInterface;
20
use phpDocumentor\Descriptor\Builder\Reflector\AssemblerAbstract;
21
use phpDocumentor\Descriptor\Cache\ProjectDescriptorMapper;
22
use phpDocumentor\Descriptor\Filter\Filter;
23
use phpDocumentor\Descriptor\Filter\Filterable;
24
use phpDocumentor\Descriptor\ProjectDescriptor\Settings;
25
use phpDocumentor\Reflection\Php\Project;
26
use Psr\Log\LogLevel;
27
28
/**
29
 * Builds a Project Descriptor and underlying tree.
30
 */
31
class ProjectDescriptorBuilder
32
{
33
    /** @var string */
34
    const DEFAULT_PROJECT_NAME = 'Untitled project';
35
36
    /** @var AssemblerFactory $assemblerFactory */
37
    protected $assemblerFactory;
38
39
    /** @var Filter $filter */
40
    protected $filter;
41
42
    /** @var ProjectDescriptor $project */
43
    protected $project;
44
45
    private $defaultPackage;
46
47
    public function __construct(AssemblerFactory $assemblerFactory, Filter $filterManager)
48
    {
49
        $this->assemblerFactory = $assemblerFactory;
50
        $this->filter = $filterManager;
51
    }
52
53 1
    public function createProjectDescriptor()
54
    {
55 1
        $this->project = new ProjectDescriptor(self::DEFAULT_PROJECT_NAME);
56 1
    }
57
58
    /**
59
     * Returns the project descriptor that is being built.
60
     *
61
     * @return ProjectDescriptor
62
     */
63 1
    public function getProjectDescriptor()
64
    {
65 1
        return $this->project;
66
    }
67
68
    /**
69
     * Takes the given data and attempts to build a Descriptor from it.
70
     *
71
     * @param mixed $data
72
     *
73
     * @throws \InvalidArgumentException if no Assembler could be found that matches the given data.
74
     *
75
     * @return DescriptorAbstract|Collection|null
76
     */
77
    public function buildDescriptor($data)
78
    {
79
        $assembler = $this->getAssembler($data);
80
        if (!$assembler) {
81
            throw new \InvalidArgumentException(
82
                'Unable to build a Descriptor; the provided data did not match any Assembler ' .
83
                get_class($data)
84
            );
85
        }
86
87
        if ($assembler instanceof Builder\AssemblerAbstract) {
88
            $assembler->setBuilder($this);
89
        }
90
91
        // create Descriptor and populate with the provided data
92
        $descriptor = $assembler->create($data);
93
        if (!$descriptor) {
94
            return null;
95
        }
96
97
        return (!is_array($descriptor) && (!$descriptor instanceof Collection))
98
            ? $this->filterDescriptor($descriptor)
99
            : $this->filterEachDescriptor($descriptor);
0 ignored issues
show
Documentation introduced by
$descriptor is of type array|object<phpDocumentor\Descriptor\Collection>, but the function expects a array<integer,object<php...or\DescriptorAbstract>>.

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...
100
    }
101
102
    /**
103
     * Attempts to find an assembler matching the given data.
104
     *
105
     * @param mixed $data
106
     *
107
     * @return AssemblerInterface|null
108
     */
109
    public function getAssembler($data)
110
    {
111
        return $this->assemblerFactory->get($data);
112
    }
113
114
    /**
115
     * Analyzes a Descriptor and alters its state based on its state or even removes the descriptor.
116
     *
117
     * @return Filterable
118
     */
119
    public function filter(Filterable $descriptor)
120
    {
121
        return $this->filter->filter($descriptor);
122
    }
123
124
    /**
125
     * Filters each descriptor, validates them, stores the validation results and returns a collection of transmuted
126
     * objects.
127
     *
128
     * @param DescriptorAbstract[] $descriptor
129
     *
130
     * @return Collection
131
     */
132
    private function filterEachDescriptor($descriptor)
133
    {
134
        $descriptors = new Collection();
135
        foreach ($descriptor as $key => $item) {
136
            $item = $this->filterDescriptor($item);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $item is correct as $this->filterDescriptor($item) (which targets phpDocumentor\Descriptor...der::filterDescriptor()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
137
            if (!$item) {
138
                continue;
139
            }
140
141
            $descriptors[$key] = $item;
142
        }
143
144
        return $descriptors;
145
    }
146
147
    /**
148
     * Filters a descriptor, validates it, stores the validation results and returns the transmuted object or null
149
     * if it is supposed to be removed.
150
     *
151
     * @param Descriptor $descriptor
152
     *
153
     * @return Descriptor|null
154
     */
155
    protected function filterDescriptor(Descriptor $descriptor)
156
    {
157
        if (!$descriptor instanceof Filterable) {
158
            return $descriptor;
159
        }
160
161
        // filter the descriptor; this may result in the descriptor being removed!
162
        $descriptor = $this->filter($descriptor);
163
        if (!$descriptor) {
164
            return null;
165
        }
166
167
        return $descriptor;
168
    }
169
170
    public function build(Project $project)
171
    {
172
        $packageName = $project->getRootNamespace()->getFqsen()->getName();
173
        $this->defaultPackage = new PackageDescriptor();
174
        $this->defaultPackage->setFullyQualifiedStructuralElementName(
175
            (string) $project->getRootNamespace()->getFqsen()
176
        );
177
        $this->defaultPackage->setName($packageName);
178
        $this->defaultPackage->setNamespace(
179
            substr((string) $project->getRootNamespace()->getFqsen(), 0, -strlen($packageName) - 1)
180
        );
181
182
        foreach ($project->getFiles() as $file) {
183
            $descriptor = $this->buildDescriptor($file);
184
            if (!$descriptor) {
185
                return;
186
            }
187
188
            $this->getProjectDescriptor()->getFiles()->set($descriptor->getPath(), $descriptor);
189
        }
190
191
        $namespaces = $this->getProjectDescriptor()->getIndexes()->get('namespaces', new Collection());
192
//        $namespaces->add($this->defaultPackage);
193
194
        foreach ($project->getNamespaces() as $namespace) {
195
            $namespaces->set((string) $namespace->getFqsen(), $this->buildDescriptor($namespace));
196
        }
197
    }
198
199
    public function getDefaultPackage()
200
    {
201
        return $this->defaultPackage;
202
    }
203
204
    public function setVisibility(array $apiConfig) : void
205
    {
206
        $visibilities = $apiConfig['visibility'];
207
        $visibility = null;
208
209
        foreach ($visibilities as $item) {
210
            switch ($item) {
211
                case 'public':
212
                    $visibility |= ProjectDescriptor\Settings::VISIBILITY_PUBLIC;
213
                    break;
214
                case 'protected':
215
                    $visibility |= ProjectDescriptor\Settings::VISIBILITY_PROTECTED;
216
                    break;
217
                case 'private':
218
                    $visibility |= ProjectDescriptor\Settings::VISIBILITY_PRIVATE;
219
                    break;
220
            }
221
        }
222
223
        $this->project->getSettings()->setVisibility($visibility);
224
    }
225
226
    public function setName(string $title) : void
227
    {
228
        $this->project->setName($title);
229
    }
230
231
    public function setPartials(Collection $partials) : void
232
    {
233
        $this->project->setPartials($partials);
234
    }
235
236
    public function setMarkers(array $markers) : void
237
    {
238
        $this->project->getSettings()->setMarkers($markers);
239
    }
240
241
    public function setIncludeSource(bool $includeSources) : void
242
    {
243
        if ($includeSources) {
244
            $this->project->getSettings()->includeSource();
245
        } else {
246
            $this->project->getSettings()->excludeSource();
247
        }
248
    }
249
}
250