Completed
Push — master ( aba7f7...29fb7d )
by Eugene
29s
created

ScriptHandler   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 55
Duplicated Lines 10.91 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 11
c 3
b 1
f 0
lcom 0
cbo 4
dl 6
loc 55
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B compileCSS() 0 22 4
C validateConfiguration() 6 23 7

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\Handler;
4
5
use Composer\Script\Event;
6
use EM\CssCompiler\Processor\Processor;
7
8
class ScriptHandler
9
{
10
    const CONFIG_MAIN_KEY          = 'css-compiler';
11
    const CONFIG_INPUT_KEY         = 'input';
12
    const CONFIG_OUTPUT_KEY        = 'output';
13
    const CONFIG_FORMATTER_KEY     = 'format';
14
    const CONFIG_DEFAULT_FORMATTER = 'compact';
15
16
    public static function compileCSS(Event $event)
17
    {
18
        $extra = $event->getComposer()->getPackage()->getExtra();
19
20
        static::validateConfiguration($extra);
21
22
        $processor = new Processor($event->getIO());
23
        $currentDirectory = getcwd();
24
        foreach ($extra[static::CONFIG_MAIN_KEY] as $config) {
25
            foreach ($config[static::CONFIG_INPUT_KEY] as $item => $value) {
26
                $processor->attachFiles("{$currentDirectory}/{$value}", "{$currentDirectory}/{$config[static::CONFIG_OUTPUT_KEY]}");
27
            }
28
29
            $formatter = isset($config[static::CONFIG_FORMATTER_KEY])
30
                ? $config[static::CONFIG_FORMATTER_KEY]
31
                : static::CONFIG_DEFAULT_FORMATTER;
32
33
            $processor->processFiles($formatter);
34
        }
35
36
        $processor->saveOutput();
37
    }
38
39
    protected static function validateConfiguration(array $config)
40
    {
41
        if (!isset($config[static::CONFIG_MAIN_KEY])) {
42
            throw new \InvalidArgumentException('the parameter handler needs to be configured through the extra.css-compiler setting.');
43
        }
44
45
        if (!is_array($config[static::CONFIG_MAIN_KEY])) {
46
            throw new \InvalidArgumentException('the extra.css-compiler setting must be an array of a configuration objects.');
47
        }
48
49
        foreach ($config[static::CONFIG_MAIN_KEY] as $el) {
50
            if (!is_array($el)) {
51
                throw new \InvalidArgumentException('The extra.css-compiler should contain only configuration objects.');
52
            }
53
54 View Code Duplication
            if (!isset($el[static::CONFIG_INPUT_KEY])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
55
                throw new \InvalidArgumentException('The extra.css-compiler[].' . static::CONFIG_INPUT_KEY . ' is required!');
56
            }
57 View Code Duplication
            if (!isset($el[static::CONFIG_OUTPUT_KEY])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
58
                throw new \InvalidArgumentException('The extra.css-compiler[].' . static::CONFIG_OUTPUT_KEY . ' is required!');
59
            }
60
        }
61
    }
62
}
63