Completed
Push — master ( 4c81c5...bcd560 )
by CodexShaper
02:04
created

DumperTrait   A

Complexity

Total Complexity 42

Size/Duplication

Total Lines 274
Duplicated Lines 0 %

Test Coverage

Coverage 25.81%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 96
c 3
b 0
f 0
dl 0
loc 274
ccs 24
cts 93
cp 0.2581
rs 9.0399
wmc 42

32 Methods

Rating   Name   Duplication   Size   Complexity  
A setUserName() 0 5 1
A setHost() 0 5 1
A setTimeOut() 0 5 1
A setDbName() 0 4 1
A setPassword() 0 5 1
A setSocket() 0 5 1
A setPort() 0 5 1
A setCompressBinaryPath() 0 4 1
A isWindows() 0 3 2
A getPassword() 0 3 1
A setDestinationPath() 0 5 1
A setCommandBinaryPath() 0 5 1
A getHost() 0 3 1
A getDbName() 0 3 1
A getTimeOut() 0 3 1
A removeExtraSpaces() 0 3 1
A getRestorePath() 0 3 1
A setRestorePath() 0 5 1
A getRestoreCommand() 0 13 3
A setCompressExtension() 0 4 1
A enableDebug() 0 4 1
A getSocket() 0 3 1
A getCommandBinaryPath() 0 3 1
A getPort() 0 3 1
A setCompressCommand() 0 5 1
A getUserName() 0 3 1
A setTables() 0 13 3
A getDumpCommand() 0 13 3
A setIgnoreTables() 0 14 3
A quoteCommand() 0 3 2
A getDestinationPath() 0 3 1
A useCompress() 0 8 1

How to fix   Complexity   

Complex Class

