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 |
||
21 | class Pgdump extends SimulatorExecutable implements Simulator |
||
22 | { |
||
23 | /** |
||
24 | * pg_dump default format. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | const DEFAULT_FORMAT = 'p'; |
||
29 | |||
30 | /** |
||
31 | * Path to executable. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | private $pathToPgdump; |
||
36 | |||
37 | /** |
||
38 | * Host to connect to |
||
39 | * --host=<hostname> |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | private $host; |
||
44 | |||
45 | /** |
||
46 | * Port to connect to |
||
47 | * --port=<portnumber> |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | private $port; |
||
52 | |||
53 | /** |
||
54 | * User to connect with |
||
55 | * --user=<username> |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | private $user; |
||
60 | |||
61 | /** |
||
62 | * Password to authenticate with |
||
63 | * PGPASSWORD=<password> |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | private $password; |
||
68 | |||
69 | /** |
||
70 | * List of databases to backup |
||
71 | * --dbname string |
||
72 | * |
||
73 | * @var string |
||
74 | */ |
||
75 | private $database; |
||
76 | |||
77 | /** |
||
78 | * List of schemas to backup |
||
79 | * --schema=<schema> array of strings |
||
80 | * |
||
81 | * @var array |
||
82 | */ |
||
83 | private $schemas; |
||
84 | |||
85 | /** |
||
86 | * List of schemas to ignore |
||
87 | * --exclude-schema=<schema> |
||
88 | * |
||
89 | * @var array |
||
90 | */ |
||
91 | private $excludeSchemas; |
||
92 | |||
93 | /** |
||
94 | * List of tables to backup |
||
95 | * --table=<table> array of strings |
||
96 | * |
||
97 | * @var array |
||
98 | */ |
||
99 | private $tables; |
||
100 | |||
101 | /** |
||
102 | * List of tables to ignore |
||
103 | * --exclude-table=<table> |
||
104 | * |
||
105 | * @var array |
||
106 | */ |
||
107 | private $excludeTables; |
||
108 | |||
109 | /** |
||
110 | * List of tables where only the table structure is stored |
||
111 | * |
||
112 | * @var array |
||
113 | */ |
||
114 | private $excludeTableData; |
||
115 | |||
116 | /** |
||
117 | * Dump encoding |
||
118 | * --encoding |
||
119 | * |
||
120 | * @var string |
||
121 | */ |
||
122 | private $encoding; |
||
123 | |||
124 | /** |
||
125 | * Add drop statements. |
||
126 | * --clean |
||
127 | * |
||
128 | * @var boolean |
||
129 | */ |
||
130 | private $clean; |
||
131 | |||
132 | /** |
||
133 | * Dump no owner statement |
||
134 | * --no-owner |
||
135 | * |
||
136 | * @var bool |
||
137 | */ |
||
138 | private $noOwner; |
||
139 | |||
140 | /** |
||
141 | * Dump format |
||
142 | * --format=<format> |
||
143 | * |
||
144 | * @var bool |
||
145 | */ |
||
146 | private $format; |
||
147 | |||
148 | /** |
||
149 | * Dump only table structures |
||
150 | * --schema-only |
||
151 | * |
||
152 | * @var bool |
||
153 | */ |
||
154 | private $schemaOnly; |
||
155 | |||
156 | /** |
||
157 | * Dump only table data |
||
158 | * --data-only |
||
159 | * |
||
160 | * @var bool |
||
161 | */ |
||
162 | private $dataOnly; |
||
163 | |||
164 | /** |
||
165 | * Dump no privilege data |
||
166 | * --no-acl |
||
167 | * |
||
168 | * @var bool |
||
169 | */ |
||
170 | private $noPrivileges; |
||
171 | |||
172 | /** |
||
173 | * Setup. |
||
174 | * |
||
175 | * @see \phpbu\App\Backup\Source |
||
176 | * @param array $conf |
||
177 | * @throws \phpbu\App\Exception |
||
178 | */ |
||
179 | public function setup(array $conf = []) |
||
187 | |||
188 | /** |
||
189 | * Setup connection settings. |
||
190 | * |
||
191 | * @param array $conf |
||
192 | */ |
||
193 | View Code Duplication | private function setupConnection(array $conf) |
|
200 | |||
201 | /** |
||
202 | * Get tables and databases to backup. |
||
203 | * |
||
204 | * @param array $conf |
||
205 | */ |
||
206 | private function setupSourceData(array $conf) |
||
215 | |||
216 | /** |
||
217 | * Setup some dump options. |
||
218 | * |
||
219 | * @param array $conf |
||
220 | */ |
||
221 | private function setupDumpOptions(array $conf) |
||
231 | |||
232 | |||
233 | /** |
||
234 | * Execute the backup. |
||
235 | * |
||
236 | * @see \phpbu\App\Backup\Source |
||
237 | * @param \phpbu\App\Backup\Target $target |
||
238 | * @param \phpbu\App\Result $result |
||
239 | * @return \phpbu\App\Backup\Source\Status |
||
240 | * @throws \phpbu\App\Exception |
||
241 | */ |
||
242 | public function backup(Target $target, Result $result) |
||
254 | |||
255 | /** |
||
256 | * Create the Executable to run the mysqldump command. |
||
257 | * |
||
258 | * @param \phpbu\App\Backup\Target $target |
||
259 | * @return \phpbu\App\Cli\Executable |
||
260 | */ |
||
261 | public function getExecutable(Target $target) |
||
283 | |||
284 | /** |
||
285 | * Create backup status. |
||
286 | * |
||
287 | * @param \phpbu\App\Backup\Target |
||
288 | * @return \phpbu\App\Backup\Source\Status |
||
289 | */ |
||
290 | protected function createStatus(Target $target) |
||
294 | } |
||
295 |
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.