InputTransformer   B
last analyzed

Complexity

Total Complexity 51

Size/Duplication

Total Lines 343
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 51
lcom 1
cbo 8
dl 0
loc 343
rs 8.3206
c 0
b 0
f 0
ccs 176
cts 176
cp 1

15 Methods

Rating   Name   Duplication   Size   Complexity  
A fromCommandLine() 0 4 1
B createConfigurationFile() 0 72 5
A getValue() 0 4 2
A extractKeywords() 0 11 4
A extractAuthors() 0 16 4
A extractProvidedPackages() 0 12 4
A extractSuggestedPackages() 0 17 4
A extractSupports() 0 12 4
A extractAutoloads() 0 15 3
A extractAutoloadsDev() 0 14 3
A extractRequiredPackages() 0 12 4
A extractRequiredDevPackages() 0 12 4
A extractScripts() 0 12 4
A extractDataFromValue() 0 4 1
A extractAutoloadList() 0 12 4

How to fix   Complexity   

Complex Class

Complex classes like InputTransformer 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 InputTransformer, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace Yoanm\ComposerConfigManager\Infrastructure\Transformer;
3
4
use Yoanm\ComposerConfigManager\Domain\Model\Author;
5
use Yoanm\ComposerConfigManager\Domain\Model\Autoload;
6
use Yoanm\ComposerConfigManager\Domain\Model\Configuration;
7
use Yoanm\ComposerConfigManager\Domain\Model\ConfigurationFile;
8
use Yoanm\ComposerConfigManager\Domain\Model\Package;
9
use Yoanm\ComposerConfigManager\Domain\Model\Script;
10
use Yoanm\ComposerConfigManager\Domain\Model\SuggestedPackage;
11
use Yoanm\ComposerConfigManager\Domain\Model\Support;
12
13
class InputTransformer
14
{
15
    const SEPARATOR = '#';
16
17
    const KEY_PACKAGE_NAME = 'package-name';
18
    const KEY_TYPE = 'type';
19
    const KEY_LICENSE = 'license';
20
    const KEY_PACKAGE_VERSION = 'package-version';
21
    const KEY_DESCRIPTION = 'description';
22
    const KEY_KEYWORD = 'keyword';
23
    const KEY_AUTHOR = 'author';
24
    const KEY_PROVIDED_PACKAGE = 'provide';
25
    const KEY_SUGGESTED_PACKAGE = 'suggest';
26
    const KEY_SUPPORT = 'support';
27
    const KEY_AUTOLOAD_PSR0 = 'autoload-psr0';
28
    const KEY_AUTOLOAD_PSR4 = 'autoload-psr4';
29
    const KEY_AUTOLOAD_DEV_PSR0 = 'autoload-dev-psr0';
30
    const KEY_AUTOLOAD_DEV_PSR4 = 'autoload-dev-psr4';
31
    const KEY_REQUIRE = 'require';
32
    const KEY_REQUIRE_DEV = 'require-dev';
33
    const KEY_SCRIPT = 'script';
34
35
    /**
36
     * @param $inputList
37
     *
38
     * @return ConfigurationFile|null
39
     */
40 10
    public function fromCommandLine($inputList)
41
    {
42 10
        return $this->createConfigurationFile($inputList);
43
    }
44
45
    /**
46
     * @param array $inputList
47
     *
48
     * @return ConfigurationFile|null
49
     */
50 10
    protected function createConfigurationFile(array $inputList)
51
    {
52
        $defaultKeyList = [
53 10
            self::KEY_PACKAGE_NAME,
54 10
            self::KEY_DESCRIPTION,
55 10
            self::KEY_PACKAGE_VERSION,
56 10
            self::KEY_TYPE,
57 10
            self::KEY_KEYWORD,
58 10
            self::KEY_LICENSE,
59 10
            self::KEY_AUTHOR,
60 10
            self::KEY_SUPPORT,
61 10
            self::KEY_REQUIRE,
62 10
            self::KEY_REQUIRE_DEV,
63 10
            self::KEY_PROVIDED_PACKAGE,
64 10
            self::KEY_SUGGESTED_PACKAGE,
65 10
            self::KEY_AUTOLOAD_PSR0,
66 10
            self::KEY_AUTOLOAD_PSR4,
67 10
            self::KEY_AUTOLOAD_DEV_PSR0,
68 10
            self::KEY_AUTOLOAD_DEV_PSR4,
69 10
            self::KEY_SCRIPT,
70 10
        ];
71
        $defaultNormalizedFileKeyList = [
72 10
            ConfigurationFile::KEY_NAME => [self::KEY_PACKAGE_NAME],
73 10
            ConfigurationFile::KEY_DESCRIPTION => [self::KEY_DESCRIPTION],
74 10
            ConfigurationFile::KEY_VERSION => [self::KEY_PACKAGE_VERSION],
75 10
            ConfigurationFile::KEY_TYPE => [self::KEY_TYPE],
76 10
            ConfigurationFile::KEY_KEYWORDS => [self::KEY_KEYWORD],
77 10
            ConfigurationFile::KEY_LICENSE => [self::KEY_LICENSE],
78 10
            ConfigurationFile::KEY_AUTHORS => [self::KEY_AUTHOR],
79 10
            ConfigurationFile::KEY_SUPPORT => [self::KEY_SUPPORT],
80 10
            ConfigurationFile::KEY_REQUIRE => [self::KEY_REQUIRE],
81 10
            ConfigurationFile::KEY_REQUIRE_DEV => [self::KEY_REQUIRE_DEV],
82 10
            ConfigurationFile::KEY_PROVIDE => [self::KEY_PROVIDED_PACKAGE],
83 10
            ConfigurationFile::KEY_SUGGEST => [self::KEY_SUGGESTED_PACKAGE],
84 10
            ConfigurationFile::KEY_AUTOLOAD => [self::KEY_AUTOLOAD_PSR0, self::KEY_AUTOLOAD_PSR4],
85 10
            ConfigurationFile::KEY_AUTOLOAD_DEV => [self::KEY_AUTOLOAD_DEV_PSR0, self::KEY_AUTOLOAD_DEV_PSR4],
86 10
            ConfigurationFile::KEY_SCRIPTS => [self::KEY_SCRIPT],
87 10
        ];
88 10
        if (0 === count(array_intersect($defaultKeyList, array_keys($inputList)))) {
89 1
            return null;
90
        }
91 9
        $configKeyList = [];
92 9
        foreach ($defaultNormalizedFileKeyList as $fileKey => $inputKeyList) {
93 9
            foreach ($inputKeyList as $inputKey) {
94 9
                if (isset($inputList[$inputKey])) {
95 9
                    $configKeyList[] = $fileKey;
96 9
                    break;
97
                }
98 9
            }
99 9
        }
100
101 9
        return new ConfigurationFile(
102 9
            new Configuration(
103 9
                $this->getValue($inputList, self::KEY_PACKAGE_NAME, null),
104 9
                $this->getValue($inputList, self::KEY_TYPE, null),
105 9
                $this->getValue($inputList, self::KEY_LICENSE, null),
106 9
                $this->getValue($inputList, self::KEY_PACKAGE_VERSION, null),
107 9
                $this->getValue($inputList, self::KEY_DESCRIPTION, null),
108 9
                $this->extractKeywords($inputList),
109 9
                $this->extractAuthors($inputList),
110 9
                $this->extractProvidedPackages($inputList),
111 9
                $this->extractSuggestedPackages($inputList),
112 9
                $this->extractSupports($inputList),
113 9
                $this->extractAutoloads($inputList),
114 9
                $this->extractAutoloadsDev($inputList),
115 9
                $this->extractRequiredPackages($inputList),
116 9
                $this->extractRequiredDevPackages($inputList),
117 9
                $this->extractScripts($inputList)
118 9
            ),
119
            $configKeyList
120 9
        );
121
    }
122
123
    /**
124
     * @param array  $inputList
125
     * @param string $key
126
     * @param string $defaultValue
127
     *
128
     * @return string
129
     */
130 9
    protected function getValue(array $inputList, $key, $defaultValue)
131
    {
132 9
        return isset($inputList[$key]) ? $inputList[$key] : $defaultValue;
133
    }
134
135
    /**
136
     * @param array $inputList
137
     *
138
     * @return array
139
     */
140 9
    protected function extractKeywords(array $inputList)
141
    {
142 9
        $list = [];
143 9
        if (isset($inputList[self::KEY_KEYWORD]) && is_array($inputList[self::KEY_KEYWORD])) {
144 1
            foreach ($inputList[self::KEY_KEYWORD] as $keyword) {
145 1
                $list[] = $keyword;
146 1
            }
147 1
        }
148
149 9
        return $list;
150
    }
151
152
    /**
153
     * @param array $inputList
154
     *
155
     * @return array
156
     */
157 9
    protected function extractAuthors(array $inputList)
158
    {
159 9
        $list = [];
160 9
        if (isset($inputList[self::KEY_AUTHOR]) && is_array($inputList[self::KEY_AUTHOR])) {
161 1
            foreach ($inputList[self::KEY_AUTHOR] as $key => $author) {
162 1
                $data = $this->extractDataFromValue($author);
163 1
                $name = array_shift($data);
164 1
                $email = array_shift($data);
165 1
                $role = array_shift($data);
166
167 1
                $list[] = new Author($name, $email, $role);
168 1
            }
169 1
        }
170
171 9
        return $list;
172
    }
173
174
    /**
175
     * @param array $inputList
176
     *
177
     * @return array
178
     */
179 9
    protected function extractProvidedPackages(array $inputList)
180
    {
181 9
        $list = [];
182 9
        if (isset($inputList[self::KEY_PROVIDED_PACKAGE]) && is_array($inputList[self::KEY_PROVIDED_PACKAGE])) {
183 2
            foreach ($inputList[self::KEY_PROVIDED_PACKAGE] as $rawValue) {
184 2
                list ($name, $versionConstraint) = $this->extractDataFromValue($rawValue);
185 2
                $list[] = new Package($name, $versionConstraint);
186 2
            }
187 2
        }
188
189 9
        return $list;
190
    }
191
192
    /**
193
     * @param array $inputList
194
     *
195
     * @return array
196
     */
197 9
    protected function extractSuggestedPackages(array $inputList)
198
    {
199 9
        $list = [];
200 9
        if (isset($inputList[self::KEY_SUGGESTED_PACKAGE])
201 9
            && is_array($inputList[self::KEY_SUGGESTED_PACKAGE])
202 9
        ) {
203 2
            foreach ($inputList[self::KEY_SUGGESTED_PACKAGE] as $rawValue) {
204 2
                $data = $this->extractDataFromValue($rawValue);
205 2
                $list[] = new SuggestedPackage(
206 2
                    array_shift($data),
207 2
                    implode(self::SEPARATOR, $data)
208 2
                );
209 2
            }
210 2
        }
211
212 9
        return $list;
213
    }
214
215
    /**
216
     * @param array $inputList
217
     *
218
     * @return array
219
     */
220 9
    protected function extractSupports(array $inputList)
221
    {
222 9
        $list = [];
223 9
        if (isset($inputList[self::KEY_SUPPORT]) && is_array($inputList[self::KEY_SUPPORT])) {
224 2
            foreach ($inputList[self::KEY_SUPPORT] as $rawValue) {
225 2
                $data = $this->extractDataFromValue($rawValue);
226 2
                $list[] = new Support(array_shift($data), implode(self::SEPARATOR, $data));
227 2
            }
228 2
        }
229
230 9
        return $list;
231
    }
232
233
    /**
234
     * @param array $inputList
235
     *
236
     * @return array
237
     */
238 9
    protected function extractAutoloads(array $inputList)
239
    {
240 9
        $list = [];
241
        // PSR0
242 9
        foreach ($this->extractAutoloadList($inputList, self::KEY_AUTOLOAD_PSR0) as $namespace => $path) {
243 2
            $list[] = new Autoload(Autoload::TYPE_PSR0, $path, $namespace);
244 9
        }
245
        // PSR-4
246 9
        foreach ($this->extractAutoloadList($inputList, self::KEY_AUTOLOAD_PSR4) as $namespace => $path) {
247 2
            $list[] = new Autoload(Autoload::TYPE_PSR4, $path, $namespace);
248 9
        }
249
250
251 9
        return $list;
252
    }
253
254
    /**
255
     * @param array $inputList
256
     *
257
     * @return array
258
     */
259 9
    protected function extractAutoloadsDev(array $inputList)
260
    {
261 9
        $list = [];
262
        // PSR0
263 9
        foreach ($this->extractAutoloadList($inputList, self::KEY_AUTOLOAD_DEV_PSR0) as $namespace => $path) {
264 2
            $list[] = new Autoload(Autoload::TYPE_PSR0, $path, $namespace);
265 9
        }
266
        // PSR-4
267 9
        foreach ($this->extractAutoloadList($inputList, self::KEY_AUTOLOAD_DEV_PSR4) as $namespace => $path) {
268 2
            $list[] = new Autoload(Autoload::TYPE_PSR4, $path, $namespace);
269 9
        }
270
271 9
        return $list;
272
    }
273
274
    /**
275
     * @param array $inputList
276
     *
277
     * @return array
278
     */
279 9
    protected function extractRequiredPackages(array $inputList)
280
    {
281 9
        $list = [];
282 9
        if (isset($inputList[self::KEY_REQUIRE]) && is_array($inputList[self::KEY_REQUIRE])) {
283 2
            foreach ($inputList[self::KEY_REQUIRE] as $rawValue) {
284 2
                list ($name, $versionConstraint) = $this->extractDataFromValue($rawValue);
285 2
                $list[] = new Package($name, $versionConstraint);
286 2
            }
287 2
        }
288
289 9
        return $list;
290
    }
291
    /**
292
     * @param array $inputList
293
     *
294
     * @return array
295
     */
296 9
    protected function extractRequiredDevPackages(array $inputList)
297
    {
298 9
        $list = [];
299 9
        if (isset($inputList[self::KEY_REQUIRE_DEV]) && is_array($inputList[self::KEY_REQUIRE_DEV])) {
300 2
            foreach ($inputList[self::KEY_REQUIRE_DEV] as $rawValue) {
301 2
                list ($name, $versionConstraint) = $this->extractDataFromValue($rawValue);
302 2
                $list[] = new Package($name, $versionConstraint);
303 2
            }
304 2
        }
305
306 9
        return $list;
307
    }
308
309
    /**
310
     * @param array $inputList
311
     *
312
     * @return array
313
     */
314 9
    protected function extractScripts(array $inputList)
315
    {
316 9
        $list = [];
317 9
        if (isset($inputList[self::KEY_SCRIPT]) && is_array($inputList[self::KEY_SCRIPT])) {
318 2
            foreach ($inputList[self::KEY_SCRIPT] as $rawValue) {
319 2
                list ($name, $command) = $this->extractDataFromValue($rawValue);
320 2
                $list[] = new Script($name, $command);
321 2
            }
322 2
        }
323
324 9
        return $list;
325
    }
326
327
    /**
328
     * @param string $value
329
     *
330
     * @return array
331
     */
332 9
    protected function extractDataFromValue($value)
333
    {
334 9
        return explode(self::SEPARATOR, $value);
335
    }
336
337
    /**
338
     * @param array  $inputList
339
     * @param string $optionKey
340
     *
341
     * @return array
342
     */
343 9
    protected function extractAutoloadList(array $inputList, $optionKey)
344
    {
345 9
        $list = [];
346 9
        if (isset($inputList[$optionKey]) && is_array($inputList[$optionKey])) {
347 3
            foreach ($inputList[$optionKey] as $rawValue) {
348 3
                list ($namespace, $path) = $this->extractDataFromValue($rawValue);
349 3
                $list[$namespace] = $path;
350 3
            }
351 3
        }
352
353 9
        return $list;
354
    }
355
}
356