Completed
Push — develop ( 722f70...af048b )
by Jaap
15:12 queued 05:04
created

Configuration/Factory/PhpDocumentor3.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * This file is part of phpDocumentor.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @copyright 2010-2015 Mike van Riel<[email protected]>
9
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
10
 * @link      http://phpdoc.org
11
 */
12
13
namespace phpDocumentor\Application\Configuration\Factory;
14
15
use phpDocumentor\DomainModel\Dsn;
16
use phpDocumentor\DomainModel\Path;
17
18
/**
19
 * phpDocumentor3 strategy for converting the configuration xml to an array.
20
 */
21
final class PhpDocumentor3 implements Strategy
22
{
23
    /**
24
     * The path to the xsd that is used for validation of the configuration file.
25
     *
26
     * @var string
27
     */
28
    private $schemaPath;
29
30
    /**
31
     * Initializes the PhpDocumentor3 strategy.
32
     *
33
     * @param string $schemaPath
34
     */
35
    public function __construct($schemaPath)
36
    {
37
        $this->schemaPath = $schemaPath;
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public function convert(\SimpleXMLElement $phpDocumentor)
0 ignored issues
show
This operation has 12500 execution paths which exceeds the configured maximum of 200.

A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.

You can also find more information in the “Code” section of your repository.

Loading history...
44
    {
45
        $this->validate($phpDocumentor);
46
47
        $versions  = [];
48
        $templates = [];
49
50
        foreach ($phpDocumentor->children() as $child) {
51
            switch ($child->getName()) {
52
                case 'version':
53
                    $versions[(string) $child->attributes()->number] = $this->buildVersion($child);
54
                    break;
55
                case 'template':
56
                    $templates[] = $this->buildTemplate($child);
57
                    break;
58
                default:
59
                    break;
60
            }
61
        }
62
63
        $phpdoc3Array = [
64
            'phpdocumentor' => [
65
                'use-cache' => $phpDocumentor->{"use-cache"} ?: true,
66
                'paths'     => [
67
                    'output' => new Dsn(((string) $phpDocumentor->paths->output) ?: 'file://build/docs'),
68
                    'cache'  => new Path(((string) $phpDocumentor->paths->cache) ?: '/tmp/phpdoc-doc-cache'),
69
                ],
70
                'versions'  => ($versions) ?: $this->defaultVersions(),
71
                'templates' => ($templates) ?: [$this->defaultTemplate()],
72
            ],
73
        ];
74
75
        return $phpdoc3Array;
76
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81
    public function match(\SimpleXMLElement $phpDocumentor)
82
    {
83
        return (string) $phpDocumentor->attributes()->version === '3';
84
    }
85
86
    /**
87
     * Builds the versions part of the array from the configuration xml.
88
     *
89
     * @param \SimpleXMLElement $version
90
     *
91
     * @return array
92
     */
93
    private function buildVersion(\SimpleXMLElement $version)
94
    {
95
        $apis   = [];
96
        $guides = [];
97
        foreach ($version->children() as $child) {
98
            switch ($child->getName()) {
99
                case 'api':
100
                    $apis[] = $this->buildApi($child);
101
                    break;
102
                case 'guide':
103
                    $guides[] = $this->buildGuide($child);
104
                    break;
105
                default:
106
                    break;
107
            }
108
        }
109
110
        $version = [
111
            'folder' => (string) $version->folder,
112
        ];
113
114
        if (count($apis) > 0) {
115
            $version['api'] = $apis;
116
        }
117
118
        if (count($guides) > 0) {
119
            $version['guide'] = $guides;
120
        }
121
122
        return $version;
123
    }
124
125
    /**
126
     * Builds the api part of the array from the configuration xml.
127
     *
128
     * @param \SimpleXMLElement $api
129
     *
130
     * @return array
131
     */
132
    private function buildApi(\SimpleXMLElement $api)
133
    {
134
        $extensions = [];
135
        foreach ($api->extensions->children() as $extension) {
136
            if ((string) $extension !== '') {
137
                $extensions[] = (string) $extension;
138
            }
139
        }
140
141
        $ignoreHidden = filter_var($api->ignore->attributes()->hidden, FILTER_VALIDATE_BOOLEAN);
142
143
        return [
144
            'format'               => ((string) $api->attributes()->format) ?: 'php',
145
            'source'               => [
146
                'dsn'   => ((string) $api->source->attributes()->dsn) ?: 'file://.',
147
                'paths' => ((array) $api->source->path) ?: ['.'],
148
            ],
149
            'ignore'               => [
150
                'hidden' => $ignoreHidden,
151
                'paths'  => (array) $api->ignore->path,
152
            ],
153
            'extensions'           => $extensions,
154
            'visibility'           => (array) $api->visibility,
155
            'default-package-name' => ((string) $api->{'default-package-name'}) ?: 'Default',
156
            'markers'              => (array) $api->markers->children()->marker,
157
        ];
158
    }
159
160
    /**
161
     * Builds the guide part of the array from the configuration xml.
162
     *
163
     * @param \SimpleXMLElement $guide
164
     *
165
     * @return array
166
     */
167
    private function buildGuide(\SimpleXMLElement $guide)
168
    {
169
        return [
170
            'format' => ((string) $guide->attributes()->format) ?: 'rst',
171
            'source' => [
172
                'dsn'   => ((string) $guide->source->attributes()->dsn) ?: 'file://.',
173
                'paths' => ((array) $guide->source->path) ?: [''],
174
            ],
175
        ];
176
    }
177
178
    /**
179
     * Builds the template part of the array from the configuration xml.
180
     *
181
     * @param \SimpleXMLElement $template
182
     *
183
     * @return array
184
     */
185
    private function buildTemplate(\SimpleXMLElement $template)
186
    {
187
        if ((array) $template === []) {
188
            return $this->defaultTemplate();
189
        }
190
191
        $attributes = [];
192
        foreach ($template->attributes() as $attribute) {
193
            $attributes[$attribute->getName()] = (string) $attribute;
194
        }
195
196
        return $attributes;
197
    }
198
199
    /**
200
     * Default versions part if none is found in the configuration.
201
     *
202
     * @return array
203
     */
204
    private function defaultVersions()
205
    {
206
        return [
207
            '1.0.0' => [
208
                'folder' => 'latest',
209
                'api'    => [
210
                    0 => [
211
                        'format'               => 'php',
212
                        'source'               => [
213
                            'dsn'   => 'file://.',
214
                            'paths' => [
215
                                0 => 'src'
216
                            ]
217
                        ],
218
                        'ignore'               => [
219
                            'hidden' => true,
220
                            'paths'  => []
221
                        ],
222
                        'extensions'           => [
223
                            0 => 'php',
224
                            1 => 'php3',
225
                            2 => 'phtml'
226
                        ],
227
                        'visibility'           => ['public'],
228
                        'default-package-name' => 'Default',
229
                        'markers'              => [
230
                            0 => 'TODO',
231
                            1 => 'FIXME'
232
                        ]
233
                    ]
234
                ],
235
                'guide'  => [
236
                    0 => [
237
                        'format' => 'rst',
238
                        'source' => [
239
                            'dsn'   => 'file://.',
240
                            'paths' => [
241
                                0 => 'docs'
242
                            ]
243
                        ]
244
                    ]
245
                ]
246
            ]
247
        ];
248
    }
249
250
    /**
251
     * Default template part if none is found in the configuration.
252
     *
253
     * @return array
254
     */
255
    private function defaultTemplate()
256
    {
257
        return [
258
            'name' => 'clean'
259
        ];
260
    }
261
262
    /**
263
     * Validates the configuration xml structure against the schema defined in the schemaPath.
264
     *
265
     * @param \SimpleXMLElement $phpDocumentor
266
     *
267
     * @throws \InvalidArgumentException if the xml structure is not valid.
268
     */
269
    private function validate(\SimpleXMLElement $phpDocumentor)
270
    {
271
        libxml_clear_errors();
272
        $priorSetting = libxml_use_internal_errors(true);
273
274
        $dom        = new \DOMDocument();
275
        $domElement = dom_import_simplexml($phpDocumentor);
276
        $domElement = $dom->importNode($domElement, true);
277
        $dom->appendChild($domElement);
278
279
        $dom->schemaValidate($this->schemaPath);
280
281
        $error = libxml_get_last_error();
282
283
        if ($error !== false) {
284
            throw new \InvalidArgumentException(trim($error->message));
285
        }
286
287
        libxml_use_internal_errors($priorSetting);
288
    }
289
}
290