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 classes like SmartDbTable 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 SmartDbTable, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
66 | class SmartDbTable |
||
67 | { |
||
68 | /** |
||
69 | * @var string $_name name of the table |
||
70 | */ |
||
71 | private $_name; |
||
72 | |||
73 | /** |
||
74 | * @var string $_structure structure of the table |
||
75 | */ |
||
76 | private $_structure; |
||
77 | |||
78 | /** |
||
79 | * @var array $_data containing valued of each records to be added |
||
80 | */ |
||
81 | private $_data; |
||
82 | |||
83 | /** |
||
84 | * @var array $_alteredFields containing fields to be altered |
||
85 | */ |
||
86 | private $_alteredFields; |
||
87 | |||
88 | /** |
||
89 | * @var array $_newFields containing new fields to be added |
||
90 | */ |
||
91 | private $_newFields; |
||
92 | |||
93 | /** |
||
94 | * @var array $_droppedFields containing fields to be dropped |
||
95 | */ |
||
96 | private $_droppedFields; |
||
97 | |||
98 | /** |
||
99 | * @var array $_flagForDrop flag table to drop it |
||
100 | */ |
||
101 | private $_flagForDrop = false; |
||
102 | |||
103 | /** |
||
104 | * @var array $_updatedFields containing fields which values will be updated |
||
105 | */ |
||
106 | private $_updatedFields; |
||
107 | |||
108 | /** |
||
109 | * @var array $_updatedFields containing fields which values will be updated |
||
110 | */ //felix |
||
111 | private $_updatedWhere; |
||
112 | |||
113 | /** |
||
114 | * Constructor |
||
115 | * |
||
116 | * @param string $name name of the table |
||
117 | * |
||
118 | */ |
||
119 | public function __construct($name) |
||
124 | |||
125 | /** |
||
126 | * Return the table name, prefixed with site table prefix |
||
127 | * |
||
128 | * @return string table name |
||
129 | * |
||
130 | */ |
||
131 | public function name() |
||
137 | |||
138 | /** |
||
139 | * Checks if the table already exists in the database |
||
140 | * |
||
141 | * @return bool TRUE if it exists, FALSE if not |
||
142 | * |
||
143 | */ |
||
144 | public function exists() |
||
148 | |||
149 | /** |
||
150 | * @return mixed |
||
151 | */ |
||
152 | public function getExistingFieldsArray() |
||
168 | |||
169 | /** |
||
170 | * @param $field |
||
171 | * @return bool |
||
172 | */ |
||
173 | public function fieldExists($field) |
||
179 | |||
180 | /** |
||
181 | * Set the table structure |
||
182 | * |
||
183 | * @param string $structure table structure |
||
184 | * |
||
185 | */ |
||
186 | public function setStructure($structure) |
||
190 | |||
191 | /** |
||
192 | * Return the table structure |
||
193 | * |
||
194 | * @return string table structure |
||
195 | * |
||
196 | */ |
||
197 | public function getStructure() |
||
201 | |||
202 | /** |
||
203 | * Add values of a record to be added |
||
204 | * |
||
205 | * @param string $data values of a record |
||
206 | * |
||
207 | */ |
||
208 | public function setData($data) |
||
212 | |||
213 | /** |
||
214 | * Get the data array |
||
215 | * |
||
216 | * @return array containing the records values to be added |
||
217 | * |
||
218 | */ |
||
219 | public function getData() |
||
223 | |||
224 | /** |
||
225 | * Use to insert data in a table |
||
226 | * |
||
227 | * @return bool true if success, false if an error occured |
||
228 | * |
||
229 | */ |
||
230 | public function addData() |
||
246 | |||
247 | /** |
||
248 | * Add a field to be added |
||
249 | * |
||
250 | * @param string $name name of the field |
||
251 | * @param string $properties properties of the field |
||
252 | * @param bool $showerror |
||
253 | */ |
||
254 | public function addAlteredField($name, $properties, $showerror = true) |
||
261 | |||
262 | /** |
||
263 | * Invert values 0 to 1 and 1 to 0 |
||
264 | * |
||
265 | * @param string $name name of the field |
||
266 | * @param $newValue |
||
267 | * @param $oldValue |
||
268 | * @internal param string $old old propertie |
||
269 | * @internal param string $new new propertie |
||
270 | */ //felix |
||
271 | public function addUpdatedWhere($name, $newValue, $oldValue) |
||
278 | |||
279 | /** |
||
280 | * Add new field of a record to be added |
||
281 | * |
||
282 | * @param string $name name of the field |
||
283 | * @param string $properties properties of the field |
||
284 | * |
||
285 | */ |
||
286 | public function addNewField($name, $properties) |
||
292 | |||
293 | /** |
||
294 | * Get fields that need to be altered |
||
295 | * |
||
296 | * @return array fields that need to be altered |
||
297 | * |
||
298 | */ |
||
299 | public function getAlteredFields() |
||
303 | |||
304 | /** |
||
305 | * Add field for which the value will be updated |
||
306 | * |
||
307 | * @param string $name name of the field |
||
308 | * @param string $value value to be set |
||
309 | * |
||
310 | */ |
||
311 | public function addUpdatedField($name, $value) |
||
317 | |||
318 | /** |
||
319 | * Get new fields to be added |
||
320 | * |
||
321 | * @return array fields to be added |
||
322 | * |
||
323 | */ |
||
324 | public function getNewFields() |
||
328 | |||
329 | /** |
||
330 | * Get fields which values need to be updated |
||
331 | * |
||
332 | * @return array fields which values need to be updated |
||
333 | * |
||
334 | */ |
||
335 | public function getUpdatedFields() |
||
339 | |||
340 | /** |
||
341 | * Get fields which values need to be updated |
||
342 | * |
||
343 | * @return array fields which values need to be updated |
||
344 | * |
||
345 | */ //felix |
||
346 | public function getUpdatedWhere() |
||
350 | |||
351 | /** |
||
352 | * Add values of a record to be added |
||
353 | * |
||
354 | * @param string $name name of the field |
||
355 | * |
||
356 | */ |
||
357 | public function addDroppedField($name) |
||
361 | |||
362 | /** |
||
363 | * Get fields that need to be dropped |
||
364 | * |
||
365 | * @return array fields that need to be dropped |
||
366 | * |
||
367 | */ |
||
368 | public function getDroppedFields() |
||
372 | |||
373 | /** |
||
374 | * Set the flag to drop the table |
||
375 | * |
||
376 | */ |
||
377 | public function setFlagForDrop() |
||
381 | |||
382 | /** |
||
383 | * Use to create a table |
||
384 | * |
||
385 | * @return bool true if success, false if an error occured |
||
386 | * |
||
387 | */ |
||
388 | public function createTable() |
||
403 | |||
404 | /** |
||
405 | * Use to drop a table |
||
406 | * |
||
407 | * @return bool true if success, false if an error occured |
||
408 | * |
||
409 | */ |
||
410 | public function dropTable() |
||
426 | |||
427 | /** |
||
428 | * Use to alter a table |
||
429 | * |
||
430 | * @return bool true if success, false if an error occured |
||
431 | * |
||
432 | */ |
||
433 | public function alterTable() |
||
454 | |||
455 | /** |
||
456 | * Use to add new fileds in the table |
||
457 | * |
||
458 | * @return bool true if success, false if an error occured |
||
459 | * |
||
460 | */ |
||
461 | public function addNewFields() |
||
479 | |||
480 | /** |
||
481 | * Use to update fields values |
||
482 | * |
||
483 | * @return bool true if success, false if an error occured |
||
484 | * |
||
485 | */ |
||
486 | public function updateFieldsValues() |
||
504 | /** |
||
505 | * Use to update fields values |
||
506 | * |
||
507 | * @return bool true if success, false if an error occured |
||
508 | * |
||
509 | */ //felix |
||
510 | public function updateWhereValues() |
||
529 | |||
530 | /** |
||
531 | * Use to drop fields |
||
532 | * |
||
533 | * @return bool true if success, false if an error occured |
||
534 | * |
||
535 | */ |
||
536 | public function dropFields() |
||
555 | } |
||
556 | |||
686 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.