Completed
Push — develop ( 373768...b82813 )
by Mike
05:50
created

Version2::buildIgnoreSymlinks()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

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