setDirectoriesInPath()   B
last analyzed

Complexity

Conditions 7
Paths 13

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
nc 13
nop 1
dl 0
loc 35
ccs 18
cts 18
cp 1
crap 7
rs 8.4266
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;
17
18
use phpDocumentor\Configuration\Factory\Version3;
19
use phpDocumentor\Dsn;
20
use phpDocumentor\Path;
21
22
final class CommandlineOptionsMiddleware
23
{
24
    /** @var array */
25
    private $options = [];
26
27 16
    public function __construct(array $options = [])
28
    {
29 16
        $this->options = $options;
30 16
    }
31
32 14
    public function __invoke(array $configuration): array
33
    {
34 14
        $configuration = $this->overwriteDestinationFolder($configuration);
35 14
        $configuration = $this->disableCache($configuration);
36 14
        $configuration = $this->overwriteCacheFolder($configuration);
37 14
        $configuration = $this->overwriteTitle($configuration);
38 14
        $configuration = $this->overwriteTemplates($configuration);
39
40 14
        if (!isset($configuration['phpdocumentor']['versions'])) {
41 5
            return $configuration;
42
        }
43
44 9
        foreach ($configuration['phpdocumentor']['versions'] as &$version) {
45 9
            $version = $this->setFilesInPath($version);
46 9
            $version = $this->setDirectoriesInPath($version);
47 9
            $version = $this->registerExtensions($version);
48 9
            $version = $this->overwriteIgnoredPaths($version);
49 9
            $version = $this->overwriteMarkers($version);
50 9
            $version = $this->overwriteIncludeSource($version);
51 9
            $version = $this->overwriteVisibility($version);
52 9
            $version = $this->overwriteDefaultPackageName($version);
53
        }
54
55 9
        return $configuration;
56
    }
57
58 16
    private function overwriteDestinationFolder(array $configuration): array
59
    {
60 16
        if (isset($this->options['target']) && $this->options['target']) {
61 1
            $configuration['phpdocumentor']['paths']['output'] = new Dsn($this->options['target']);
62
        }
63
64 16
        return $configuration;
65
    }
66
67
    /**
68
     * Changes the given configuration array so that the cache handling is disabled.
69
     */
70 16
    private function disableCache(array $configuration): array
71
    {
72 16
        if (isset($this->options['force']) && $this->options['force']) {
73 2
            $configuration['phpdocumentor']['use-cache'] = false;
74
        }
75
76 16
        return $configuration;
77
    }
78
79 16
    private function overwriteCacheFolder(array $configuration): array
80
    {
81 16
        if (isset($this->options['cache-folder']) && $this->options['cache-folder']) {
82 1
            $configuration['phpdocumentor']['paths']['cache'] = new Path($this->options['cache-folder']);
83
        }
84
85 16
        return $configuration;
86
    }
87
88 16
    private function overwriteTitle(array $configuration): array
89
    {
90 16
        if (isset($this->options['title']) && $this->options['title']) {
91 1
            $configuration['phpdocumentor']['title'] = $this->options['title'];
92
        }
93
94 16
        return $configuration;
95
    }
96
97
    /**
98
     * Changes the given configuration array to feature the templates from the options.
99
     */
100 16
    private function overwriteTemplates(array $configuration): array
101
    {
102 16
        if (isset($this->options['template']) && $this->options['template']) {
103 1
            $configuration['phpdocumentor']['templates'] = array_map(
104 1
                function ($templateName) {
105 1
                    return ['name' => $templateName];
106 1
                },
107 1
                (array)$this->options['template']
108
            );
109
        }
110
111 16
        return $configuration;
112
    }
113
114 11
    private function setFilesInPath(array $version): array
115
    {
116 11
        if (! isset($this->options['filename']) || ! $this->options['filename']) {
117 10
            return $version;
118
        }
119
120 1
        if (! isset($version['api'])) {
121 1
            $version['api'] = $this->createDefaultApiSettings();
122
        }
123
124 1
        $version['api'][0]['source']['paths'] = array_map(
125 1
            function ($path) {
126 1
                return new Path($path);
127 1
            },
128 1
            $this->options['filename']
129
        );
130
131 1
        return $version;
132
    }
133
134 11
    private function setDirectoriesInPath(array $version): array
135
    {
136 11
        if (! isset($this->options['directory']) || ! $this->options['directory']) {
137 8
            return $version;
138
        }
139
140 3
        $currentApiConfig = current($this->createDefaultApiSettings());
141
142 3
        if (isset($version['api'])) {
143 2
            $currentApiConfig = current($version['api']);
144
        }
145
146
        //Reset the current config, because directory it overwriting the config.
147 3
        $currentApiConfig['source']['paths'] = [];
148 3
        $version['api'] = [];
149
150 3
        foreach ($this->options['directory'] as $path) {
151
            //If the passed directory is an absolute path this should be handled as a new Api
152
            //A version may contain multiple APIs.
153 3
            if (Path::isAbsolutePath($path)) {
154 2
                $apiConfig = $currentApiConfig;
155 2
                $apiConfig['source']['dsn'] = new Dsn($path);
156 2
                $apiConfig['source']['paths'] = [ new Path('./')];
157 2
                $version['api'][] = $apiConfig;
158
            } else {
159 3
                $currentApiConfig['source']['paths'][] = new Path($path);
160
            }
161
        }
162
163 3
        if (count($currentApiConfig['source']['paths']) > 0) {
164 2
            $version['api'][] = $currentApiConfig;
165
        }
166
167 3
        return $version;
168
    }
169
170 11
    private function registerExtensions(array $version): array
171
    {
172 11
        if (!isset($this->options['extensions']) || ! $this->options['extensions']) {
173 10
            return $version;
174
        }
175
176 1
        if (! isset($version['api'])) {
177 1
            $version['api'] = $this->createDefaultApiSettings();
178
        }
179
180 1
        $version['api'][0]['extensions'] = $this->options['extensions'];
181
182 1
        return $version;
183
    }
184
185 11
    private function overwriteIgnoredPaths(array $version): array
186
    {
187 11
        if (! isset($this->options['ignore']) || ! $this->options['ignore']) {
188 10
            return $version;
189
        }
190
191 1
        if (! isset($version['api'])) {
192 1
            $version['api'] = $this->createDefaultApiSettings();
193
        }
194
195 1
        $version['api'][0]['ignore']['paths'] = array_map(
196 1
            function ($path) {
197 1
                return new Path($path);
198 1
            },
199 1
            $this->options['ignore']
200
        );
201
202 1
        return $version;
203
    }
204
205 11
    private function overwriteMarkers(array $version): array
206
    {
207 11
        if (! isset($this->options['markers']) || ! $this->options['markers']) {
208 10
            return $version;
209
        }
210
211 1
        if (! isset($version['api'])) {
212 1
            $version['api'] = $this->createDefaultApiSettings();
213
        }
214
215 1
        $version['api'][0]['markers'] = $this->options['markers'];
216
217 1
        return $version;
218
    }
219
220 11
    private function overwriteIncludeSource(array $version): array
221
    {
222 11
        if (! isset($this->options['sourcecode'])) {
223 10
            return $version;
224
        }
225
226 1
        if (! isset($version['api'])) {
227 1
            $version['api'] = $this->createDefaultApiSettings();
228
        }
229
230 1
        $version['api'][0]['include-source'] = $this->options['sourcecode'];
231
232 1
        return $version;
233
    }
234
235 11
    private function overwriteVisibility(array $version): array
236
    {
237 11
        if (! isset($this->options['visibility']) || ! $this->options['visibility']) {
238 10
            return $version;
239
        }
240
241 1
        if (! isset($version['api'])) {
242 1
            $version['api'] = $this->createDefaultApiSettings();
243
        }
244
245 1
        $visibilities = [];
246 1
        foreach ($this->options['visibility'] as $visibility) {
247 1
            $visibilities = array_merge($visibilities, explode(',', $visibility));
248
        }
249 1
        $visibilities = array_unique($visibilities);
250 1
        $version['api'][0]['visibility'] = $visibilities;
251
252 1
        return $version;
253
    }
254
255 11
    private function overwriteDefaultPackageName(array $version): array
256
    {
257 11
        if (! isset($this->options['defaultpackagename']) || ! $this->options['defaultpackagename']) {
258 10
            return $version;
259
        }
260
261 1
        if (! isset($version['api'])) {
262 1
            $version['api'] = $this->createDefaultApiSettings();
263
        }
264
265 1
        $version['api'][0]['default-package-name'] = $this->options['defaultpackagename'];
266
267 1
        return $version;
268
    }
269
270 10
    private function createDefaultApiSettings(): array
271
    {
272 10
        return current(Version3::buildDefault()['phpdocumentor']['versions'])['api'];
273
    }
274
}
275