Completed
Push — master ( a59c49...1dd4d3 )
by personal
18s queued 11s
created

ConfigFileReaderJson::read()   F

Complexity

Conditions 15
Paths 290

Size

Total Lines 58

Duplication

Lines 6
Ratio 10.34 %

Importance

Changes 0
Metric Value
cc 15
nc 290
nop 1
dl 6
loc 58
rs 3.9583
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
                $include = $this->resolvePath($include);
49
                $files[] = $include;
50
            }
51
            $config->set('files', $files);
52
        }
53
54
        if (isset($jsonData['groups'])) {
55
            $config->set('groups', $jsonData['groups']);
56
        }
57
58
        if (isset($jsonData['extensions'])) {
59
            $config->set('extensions', implode(',', $jsonData['extensions']));
60
        }
61
62
        if (isset($jsonData['excludes'])) {
63
            // retro-compatibility
64
            // "exclude" is a string
65
            // excludes is an array
66
            $config->set('exclude', implode(',', $jsonData['excludes']));
67
        } else if (isset($jsonData['exclude'])) {
68
            $config->set('exclude', $jsonData['exclude']);
69
        }
70
71 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...
72
            $config->set('git', $jsonData['plugins']['git']['binary']);
73
        }
74
75 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...
76
            $config->set('junit', $jsonData['plugins']['junit']["report"]);
77
        }
78
79
        // reports
80
        if (isset($jsonData['report']) && is_array($jsonData['report'])) {
81
            foreach ($jsonData['report'] as $reportType => $path) {
82
                $path = $this->resolvePath($path);
83
                $config->set('report-' . $reportType, $path);
84
            }
85
        }
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    private function resolvePath($path)
92
    {
93
        if (DIRECTORY_SEPARATOR !== $path[0]) {
94
             $path = dirname($this->filename) . DIRECTORY_SEPARATOR . $path;
95
        }
96
97
        return $path;
98
    }
99
}
100