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

ConfigFileReaderJson   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 86
Duplicated Lines 6.98 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
F read() 6 65 17

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
                $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