Completed
Pull Request — master (#428)
by personal
02:31
created

ConfigFileReaderJson::read()   F

Complexity

Conditions 17
Paths 1634

Size

Total Lines 65

Duplication

Lines 6
Ratio 9.23 %

Importance

Changes 0
Metric Value
cc 17
nc 1634
nop 1
dl 6
loc 65
rs 1.0499
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Hal\Application\Config\File;
4
5
use Hal\Application\Config\Config;
6
use RecursiveArrayIterator;
7
use RecursiveIteratorIterator;
8
9
class ConfigFileReaderJson implements ConfigFileReaderInterface
10
{
11
    /**
12
     * @var string
13
     */
14
    private $filename;
15
16
    /**
17
     * @param string $filename
18
     */
19
    public function __construct($filename)
20
    {
21
        $this->filename = $filename;
22
    }
23
24
    /**
25
     * @param Config $config
26
     *
27
     * @return void
28
     */
29
    public function read(Config $config)
30
    {
31
        $jsonText = file_get_contents($this->filename);
32
33
        if (false === $jsonText) {
34
            throw new \InvalidArgumentException("Cannot read configuration file '{$this->filename}'");
35
        }
36
37
        $jsonData = json_decode($jsonText, true);
38
39
        if (false === $jsonData || null === $jsonData) {
40
            throw new \InvalidArgumentException("Bad json file '{$this->filename}'");
41
        }
42
43
        if (isset($jsonData['includes'])) {
44
            $includes = $jsonData['includes'];
45
            $files = [];
46
            // with config file, includes are relative to config file
47
            foreach ($includes as $include) {
48
                $files[] = dirname($this->filename) . DIRECTORY_SEPARATOR . $include;
49
            }
50
            $config->set('files', $files);
51
        }
52
53
        if (isset($jsonData['groups'])) {
54
            $config->set('groups', $jsonData['groups']);
55
        }
56
57
        if (isset($jsonData['extensions'])) {
58
            $config->set('extensions', implode(',', $jsonData['extensions']));
59
        }
60
61
        if (isset($jsonData['excludes'])) {
62
            // retro-compatibility
63
            // "exclude" is a string
64
            // excludes is an array
65
            $config->set('exclude', implode(',', $jsonData['excludes']));
66
        } else if (isset($jsonData['exclude'])) {
67
            $config->set('exclude', $jsonData['exclude']);
68
        }
69
70 View Code Duplication
        if (isset($jsonData['plugins'], $jsonData['plugins']['git'], $jsonData['plugins']['git']['binary'])) {
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...
71
            $config->set('git', $jsonData['plugins']['git']['binary']);
72
        }
73
74 View Code Duplication
        if (isset($jsonData['plugins'], $jsonData['plugins']['junit'], $jsonData['plugins']['junit']["report"])) {
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...
75
            $config->set('junit', $jsonData['plugins']['junit']["report"]);
76
        }
77
78
        // reports
79
        if (isset($jsonData['report'])) {
80
            if (isset($jsonData['report']['html'])) {
81
                $config->set('report-html', $jsonData['report']['html']);
82
            }
83
            if (isset($jsonData['report']['csv'])) {
84
                $config->set('report-csv', $jsonData['report']['csv']);
85
            }
86
            if (isset($jsonData['report']['json'])) {
87
                $config->set('report-json', $jsonData['report']['json']);
88
            }
89
            if (isset($jsonData['report']['violations'])) {
90
                $config->set('report-violations', $jsonData['report']['violations']);
91
            }
92
        }
93
    }
94
}
95