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 |
||
11 | abstract class DbDumper |
||
12 | { |
||
13 | /** @var string */ |
||
14 | protected $dbName; |
||
15 | |||
16 | /** @var string */ |
||
17 | protected $userName; |
||
18 | |||
19 | /** @var string */ |
||
20 | protected $password; |
||
21 | |||
22 | /** @var string */ |
||
23 | protected $host = 'localhost'; |
||
24 | |||
25 | /** @var int */ |
||
26 | protected $port = 5432; |
||
27 | |||
28 | /** @var string */ |
||
29 | protected $socket = ''; |
||
30 | |||
31 | /** @var int */ |
||
32 | protected $timeout = 0; |
||
33 | |||
34 | /** @var string */ |
||
35 | protected $dumpBinaryPath = ''; |
||
36 | |||
37 | /** @var array */ |
||
38 | protected $includeTables = []; |
||
39 | |||
40 | /** @var array */ |
||
41 | protected $excludeTables = []; |
||
42 | |||
43 | /** @var array */ |
||
44 | protected $extraOptions = []; |
||
45 | |||
46 | /** @var array */ |
||
47 | protected $extraOptionsAfterDbName = []; |
||
48 | |||
49 | /** @var object */ |
||
50 | protected $compressor = null; |
||
51 | |||
52 | public static function create() |
||
53 | { |
||
54 | return new static(); |
||
55 | } |
||
56 | |||
57 | public function getDbName(): string |
||
58 | { |
||
59 | return $this->dbName; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @param string $dbName |
||
64 | * |
||
65 | * @return $this |
||
66 | */ |
||
67 | public function setDbName(string $dbName) |
||
68 | { |
||
69 | $this->dbName = $dbName; |
||
70 | |||
71 | return $this; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @param string $userName |
||
76 | * |
||
77 | * @return $this |
||
78 | */ |
||
79 | public function setUserName(string $userName) |
||
80 | { |
||
81 | $this->userName = $userName; |
||
82 | |||
83 | return $this; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @param string $password |
||
88 | * |
||
89 | * @return $this |
||
90 | */ |
||
91 | public function setPassword(string $password) |
||
92 | { |
||
93 | $this->password = $password; |
||
94 | |||
95 | return $this; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @param string $host |
||
100 | * |
||
101 | * @return $this |
||
102 | */ |
||
103 | public function setHost(string $host) |
||
104 | { |
||
105 | $this->host = $host; |
||
106 | |||
107 | return $this; |
||
108 | } |
||
109 | |||
110 | public function getHost(): string |
||
111 | { |
||
112 | return $this->host; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @param int $port |
||
117 | * |
||
118 | * @return $this |
||
119 | */ |
||
120 | public function setPort(int $port) |
||
121 | { |
||
122 | $this->port = $port; |
||
123 | |||
124 | return $this; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @param string $socket |
||
129 | * |
||
130 | * @return $this |
||
131 | */ |
||
132 | public function setSocket(string $socket) |
||
133 | { |
||
134 | $this->socket = $socket; |
||
135 | |||
136 | return $this; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @param int $timeout |
||
141 | * |
||
142 | * @return $this |
||
143 | */ |
||
144 | public function setTimeout(int $timeout) |
||
145 | { |
||
146 | $this->timeout = $timeout; |
||
147 | |||
148 | return $this; |
||
149 | } |
||
150 | |||
151 | public function setDumpBinaryPath(string $dumpBinaryPath) |
||
161 | |||
162 | /** |
||
163 | * @deprecated |
||
164 | * |
||
165 | * @return $this |
||
166 | */ |
||
167 | public function enableCompression() |
||
173 | |||
174 | public function getCompressorExtension(): string |
||
175 | { |
||
176 | return $this->compressor->useExtension(); |
||
177 | } |
||
178 | |||
179 | public function useCompressor(Compressor $compressor) |
||
180 | { |
||
181 | $this->compressor = $compressor; |
||
182 | |||
183 | return $this; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * @param string|array $includeTables |
||
188 | * |
||
189 | * @return $this |
||
190 | * |
||
191 | * @throws \Spatie\DbDumper\Exceptions\CannotSetParameter |
||
192 | */ |
||
193 | View Code Duplication | public function includeTables($includeTables) |
|
207 | |||
208 | /** |
||
209 | * @param string|array $excludeTables |
||
210 | * |
||
211 | * @return $this |
||
212 | * |
||
213 | * @throws \Spatie\DbDumper\Exceptions\CannotSetParameter |
||
214 | */ |
||
215 | View Code Duplication | public function excludeTables($excludeTables) |
|
229 | |||
230 | /** |
||
231 | * @param string $extraOption |
||
232 | * |
||
233 | * @return $this |
||
234 | */ |
||
235 | public function addExtraOption(string $extraOption) |
||
243 | |||
244 | /** |
||
245 | * @param string $extraOptionAtEnd |
||
246 | * |
||
247 | * @return $this |
||
248 | */ |
||
249 | public function addExtraOptionAfterDbName(string $extraOptionAfterDbName) |
||
250 | { |
||
251 | if (! empty($extraOptionAfterDbName)) { |
||
252 | $this->extraOptionsAfterDbName[] = $extraOptionAfterDbName; |
||
253 | } |
||
254 | |||
255 | return $this; |
||
256 | } |
||
257 | |||
258 | abstract public function dumpToFile(string $dumpFile); |
||
259 | |||
260 | protected function checkIfDumpWasSuccessFul(Process $process, string $outputFile) |
||
274 | |||
275 | protected function getCompressCommand(string $command, string $dumpFile): string |
||
285 | |||
286 | protected function echoToFile(string $command, string $dumpFile): string |
||
296 | |||
297 | protected function determineQuote(): string |
||
301 | |||
302 | protected function isWindows(): bool |
||
306 | } |
||
307 |
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.