Completed
Push — develop ( 521632...eb9c4a )
by Mike
06:46
created

Version2::buildIncludeSourcecode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 8
ccs 3
cts 4
cp 0.75
crap 2.0625
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\Configuration\Factory;
17
18
use InvalidArgumentException;
19
use phpDocumentor\Dsn;
20
use phpDocumentor\Path;
21
use SimpleXMLElement;
22
23
/**
24
 * phpDocumentor2 strategy for converting the configuration xml to an array.
25
 */
26
final class Version2 implements Strategy
27
{
28
    /** @var string[] */
29
    private $extensions = ['php', 'php3', 'phtml'];
30
31
    /** @var string[] */
32
    private $markers = ['TODO', 'FIXME'];
33
34
    /** @var string[] */
35
    private $visibility = ['public', 'protected', 'private'];
36
37
    /** @var string */
38
    private $defaultPackageName = 'Default';
39
40
    /** @var string */
41
    private $template = 'clean';
42
43
    /** @var bool */
44
    private $ignoreHidden = true;
45
46
    /** @var bool */
47
    private $ignoreSymlinks = true;
48
49
    /** @var string[] */
50
    private $ignorePaths = [];
51
52
    /** @var string */
53
    private $outputDirectory = 'file://build/docs';
54
55
    /** @var string[] */
56
    private $directories = ['src'];
57
58 5
    private $includeSource = false;
59
60 5
    public function convert(SimpleXMLElement $phpDocumentor): array
61
    {
62 5
        $this->validate($phpDocumentor);
63
64
        $outputDirectory = $this->buildOutputDirectory($phpDocumentor);
65 5
66 5
        return [
67
            'phpdocumentor' => [
68
                'title' => ((string)$phpDocumentor->title) ?: 'my-doc',
69 5
                'use-cache' => true,
70 5
                'paths' => [
71
                    'output' => new Dsn($outputDirectory),
72
                    'cache' => new Path('/tmp/phpdoc-doc-cache'),
73
                ],
74 5
                'versions' => [
75
                    '1.0.0' => [
76
                        'folder' => '',
77 5
                        'api' => [
78
                            0 => [
79 5
                                'encoding' => 'utf8',
80
                                'ignore-tags' => [],
81
                                'format' => 'php',
82 5
                                'validate' => false,
83 5
                                'source' => [
84
                                    'dsn' => new Dsn('file://' . getcwd()),
85
                                    'paths' => $this->buildSourcePaths($phpDocumentor),
86 5
                                ],
87 5
                                'ignore' => [
88 5
                                    'hidden' => $this->buildIgnoreHidden($phpDocumentor),
89
                                    'symlinks' => $this->buildIgnoreSymlinks($phpDocumentor),
90 5
                                    'paths' => $this->buildIgnorePaths($phpDocumentor),
91 5
                                ],
92 5
                                'extensions' => $this->buildExtensions($phpDocumentor),
93 5
                                'visibility' => $this->buildVisibility($phpDocumentor),
94
                                'include-source' => $this->buildIncludeSourcecode($phpDocumentor),
95
                                'default-package-name' => $this->buildDefaultPackageName($phpDocumentor),
96
                                'markers' => $this->buildMarkers($phpDocumentor),
97
                            ],
98
                        ],
99
                    ],
100 5
                ],
101
                'templates' => [
102
                    [
103
                        'name' => $this->buildTemplate($phpDocumentor),
104
                    ],
105
                ],
106
            ],
107 1
        ];
108
    }
109 1
110 1
    public function supports(SimpleXMLElement $phpDocumentor): bool
111
    {
112
        return isset($phpDocumentor->attributes()->version) === false
113
            || $phpDocumentor->attributes()->version == '2';
114
    }
115
116 1
    /**
117
     * Loops over a node and fills an array with the found children.
118 1
     */
119 1
    private function buildArrayFromNode(SimpleXMLElement $node): array
120 1
    {
121 1
        $array = [];
122
        foreach ($node->children() as $child) {
123
            if ((string) $child !== '') {
124
                $array[] = (string) $child;
125 1
            }
126
        }
127
128
        return $array;
129
    }
130
131
    /**
132
     * Builds the extensions part of the array from the configuration xml.
133 5
     *
134
     * @return string[]
135 5
     */
136
    private function buildExtensions(SimpleXMLElement $phpDocumentor): array
137
    {
138
        if ((array) $phpDocumentor->parser === []) {
139 5
            return $this->extensions;
140 4
        }
141
142
        if ((array) $phpDocumentor->parser->extensions === []) {
143 1
            return $this->extensions;
144
        }
145
146
        return $this->buildArrayFromNode($phpDocumentor->parser->extensions);
147
    }
148
149
    /**
150
     * Builds the markers part of the array from the configuration xml.
151 5
     *
152
     * @return string[]
153 5
     */
154
    private function buildMarkers(SimpleXMLElement $phpDocumentor): array
155
    {
156
        if ((array) $phpDocumentor->parser === []) {
157 5
            return $this->markers;
158 4
        }
159
160
        if ((array) $phpDocumentor->parser->markers === []) {
161 1
            return $this->markers;
162
        }
163
164
        return $this->buildArrayFromNode($phpDocumentor->parser->markers);
165
    }
166
167
    /**
168
     * Builds whether the source code should be part of the output.
169 5
     */
170
    private function buildIncludeSourcecode(SimpleXMLElement $phpDocumentor): bool
171 5
    {
172
        if ((array) $phpDocumentor->parser === []) {
173
            return $this->includeSource;
174
        }
175 5
176 4
        return (bool) $phpDocumentor->parser->{'include-source'};
177
    }
178
179 1
    /**
180
     * Builds the visibility part of the array from the configuration xml.
181
     *
182
     * @return string[]
183
     */
184
    private function buildVisibility(SimpleXMLElement $phpDocumentor): array
185 5
    {
186
        if ((array) $phpDocumentor->parser === []) {
187 5
            return $this->visibility;
188
        }
189
190
        if ((string) $phpDocumentor->parser->visibility === '') {
191 5
            return $this->visibility;
192
        }
193
194
        return explode(',', (string) $phpDocumentor->parser->visibility);
195 5
    }
196
197
    /**
198
     * Builds the defaultPackageName part of the array from the configuration xml.
199
     */
200
    private function buildDefaultPackageName(SimpleXMLElement $phpDocumentor): string
201 5
    {
202
        if ((array) $phpDocumentor->parser === []) {
203 5
            return $this->defaultPackageName;
204
        }
205
206
        if ((string) $phpDocumentor->parser->{'default-package-name'} === '') {
207 5
            return $this->defaultPackageName;
208 5
        }
209
210
        return (string) $phpDocumentor->parser->{'default-package-name'};
211
    }
212
213
    /**
214
     * Builds the template part of the array from the configuration xml.
215
     */
216
    private function buildTemplate(SimpleXMLElement $phpDocumentor): string
217 5
    {
218
        if ((array) $phpDocumentor->transformations === []) {
219 5
            return $this->template;
220
        }
221
222
        if ((string) $phpDocumentor->transformations->template === '') {
223 5
            return $this->template;
224 4
        }
225
226
        return (string) $phpDocumentor->transformations->template->attributes()->name;
227 1
    }
228
229
    /**
230
     * Builds the ignore-hidden part of the array from the configuration xml.
231
     */
232
    private function buildIgnoreHidden(SimpleXMLElement $phpDocumentor): bool
233 5
    {
234
        if ((array) $phpDocumentor->files === []) {
235 5
            return $this->ignoreHidden;
236
        }
237
238
        if ((string) $phpDocumentor->files->{'ignore-hidden'} === '') {
239 5
            return $this->ignoreHidden;
240 4
        }
241
242
        return filter_var($phpDocumentor->files->{'ignore-hidden'}, FILTER_VALIDATE_BOOLEAN);
243 1
    }
244
245
    /**
246
     * Builds the ignore-symlinks part of the array from the configuration xml.
247
     */
248
    private function buildIgnoreSymlinks(SimpleXMLElement $phpDocumentor): bool
249
    {
250
        if ((array) $phpDocumentor->files === []) {
251 5
            return $this->ignoreSymlinks;
252
        }
253 5
254
        if ((string) $phpDocumentor->files->{'ignore-symlinks'} === '') {
255
            return $this->ignoreSymlinks;
256 5
        }
257 5
258 5
        return filter_var($phpDocumentor->files->{'ignore-symlinks'}, FILTER_VALIDATE_BOOLEAN);
259 5
    }
260
261
    /**
262
     * Builds the ignorePaths part of the array from the configuration xml.
263 5
     *
264 4
     * @return string[]
265
     */
266
    private function buildIgnorePaths(SimpleXMLElement $phpDocumentor): array
267 1
    {
268
        if ((array) $phpDocumentor->files === []) {
269
            return $this->ignorePaths;
270
        }
271
        $ignorePaths = [];
272
        foreach ($phpDocumentor->files->children() as $child) {
273 5
            if ($child->getName() === 'ignore') {
274
                $ignorePaths[] = (string) $child;
275 5
            }
276
        }
277
278
        if (count($ignorePaths) === 0) {
279 5
            return $this->ignorePaths;
280
        }
281
282
        return $ignorePaths;
283 5
    }
284
285
    /**
286
     * Builds the outputDirectory part of the array from the configuration xml.
287
     */
288
    private function buildOutputDirectory(SimpleXMLElement $phpDocumentor): string
289
    {
290
        if ((array) $phpDocumentor->transformer === []) {
291 5
            return $this->outputDirectory;
292
        }
293 5
294
        if ((string) $phpDocumentor->transformer->target === '') {
295
            return $this->outputDirectory;
296
        }
297 5
298
        return (string) $phpDocumentor->transformer->target;
299
    }
300
301 5
    /**
302
     * Builds the directories that are used in the sourcePaths.
303
     *
304
     * @return string[]
305
     */
306
    private function buildDirectories(SimpleXMLElement $phpDocumentor): array
307
    {
308
        if ((array) $phpDocumentor->files === []) {
309 5
            return $this->directories;
310
        }
311 5
312 5
        if ((string) $phpDocumentor->files->directory === '') {
313
            return $this->directories;
314 5
        }
315 5
316
        return (array) $phpDocumentor->files->directory;
317
    }
318 5
319
    /**
320
     * Builds the sourcePaths part of the array from the configuration xml.
321
     *
322
     * @return Path[]
323
     */
324
    private function buildSourcePaths(SimpleXMLElement $phpDocumentor): array
325
    {
326 6
        $sourcePaths = [];
327
        $directories = $this->buildDirectories($phpDocumentor);
328 6
329 1
        foreach ($directories as $directory) {
330 1
            $sourcePaths[] = (new Dsn($directory))->getPath();
331
        }
332
333 5
        return $sourcePaths;
334
    }
335
336
    /**
337
     * Validates if the xml has a root element which name is phpdocumentor.
338
     *
339
     * @throws InvalidArgumentException if the root element of the xml is not phpdocumentor.
340
     */
341
    private function validate(SimpleXMLElement $xml): void
342
    {
343
        if ($xml->getName() !== 'phpdocumentor') {
344
            throw new InvalidArgumentException(
345
                sprintf('Root element name should be phpdocumentor, %s found', $xml->getName())
346
            );
347
        }
348
    }
349
}
350