Completed
Push — master ( a59c49...1dd4d3 )
by personal
26s queued 20s
created

ConfigFileReaderJson   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 91
Duplicated Lines 6.59 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 6
loc 91
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
F read() 6 58 15
A resolvePath() 0 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 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