Completed
Push — master ( 832993...8b4c85 )
by Eugene
40s
created

ScriptHandler   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 135
Duplicated Lines 11.85 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 21
c 4
b 1
f 0
lcom 1
cbo 4
dl 16
loc 135
ccs 46
cts 46
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A generateCSS() 0 20 4
A resolvePath() 0 4 2
A validateConfiguration() 0 12 3
A validateOptions() 0 12 3
B validateMandatoryOptions() 0 19 5
A validateIsArray() 8 8 2
A validateIsString() 8 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace EM\CssCompiler;
4
5
use Composer\Script\Event;
6
use EM\CssCompiler\Processor\Processor;
7
8
/**
9
 * @see   ScriptHandlerTest
10
 *
11
 * @since 0.1
12
 */
13
class ScriptHandler
14
{
15
    const CONFIG_MAIN_KEY          = 'css-compiler';
16
    const OPTION_KEY_INPUT         = 'input';
17
    const OPTION_KEY_OUTPUT        = 'output';
18
    const OPTION_KEY_FORMATTER     = 'format';
19
    const DEFAULT_OPTION_FORMATTER = 'compact';
20
    protected static $mandatoryOptions = [
21
        self::OPTION_KEY_INPUT,
22
        self::OPTION_KEY_OUTPUT
23
    ];
24
25
    /**
26
     * @param Event $event
27
     *
28
     * @throws \InvalidArgumentException
29
     */
30 1
    public static function generateCSS(Event $event)
31
    {
32 1
        $extra = $event->getComposer()->getPackage()->getExtra();
33 1
        static::validateConfiguration($extra);
34
35 1
        $processor = new Processor($event->getIO());
36
37 1
        foreach ($extra[static::CONFIG_MAIN_KEY] as $config) {
38 1
            foreach ($config[static::OPTION_KEY_INPUT] as $inputSource) {
39 1
                $processor->attachFiles(
40 1
                    static::resolvePath($inputSource, getcwd()),
41 1
                    static::resolvePath($config[static::OPTION_KEY_OUTPUT], getcwd())
42
                );
43
            }
44
45 1
            $formatter = isset($config[static::OPTION_KEY_FORMATTER]) ? $config[static::OPTION_KEY_FORMATTER] : static::DEFAULT_OPTION_FORMATTER;
46 1
            $processor->processFiles($formatter);
47
        }
48 1
        $processor->saveOutput();
49 1
    }
50
51 1
    protected static function resolvePath($path, $prefix)
52
    {
53 1
        return '/' === substr($path, 0, 1) ? $path : "{$prefix}/{$path}";
54
    }
55
56
    /**
57
     * @param array $config
58
     *
59
     * @return bool
60
     * @throws \InvalidArgumentException
61
     */
62 6
    protected static function validateConfiguration(array $config)
63
    {
64 6
        if (empty($config[static::CONFIG_MAIN_KEY])) {
65 2
            throw new \InvalidArgumentException('compiler should needs to be configured through the extra.css-compiler setting');
66
        }
67
68 4
        if (!is_array($config[static::CONFIG_MAIN_KEY])) {
69 1
            throw new \InvalidArgumentException('the extra.css-compiler setting must be an array of objects');
70
        }
71
72 3
        return static::validateOptions($config[static::CONFIG_MAIN_KEY]);
73
    }
74
75
    /**
76
     * @param array $config
77
     *
78
     * @return bool
79
     * @throws \InvalidArgumentException
80
     */
81 8
    protected static function validateOptions(array $config)
82
    {
83 8
        foreach ($config as $option) {
84 8
            if (!is_array($option)) {
85 1
                throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[]." . static::OPTION_KEY_INPUT . ' array');
86
            }
87
88 7
            static::validateMandatoryOptions($option);
89
        }
90
91 3
        return true;
92
    }
93
94
    /**
95
     * @param array $config
96
     *
97
     * @return bool
98
     * @throws \InvalidArgumentException
99
     */
100 7
    protected static function validateMandatoryOptions(array $config)
101
    {
102 7
        foreach (static::$mandatoryOptions as $option) {
103 7
            if (empty($config[$option])) {
104 1
                throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[].{$option} is required!");
105
            }
106
107
            switch ($option) {
108 6
                case static::OPTION_KEY_INPUT:
109 6
                    static::validateIsArray($config[$option]);
110 4
                    break;
111 4
                case static::OPTION_KEY_OUTPUT:
112 4
                    static::validateIsString($config[$option]);
113 4
                    break;
114
            }
115
        }
116
117 3
        return true;
118
    }
119
120
    /**
121
     * @param array $option
122
     *
123
     * @return bool
124
     */
125 6 View Code Duplication
    protected static function validateIsArray($option)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

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

Loading history...
126
    {
127 6
        if (!is_array($option)) {
128 2
            throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . '[]' . static::OPTION_KEY_INPUT . ' should be array!');
129
        }
130
131 4
        return true;
132
    }
133
134
    /**
135
     * @param string $option
136
     *
137
     * @return bool
138
     */
139 4 View Code Duplication
    protected static function validateIsString($option)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

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

Loading history...
140
    {
141 4
        if (!is_string($option)) {
142 1
            throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . '[]' . static::OPTION_KEY_OUTPUT . ' should string!');
143
        }
144
145 3
        return true;
146
    }
147
}
148