Config   B
last analyzed

Complexity

Total Complexity 44

Size/Duplication

Total Lines 208
Duplicated Lines 7.69 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 44
lcom 1
cbo 3
dl 16
loc 208
rs 8.3396
c 0
b 0
f 0

22 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A load() 0 17 2
A getRaw() 0 4 1
A getFile() 0 4 1
A getFormattedConfiguration() 0 5 1
A getProject() 0 8 2
A getReplacements() 0 8 2
A isLockTables() 0 8 2
A getExcludeTables() 0 8 3
A isSingleWebSite() 0 5 2
A getDatabase() 0 17 2
A getDatabaseName() 8 8 2
A getDatabasePrefix() 8 8 2
A getCleanup() 0 8 2
B getTruncateCleanup() 0 18 5
A getDeleteCleanup() 0 8 2
A isNotifyOnForCommand() 0 5 2
A getNotificationConfig() 0 5 2
A getNotificationTransport() 0 4 2
A getNotificationFrom() 0 4 2
A getNotificationTo() 0 4 2
A echoJsonException() 0 7 2

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Config often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Config, and based on these observations, apply Extract Interface, too.

1
<?php namespace Tequilarapido\Cli\Config;
2
3
use Hazbo\Json\Formatter;
4
use Herrera\Json\Exception\JsonException;
5
use Herrera\Json\Json;
6
7
/**
8
 * Manage configuration from the json configuration file
9
 */
10
class Config
11
{
12
13
    protected $file;
14
    protected $schemaFile;
15
    protected $raw;
16
17
    public function __construct($file, $schemaFile)
18
    {
19
        $this->file = $file;
20
        $this->schemaFile = $schemaFile;
21
    }
22
23
    public function load()
24
    {
25
        $linter = new Json();
26
        $json = $linter->decodeFile($this->file);
27
28
        // Validate json against schema
29
        try {
30
            $linter->validate($linter->decodeFile($this->schemaFile), $json);
31
        } catch (JsonException $e) {
32
            $this->echoJsonException($e);
33
            return false;
34
        }
35
36
        // Ok ?
37
        $this->raw = $json;
38
        return true;
39
    }
40
41
    public function getRaw()
42
    {
43
        return $this->raw;
44
    }
45
46
    public function getFile()
47
    {
48
        return $this->file;
49
    }
50
51
    public function getFormattedConfiguration()
52
    {
53
        $formatter = new Formatter();
54
        return $formatter->format(json_encode($this->raw));
55
    }
56
57
    public function getProject()
58
    {
59
        if (isset($this->raw->project)) {
60
            return $this->raw->project;
61
        }
62
63
        return null;
64
    }
65
66
67
    public function getReplacements()
68
    {
69
        if (isset($this->raw->replace->replacements)) {
70
            return $this->raw->replace->replacements;
71
        }
72
73
        return null;
74
    }
75
76
    public function isLockTables()
77
    {
78
        if (isset($this->raw->replace->lockTables)) {
79
            return (bool)json_decode($this->raw->replace->lockTables);
80
        }
81
82
        return null;
83
    }
84
85
    public function getExcludeTables()
86
    {
87
        if (isset($this->raw->replace->excludeTables) && is_array($this->raw->replace->excludeTables)) {
88
            return $this->raw->replace->excludeTables;
89
        }
90
91
        return array();
92
    }
93
94
95
    public function isSingleWebSite()
96
    {
97
        $replacements = $this->getReplacements();
98
        return is_array($replacements) && count($replacements) === 1;
99
    }
100
101
    public function getDatabase()
102
    {
103
        if (isset($this->raw->database)) {
104
            return array(
105
                'driver' => 'mysql',
106
                'host' => $this->raw->database->host,
107
                'database' => $this->raw->database->database,
108
                'username' => $this->raw->database->username,
109
                'password' => $this->raw->database->password,
110
                'charset' => 'utf8',
111
                'collation' => 'utf8_general_ci',
112
                'prefix' => '',
113
            );
114
        }
115
116
        return null;
117
    }
118
119 View Code Duplication
    public function getDatabaseName()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
120
    {
121
        if (!empty($this->raw->database->database)) {
122
            return $this->raw->database->database;
123
        }
124
125
        throw new \LogicException('Database name is not specified. Maybe the configuration file is not valid');
126
    }
127
128 View Code Duplication
    public function getDatabasePrefix()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
129
    {
130
        if (!empty($this->raw->database->prefix)) {
131
            return $this->raw->database->prefix;
132
        }
133
134
        throw new \LogicException('Database prefix is not specified. Maybe the configuration file is not valid');
135
    }
136
137
    public function getCleanup()
138
    {
139
        if (isset($this->raw->cleanup)) {
140
            return $this->raw->cleanup;
141
        }
142
143
        return null;
144
    }
145
146
    public function getTruncateCleanup()
147
    {
148
        $conf = new \stdClass();
149
150
        if (isset($this->raw->cleanup->truncate->simple)) {
151
            $conf->simple = (array)$this->raw->cleanup->truncate->simple;
152
        }
153
154
        if (isset($this->raw->cleanup->truncate->multi)) {
155
            $conf->multi = (array)$this->raw->cleanup->truncate->multi;
156
        }
157
158
        if (!isset($conf->simple) && !isset($conf->multi)) {
159
            return null;
160
        }
161
162
        return $conf;
163
    }
164
165
    public function getDeleteCleanup()
166
    {
167
        if (empty($this->raw->cleanup->delete)) {
168
            return null;
169
        }
170
171
        return $this->raw->cleanup->delete;
172
    }
173
174
175
    /**
176
     * @param string $command
177
     * @return bool
178
     */
179
    public function isNotifyOnForCommand($command)
180
    {
181
        return !empty($this->raw->{$command}->notify)
182
            && $this->raw->{$command}->notify;
183
    }
184
185
    public function getNotificationConfig()
186
    {
187
        return !empty($this->raw->notify)
188
            && $this->raw->notify;
189
    }
190
191
    public function getNotificationTransport()
192
    {
193
        return empty($this->raw->notify->transport) ? null : $this->raw->notify->transport;
194
    }
195
196
    public function getNotificationFrom()
197
    {
198
        return empty($this->raw->notify->from) ? null : $this->raw->notify->from;
199
    }
200
201
    public function getNotificationTo()
202
    {
203
        return empty($this->raw->notify->to) ? null : $this->raw->notify->to;
204
    }
205
206
207
    /**
208
     * @param JsonException $e
209
     */
210
    private function echoJsonException($e)
211
    {
212
        echo 'Your configuration file may contain errors, ' . $e->getMessage() . PHP_EOL;
213
        foreach ($e->getErrors() as $k => $error) {
214
            echo ($k + 1) . ' : ' . $error . PHP_EOL;
215
        }
216
    }
217
}