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 |
||
9 | class MySql extends DbDumper |
||
10 | { |
||
11 | /** @var bool */ |
||
12 | protected $skipComments = true; |
||
13 | |||
14 | /** @var bool */ |
||
15 | protected $useExtendedInserts = true; |
||
16 | |||
17 | /** @var bool */ |
||
18 | protected $useSingleTransaction = false; |
||
19 | |||
20 | /** @var string */ |
||
21 | protected $defaultCharacterSet = ''; |
||
22 | |||
23 | /** @var bool */ |
||
24 | protected $dbNameWasSetAsExtraOption = false; |
||
25 | |||
26 | /** @var string */ |
||
27 | protected $setGtidPurged = 'AUTO'; |
||
28 | |||
29 | public function __construct() |
||
33 | |||
34 | /** |
||
35 | * @return $this |
||
36 | */ |
||
37 | public function skipComments() |
||
43 | |||
44 | /** |
||
45 | * @return $this |
||
46 | */ |
||
47 | public function dontSkipComments() |
||
53 | |||
54 | /** |
||
55 | * @return $this |
||
56 | */ |
||
57 | public function useExtendedInserts() |
||
63 | |||
64 | /** |
||
65 | * @return $this |
||
66 | */ |
||
67 | public function dontUseExtendedInserts() |
||
73 | |||
74 | /** |
||
75 | * @return $this |
||
76 | */ |
||
77 | public function useSingleTransaction() |
||
83 | |||
84 | /** |
||
85 | * @return $this |
||
86 | */ |
||
87 | public function dontUseSingleTransaction() |
||
93 | |||
94 | /** |
||
95 | * @param string $characterSet |
||
96 | * |
||
97 | * @return $this |
||
98 | */ |
||
99 | public function setDefaultCharacterSet(string $characterSet) |
||
105 | |||
106 | /** |
||
107 | * @return $this |
||
108 | */ |
||
109 | public function setGtidPurged(string $setGtidPurged) |
||
110 | { |
||
111 | $this->setGtidPurged = $setGtidPurged; |
||
112 | |||
113 | return $this; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Dump the contents of the database to the given file. |
||
118 | * |
||
119 | * @param string $dumpFile |
||
120 | * |
||
121 | * @throws \Spatie\DbDumper\Exceptions\CannotStartDump |
||
122 | * @throws \Spatie\DbDumper\Exceptions\DumpFailed |
||
123 | */ |
||
124 | View Code Duplication | public function dumpToFile(string $dumpFile) |
|
144 | |||
145 | public function addExtraOption(string $extraOption) |
||
154 | |||
155 | /** |
||
156 | * Get the command that should be performed to dump the database. |
||
157 | * |
||
158 | * @param string $dumpFile |
||
159 | * @param string $temporaryCredentialsFile |
||
160 | * |
||
161 | * @return string |
||
162 | */ |
||
163 | public function getDumpCommand(string $dumpFile, string $temporaryCredentialsFile): string |
||
219 | |||
220 | public function getContentsOfCredentialsFile(): string |
||
232 | |||
233 | View Code Duplication | protected function guardAgainstIncompleteCredentials() |
|
241 | |||
242 | protected function determineQuote(): string |
||
246 | } |
||
247 |
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.