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 DbTable 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 DbTable, and based on these observations, apply Extract Interface, too.
1 | <?php namespace XoopsModules\Smartobject; |
||
56 | class DbTable |
||
57 | { |
||
58 | /** |
||
59 | * @var string $_name name of the table |
||
60 | */ |
||
61 | public $_name; |
||
62 | /** |
||
63 | * @var string $_structure structure of the table |
||
64 | */ |
||
65 | public $_structure; |
||
66 | |||
67 | /** |
||
68 | * @var array $_data containing valued of each records to be added |
||
69 | */ |
||
70 | public $_data; |
||
71 | |||
72 | /** |
||
73 | * @var array $_alteredFields containing fields to be altered |
||
74 | */ |
||
75 | public $_alteredFields; |
||
76 | |||
77 | /** |
||
78 | * @var array $_newFields containing new fields to be added |
||
79 | */ |
||
80 | public $_newFields; |
||
81 | |||
82 | /** |
||
83 | * @var array $_droppedFields containing fields to be dropped |
||
84 | */ |
||
85 | public $_droppedFields; |
||
86 | |||
87 | /** |
||
88 | * @var array $_flagForDrop flag table to drop it |
||
89 | */ |
||
90 | public $_flagForDrop = false; |
||
91 | |||
92 | /** |
||
93 | * @var array $_updatedFields containing fields which values will be updated |
||
94 | */ |
||
95 | public $_updatedFields; |
||
96 | |||
97 | /** |
||
98 | * @var array $_updatedFields containing fields which values will be updated |
||
99 | */ //felix |
||
100 | public $_updatedWhere; |
||
101 | |||
102 | public $_existingFieldsArray = false; |
||
103 | |||
104 | /** |
||
105 | * Constructor |
||
106 | * |
||
107 | * @param string $name name of the table |
||
108 | * |
||
109 | */ |
||
110 | public function __construct($name) |
||
115 | |||
116 | /** |
||
117 | * Return the table name, prefixed with site table prefix |
||
118 | * |
||
119 | * @return string table name |
||
120 | * |
||
121 | */ |
||
122 | public function name() |
||
128 | |||
129 | /** |
||
130 | * Checks if the table already exists in the database |
||
131 | * |
||
132 | * @return bool TRUE if it exists, FALSE if not |
||
133 | * |
||
134 | */ |
||
135 | public function exists() |
||
139 | |||
140 | /** |
||
141 | * @return mixed |
||
142 | */ |
||
143 | public function getExistingFieldsArray() |
||
165 | |||
166 | /** |
||
167 | * @param $field |
||
168 | * @return bool |
||
169 | */ |
||
170 | public function fieldExists($field) |
||
176 | |||
177 | /** |
||
178 | * Set the table structure |
||
179 | * |
||
180 | * Example: |
||
181 | * |
||
182 | * $table->setStructure("`transactionid` int(11) NOT NULL auto_increment, |
||
183 | * `date` int(11) NOT NULL default '0', |
||
184 | * `status` int(1) NOT NULL default '-1', |
||
185 | * `itemid` int(11) NOT NULL default '0', |
||
186 | * `uid` int(11) NOT NULL default '0', |
||
187 | * `price` float NOT NULL default '0', |
||
188 | * `currency` varchar(100) NOT NULL default '', |
||
189 | * PRIMARY KEY (`transactionid`)"); |
||
190 | * |
||
191 | * @param string $structure table structure |
||
192 | * |
||
193 | */ |
||
194 | public function setStructure($structure) |
||
198 | |||
199 | /** |
||
200 | * Return the table structure |
||
201 | * |
||
202 | * @return string table structure |
||
203 | * |
||
204 | */ |
||
205 | public function getStructure() |
||
209 | |||
210 | /** |
||
211 | * Add values of a record to be added |
||
212 | * |
||
213 | * @param string $data values of a record |
||
214 | * |
||
215 | */ |
||
216 | public function setData($data) |
||
220 | |||
221 | /** |
||
222 | * Get the data array |
||
223 | * |
||
224 | * @return array containing the records values to be added |
||
225 | * |
||
226 | */ |
||
227 | public function getData() |
||
231 | |||
232 | /** |
||
233 | * Use to insert data in a table |
||
234 | * |
||
235 | * @return bool true if success, false if an error occured |
||
236 | * |
||
237 | */ |
||
238 | public function addData() |
||
253 | |||
254 | /** |
||
255 | * Add a field to be added |
||
256 | * |
||
257 | * @param string $name name of the field |
||
258 | * @param string $properties properties of the field |
||
259 | * @param bool $newname |
||
260 | * @param bool $showerror |
||
261 | */ |
||
262 | public function addAlteredField($name, $properties, $newname = false, $showerror = true) |
||
270 | /** |
||
271 | * Invert values 0 to 1 and 1 to 0 |
||
272 | * |
||
273 | * @param string $name name of the field |
||
274 | * @param $newValue |
||
275 | * @param $oldValue |
||
276 | * @internal param string $old old propertie |
||
277 | * @internal param string $new new propertie |
||
278 | */ //felix |
||
279 | public function addUpdatedWhere($name, $newValue, $oldValue) |
||
286 | |||
287 | /** |
||
288 | * Add new field of a record to be added |
||
289 | * |
||
290 | * @param string $name name of the field |
||
291 | * @param string $properties properties of the field |
||
292 | * |
||
293 | */ |
||
294 | public function addNewField($name, $properties) |
||
300 | |||
301 | /** |
||
302 | * Get fields that need to be altered |
||
303 | * |
||
304 | * @return array fields that need to be altered |
||
305 | * |
||
306 | */ |
||
307 | public function getAlteredFields() |
||
311 | |||
312 | /** |
||
313 | * Add field for which the value will be updated |
||
314 | * |
||
315 | * @param string $name name of the field |
||
316 | * @param string $value value to be set |
||
317 | * |
||
318 | */ |
||
319 | public function addUpdatedField($name, $value) |
||
325 | |||
326 | /** |
||
327 | * Get new fields to be added |
||
328 | * |
||
329 | * @return array fields to be added |
||
330 | * |
||
331 | */ |
||
332 | public function getNewFields() |
||
336 | |||
337 | /** |
||
338 | * Get fields which values need to be updated |
||
339 | * |
||
340 | * @return array fields which values need to be updated |
||
341 | * |
||
342 | */ |
||
343 | public function getUpdatedFields() |
||
347 | /** |
||
348 | * Get fields which values need to be updated |
||
349 | * |
||
350 | * @return array fields which values need to be updated |
||
351 | * |
||
352 | */ //felix |
||
353 | public function getUpdatedWhere() |
||
357 | |||
358 | /** |
||
359 | * Add values of a record to be added |
||
360 | * |
||
361 | * @param string $name name of the field |
||
362 | * |
||
363 | */ |
||
364 | public function addDroppedField($name) |
||
368 | |||
369 | /** |
||
370 | * Get fields that need to be dropped |
||
371 | * |
||
372 | * @return array fields that need to be dropped |
||
373 | * |
||
374 | */ |
||
375 | public function getDroppedFields() |
||
379 | |||
380 | /** |
||
381 | * Set the flag to drop the table |
||
382 | * |
||
383 | */ |
||
384 | public function setFlagForDrop() |
||
388 | |||
389 | /** |
||
390 | * Use to create a table |
||
391 | * |
||
392 | * @return bool true if success, false if an error occured |
||
393 | * |
||
394 | */ |
||
395 | public function createTable() |
||
410 | |||
411 | /** |
||
412 | * Use to drop a table |
||
413 | * |
||
414 | * @return bool true if success, false if an error occured |
||
415 | * |
||
416 | */ |
||
417 | public function dropTable() |
||
432 | |||
433 | /** |
||
434 | * Use to alter a table |
||
435 | * |
||
436 | * @return bool true if success, false if an error occured |
||
437 | * |
||
438 | */ |
||
439 | public function alterTable() |
||
462 | |||
463 | /** |
||
464 | * Use to add new fileds in the table |
||
465 | * |
||
466 | * @return bool true if success, false if an error occured |
||
467 | * |
||
468 | */ |
||
469 | public function addNewFields() |
||
486 | |||
487 | /** |
||
488 | * Use to update fields values |
||
489 | * |
||
490 | * @return bool true if success, false if an error occured |
||
491 | * |
||
492 | */ |
||
493 | public function updateFieldsValues() |
||
509 | /** |
||
510 | * Use to update fields values |
||
511 | * |
||
512 | * @return bool true if success, false if an error occured |
||
513 | * |
||
514 | */ //felix |
||
515 | public function updateWhereValues() |
||
532 | |||
533 | /** |
||
534 | * Use to drop fields |
||
535 | * |
||
536 | * @return bool true if success, false if an error occured |
||
537 | * |
||
538 | */ |
||
539 | public function dropFields() |
||
555 | } |
||
556 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.