Completed
Push — master ( 47e9b1...f4c980 )
by Eugene
02:36
created

ScriptHandler   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 129
Duplicated Lines 12.4 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 72.09%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 19
c 3
b 1
f 0
lcom 1
cbo 4
dl 16
loc 129
ccs 31
cts 43
cp 0.7209
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A generateCSS() 0 19 4
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
    public static function generateCSS(Event $event)
31
    {
32
        $extra = $event->getComposer()->getPackage()->getExtra();
33
        static::validateConfiguration($extra);
34
35
        $processor = new Processor($event->getIO());
36
        $currentDirectory = getcwd();
37
38
        foreach ($extra[static::CONFIG_MAIN_KEY] as $config) {
39
            foreach ($config[static::OPTION_KEY_INPUT] as $value) {
40
                $processor->attachFiles("{$currentDirectory}/{$value}", "{$currentDirectory}/{$config[static::OPTION_KEY_OUTPUT]}");
41
            }
42
43
            $formatter = isset($config[static::OPTION_KEY_FORMATTER]) ? $config[static::OPTION_KEY_FORMATTER] : static::DEFAULT_OPTION_FORMATTER;
44
45
            $processor->processFiles($formatter);
46
        }
47
        $processor->saveOutput();
48
    }
49
50
    /**
51
     * @param array $config
52
     *
53
     * @return bool
54
     * @throws \InvalidArgumentException
55
     */
56 5
    protected static function validateConfiguration(array $config)
57
    {
58 5
        if (empty($config[static::CONFIG_MAIN_KEY])) {
59 2
            throw new \InvalidArgumentException('compiler should needs to be configured through the extra.css-compiler setting');
60
        }
61
62 3
        if (!is_array($config[static::CONFIG_MAIN_KEY])) {
63 1
            throw new \InvalidArgumentException('the extra.css-compiler setting must be an array of objects');
64
        }
65
66 2
        return static::validateOptions($config[static::CONFIG_MAIN_KEY]);
67
    }
68
69
    /**
70
     * @param array $config
71
     *
72
     * @return bool
73
     * @throws \InvalidArgumentException
74
     */
75 7
    protected static function validateOptions(array $config)
76
    {
77 7
        foreach ($config as $option) {
78 7
            if (!is_array($option)) {
79 1
                throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[]." . static::OPTION_KEY_INPUT . ' array');
80
            }
81
82 6
            static::validateMandatoryOptions($option);
83
        }
84
85 2
        return true;
86
    }
87
88
    /**
89
     * @param array $config
90
     *
91
     * @return bool
92
     * @throws \InvalidArgumentException
93
     */
94 6
    protected static function validateMandatoryOptions(array $config)
95
    {
96 6
        foreach (static::$mandatoryOptions as $option) {
97 6
            if (empty($config[$option])) {
98 1
                throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[].{$option} is required!");
99
            }
100
101
            switch ($option) {
102 5
                case static::OPTION_KEY_INPUT:
103 5
                    static::validateIsArray($config[$option]);
104 3
                    break;
105 3
                case static::OPTION_KEY_OUTPUT:
106 3
                    static::validateIsString($config[$option]);
107 3
                    break;
108
            }
109
        }
110
111 2
        return true;
112
    }
113
114
    /**
115
     * @param array $option
116
     *
117
     * @return bool
118
     */
119 5 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...
120
    {
121 5
        if (!is_array($option)) {
122 2
            throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . '[]' . static::OPTION_KEY_INPUT . ' should be array!');
123
        }
124
125 3
        return true;
126
    }
127
128
    /**
129
     * @param string $option
130
     *
131
     * @return bool
132
     */
133 3 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...
134
    {
135 3
        if (!is_string($option)) {
136 1
            throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . '[]' . static::OPTION_KEY_OUTPUT . ' should string!');
137
        }
138
139 2
        return true;
140
    }
141
}
142