Complex classes like DumperTrait 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.

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 DumperTrait, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace CodexShaper\Dumper\Traits;
4
5
trait DumperTrait
6
{
7
    /*@var string*/
8
    protected $dbName;
9
    /*@var string*/
10
    protected $username;
11
    /*@var string*/
12
    protected $password;
13
    /*@var string*/
14
    protected $host = 'localhost';
15
    /*@var int*/
16
    protected $port = 3306;
17
    /*@var string*/
18
    protected $socket = '';
19
    /*@var string*/
20
    protected $commandBinaryPath = '';
21
    /*@var int*/
22
    protected $timeout = 0;
23
    /*@var array*/
24
    protected $tables = [];
25
    /*@var array*/
26
    protected $ignoreTables = [];
27
    /*@var string*/
28
    protected $destinationPath = 'dump.sql';
29
    /*@var string*/
30
    protected $restorePath = 'dump.sql';
31
    /*@var bool*/
32
    protected $isCompress = false;
33
    /*@var string*/
34
    protected $compressCommand = "gzip";
35
    /*@var string*/
36
    protected $compressBinaryPath = "";
37
    /*@var string*/
38
    protected $compressExtension = ".gz";
39
    /*@var bool*/
40
    protected $debug = false;
41
    /*@var string*/
42
    protected $command = "";
43
44
    public function setDbName(string $name)
45
    {
46 16
        $this->dbName = $name;
47
        return $this;
48 16
    }
49 16
50
    public function setUserName(string $username)
51
    {
52 16
        $this->username = $username;
53
54 16
        return $this;
55
    }
56 16
57
    public function setPassword(string $password)
58
    {
59 16
        $this->password = $password;
60
61 16
        return $this;
62
    }
63 16
64
    public function setHost(string $host)
65
    {
66
        $this->host = $host;
67
68
        return $this;
69
    }
70
71
    public function setPort(int $port)
72
    {
73
        $this->port = $port;
74
75
        return $this;
76
    }
77
78
    public function setSocket(string $socket)
79
    {
80
        $this->socket = $socket;
81
82
        return $this;
83
    }
84
85
    public function setTimeOut(int $timeout)
86
    {
87
        $this->timeout = $timeout;
88
89
        return $this;
90
    }
91
    /**
92
     * @param string|array $tables
93
     * @throws \Exception
94
     */
95
    public function setTables($tables)
96
    {
97
        if (!empty($this->ignoreTables)) {
98
            throw new \Exception("You can choose only once between tables and ignoreTables at a time");
99
        }
100
101
        if (is_string($tables)) {
102
            $tables = [$tables];
103
        }
104
105
        $this->tables = $tables;
106
107
        return $this;
108
    }
109
    /**
110
     * @param string|array $tables
111
     * @throws \Exception
112
     */
113
    public function setIgnoreTables($tables)
114
    {
115
        if (!empty($this->tables)) {
116
            throw new \Exception("You can choose only once between tables and ignoreTables at a time");
117
118
        }
119
120
        if (is_string($tables)) {
121
            $tables = [$tables];
122
        }
123
124
        $this->ignoreTables = $tables;
125
126
        return $this;
127
    }
128
129
    public function setCommandBinaryPath(string $path)
130
    {
131
        $this->commandBinaryPath = $path;
132
133
        return $this;
134
    }
135
136
    public function setDestinationPath(string $path)
137
    {
138
        $this->destinationPath = $path;
139 2
140
        return $this;
141 2
    }
142
143 2
    public function setRestorePath(string $path)
144
    {
145
        $this->restorePath = $path;
146
147
        return $this;
148
    }
149
150
    // Compress
151
    public function setCompressBinaryPath(string $path)
152
    {
153
        $this->compressBinaryPath = $path;
154
        return $this;
155
    }
156
157
    public function setCompressCommand(string $command)
158
    {
159
        $this->compressCommand = $command;
160
        $this->isCompress      = true;
161
        return $this;
162
    }
163
164
    public function setCompressExtension(string $extension)
165
    {
166
        $this->compressExtension = $extension;
167
        return $this;
168
    }
169
170
    public function useCompress(string $command = "gzip", string $extension = ".gz", string $binary_path = "")
171
    {
172
        $this->compressCommand    = $command;
173
        $this->compressExtension  = $extension;
174
        $this->compressBinaryPath = $binary_path;
175
        $this->isCompress         = true;
176
177
        return $this;
178
    }
179
180 4
    public function enableDebug()
181
    {
182 4
        $this->debug = true;
183 4
        return $this;
184 4
    }
185 4
186
    public function getDbName()
187 4
    {
188
        return $this->dbName;
189
    }
190
191
    public function getUserName()
192
    {
193
        return $this->username;
194
    }
195
196
    public function getPassword()
197
    {
198
        return $this->password;
199
    }
200
201
    public function getHost()
202
    {
203
        return $this->host;
204
    }
205
206
    public function getPort()
207
    {
208
        return $this->port;
209
    }
210
211
    public function getSocket()
212
    {
213
        return $this->socket;
214
    }
215
216
    public function getTimeOut()
217
    {
218
        return $this->timeout;
219
    }
220
221
    public function getCommandBinaryPath()
222
    {
223
        return $this->commandBinaryPath;
224
    }
225
226
    public function getDestinationPath()
227
    {
228
        return $this->destinationPath;
229
    }
230
231
    public function getRestorePath()
232
    {
233
        return $this->restorePath;
234
    }
235
236
    public function getDumpCommand(string $credentialFile = '', $destinationPath = '')
237
    {
238
        $destinationPath = !empty($destinationPath) ? $destinationPath : $this->destinationPath;
239
        switch (strtolower($this->getDumperClassName())) {
0 ignored issues
show
Bug introduced by
It seems like getDumperClassName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

239
        switch (strtolower($this->/** @scrutinizer ignore-call */ getDumperClassName())) {
Loading history...
240
            case 'mysqldumper':
241
                $dumpCommand = $this->prepareDumpCommand($credentialFile, $destinationPath);
0 ignored issues
show
Bug introduced by
It seems like prepareDumpCommand() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

241
                /** @scrutinizer ignore-call */ 
242
                $dumpCommand = $this->prepareDumpCommand($credentialFile, $destinationPath);
Loading history...
242
                break;
243
            default:
244
                $dumpCommand = $this->prepareDumpCommand($destinationPath);
245
                break;
246 16
        }
247
248 16
        return $this->removeExtraSpaces($dumpCommand);
249
    }
250
251 16
    public function getRestoreCommand(string $credentialFile = '', string $filePath = '')
252
    {
253 16
        $filePath = !empty($filePath) ? '"' . $filePath : $this->restorePath;
254
        switch (strtolower($this->getDumperClassName())) {
255
            case 'mysqldumper':
256 16
                $restoreCommand = $this->prepareRestoreCommand($credentialFile, $filePath);
0 ignored issues
show
Bug introduced by
It seems like prepareRestoreCommand() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

256
                /** @scrutinizer ignore-call */ 
257
                $restoreCommand = $this->prepareRestoreCommand($credentialFile, $filePath);
Loading history...
257
                break;
258 16
            default:
259
                $restoreCommand = $this->prepareRestoreCommand($filePath);
260
                break;
261
        }
262
263
        return $this->removeExtraSpaces($restoreCommand);
264
    }
265
266
    public function removeExtraSpaces(string $str)
267
    {
268
        return preg_replace('/\s+/', ' ', $str);
269
    }
270
271
    public static function isWindows()
272
    {
273
        return strcasecmp(substr(PHP_OS, 0, 3), 'WIN') == 0 ? true : false;
274
    }
275
276
    public function quoteCommand(string $command)
277
    {
278
        return static::isWindows() ? "\"{$command}\"" : "'{$command}'";
279
    }
280
}
281