Completed
Push — develop ( 141adb...8f70d4 )
by Jaap
05:39
created

Version2   B

Complexity

Total Complexity 48

Size/Duplication

Total Lines 341
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 86.21%

Importance

Changes 0
Metric Value
dl 0
loc 341
ccs 100
cts 116
cp 0.8621
rs 8.5599
c 0
b 0
f 0
wmc 48
lcom 1
cbo 2

17 Methods

Rating   Name   Duplication   Size   Complexity  
A convert() 0 50 2
A supports() 0 5 2
A buildArrayFromNode() 0 11 3
A buildExtensions() 0 12 3
A buildMarkers() 0 12 3
A buildIncludeSourcecode() 0 8 2
A buildVisibility() 0 12 3
A buildDefaultPackageName() 0 12 3
A buildTemplate() 0 12 3
A buildIgnoreHidden() 0 12 3
A buildIgnoreSymlinks() 0 12 3
A buildIgnorePaths() 0 18 5
A buildOutputDirectory() 0 12 3
A buildDirectories() 0 12 3
A buildSourcePaths() 0 11 2
A buildCacheDirectory() 0 12 3
A validate() 0 8 2

How to fix   Complexity   

Complex Class

Complex classes like Version2 often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Version2, and based on these observations, apply Extract Interface, too.

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