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 rowEditor 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 rowEditor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class rowEditor |
||
10 | { |
||
11 | public $sTable; |
||
12 | public $sAutoIncrementField = null; |
||
13 | public $pk; // (idx:name; type, default, nullable, value, insertfunction) |
||
14 | public $fields; // (idx:name; type, default, nullable, value, changed, insertfunction) |
||
15 | |||
16 | // status var |
||
17 | public $bLoaded = false; |
||
18 | public $bExist = false; |
||
19 | public $bAddNew = false; |
||
20 | |||
21 | /** |
||
22 | * primaryKey may be an array |
||
23 | * |
||
24 | * @param string $sTable |
||
25 | */ |
||
26 | public function __construct($sTable) |
||
32 | |||
33 | /** |
||
34 | * @param string $sField |
||
35 | * @param $nDefault |
||
36 | * @param bool $bNullable |
||
37 | * @param int $nInsertFunction |
||
38 | */ |
||
39 | public function addPKInt($sField, $nDefault, $bNullable, $nInsertFunction = RE_INSERT_NOTHING): void |
||
53 | |||
54 | /** |
||
55 | * @param $sField |
||
56 | * @param $nDefault |
||
57 | * @param $bNullable |
||
58 | * @param int $nInsertFunction |
||
59 | */ |
||
60 | View Code Duplication | public function addPKFloat($sField, $nDefault, $bNullable, $nInsertFunction = RE_INSERT_NOTHING): void |
|
70 | |||
71 | /** |
||
72 | * @param $sField |
||
73 | * @param $nDefault |
||
74 | * @param $bNullable |
||
75 | * @param int $nInsertFunction |
||
76 | */ |
||
77 | View Code Duplication | public function addPKDouble($sField, $nDefault, $bNullable, $nInsertFunction = RE_INSERT_NOTHING): void |
|
87 | |||
88 | /** |
||
89 | * @param $sField |
||
90 | * @param $sDefault |
||
91 | * @param $bNullable |
||
92 | * @param int $nInsertFunction |
||
93 | */ |
||
94 | public function addPKString($sField, $sDefault, $bNullable, $nInsertFunction = RE_INSERT_NOTHING): void |
||
108 | |||
109 | /** |
||
110 | * @param $sField |
||
111 | * @param $bDefault |
||
112 | * @param $bNullable |
||
113 | * @param int $nInsertFunction |
||
114 | */ |
||
115 | View Code Duplication | public function addPKBoolean($sField, $bDefault, $bNullable, $nInsertFunction = RE_INSERT_NOTHING): void |
|
125 | |||
126 | /** |
||
127 | * @param $sField |
||
128 | * @param $dDefault |
||
129 | * @param $bNullable |
||
130 | * @param int $nInsertFunction |
||
131 | */ |
||
132 | View Code Duplication | public function addPKDate($sField, $dDefault, $bNullable, $nInsertFunction = RE_INSERT_NOTHING): void |
|
142 | |||
143 | /** |
||
144 | * @param string $sField |
||
145 | * @param $nDefault |
||
146 | * @param bool $bNullable |
||
147 | * @param int $nInsertFunction |
||
148 | */ |
||
149 | View Code Duplication | public function addInt($sField, $nDefault, $bNullable, $nInsertFunction = RE_INSERT_NOTHING): void |
|
160 | |||
161 | /** |
||
162 | * @param string $sField |
||
163 | * @param int $nDefault |
||
164 | * @param bool $bNullable |
||
165 | * @param int $nInsertFunction |
||
166 | */ |
||
167 | View Code Duplication | public function addFloat($sField, $nDefault, $bNullable, $nInsertFunction = RE_INSERT_NOTHING): void |
|
178 | |||
179 | /** |
||
180 | * @param string $sField |
||
181 | * @param int $nDefault |
||
182 | * @param bool $bNullable |
||
183 | * @param int $nInsertFunction |
||
184 | */ |
||
185 | View Code Duplication | public function addDouble($sField, $nDefault, $bNullable, $nInsertFunction = RE_INSERT_NOTHING): void |
|
196 | |||
197 | /** |
||
198 | * @param string $sField |
||
199 | * @param $sDefault |
||
200 | * @param bool $bNullable |
||
201 | * @param int $nInsertFunction |
||
202 | */ |
||
203 | View Code Duplication | public function addString($sField, $sDefault, $bNullable, $nInsertFunction = RE_INSERT_NOTHING): void |
|
214 | |||
215 | /** |
||
216 | * @param string $sField |
||
217 | * @param $bDefault |
||
218 | * @param bool $bNullable |
||
219 | * @param int $nInsertFunction |
||
220 | */ |
||
221 | View Code Duplication | public function addBoolean($sField, $bDefault, $bNullable, $nInsertFunction = RE_INSERT_NOTHING): void |
|
232 | |||
233 | /** |
||
234 | * @param string $sField |
||
235 | * @param $dDefault |
||
236 | * @param bool $bNullable |
||
237 | * @param int $nInsertFunction |
||
238 | */ |
||
239 | View Code Duplication | public function addDate($sField, $dDefault, $bNullable, $nInsertFunction = RE_INSERT_NOTHING): void |
|
250 | |||
251 | /** |
||
252 | * @param $sField |
||
253 | */ |
||
254 | public function removePK($sField): void |
||
258 | |||
259 | /** |
||
260 | * @param $sField |
||
261 | */ |
||
262 | public function removeField($sField): void |
||
266 | |||
267 | /* |
||
268 | * PKValues may be an string, indized or ordered array |
||
269 | * |
||
270 | * @param $PKValues |
||
271 | * |
||
272 | * @return bool |
||
273 | */ |
||
274 | public function load($PKValues) |
||
299 | |||
300 | /** |
||
301 | * @param $PKValues |
||
302 | */ |
||
303 | public function addNew($PKValues): void |
||
311 | |||
312 | /** |
||
313 | * @return bool |
||
314 | */ |
||
315 | public function exist() |
||
319 | |||
320 | /** |
||
321 | * @param mixed $PKValues |
||
322 | */ |
||
323 | public function pSetPK($PKValues): void |
||
341 | |||
342 | /** |
||
343 | * @param $index |
||
344 | * |
||
345 | * @return int|string |
||
346 | */ |
||
347 | public function pGetPKKey($index) |
||
361 | |||
362 | public function pResetValues(): void |
||
369 | |||
370 | /** |
||
371 | * @param $type |
||
372 | * @param $value |
||
373 | * |
||
374 | * @return bool|false|int|null |
||
375 | */ |
||
376 | public function pFormatValue($type, $value) |
||
398 | |||
399 | /** |
||
400 | * @param $type |
||
401 | * @param $value |
||
402 | * |
||
403 | * @return false|int|string |
||
404 | */ |
||
405 | public function pFormatValueSql($type, $value) |
||
425 | |||
426 | /** |
||
427 | * @return string |
||
428 | */ |
||
429 | public function pBuildSelect() |
||
443 | |||
444 | /** |
||
445 | * @return string |
||
446 | */ |
||
447 | public function pBuildPK() |
||
460 | |||
461 | /** |
||
462 | * @param string $sField |
||
463 | * |
||
464 | * @return mixed |
||
465 | */ |
||
466 | public function getValue($sField) |
||
474 | |||
475 | /** |
||
476 | * @param $sField |
||
477 | * |
||
478 | * @return mixed |
||
479 | */ |
||
480 | public function getDefault($sField) |
||
484 | |||
485 | /** |
||
486 | * @param string $sField |
||
487 | * |
||
488 | * @return mixed |
||
489 | */ |
||
490 | public function getChanged($sField) |
||
494 | |||
495 | /** |
||
496 | * @return bool|null |
||
497 | */ |
||
498 | public function getAnyChanged() |
||
506 | |||
507 | /** |
||
508 | * @param string $sField |
||
509 | * @param $sValue |
||
510 | * @return bool |
||
511 | */ |
||
512 | public function setValue($sField, $sValue) |
||
534 | |||
535 | /** |
||
536 | * @return bool |
||
537 | */ |
||
538 | public function save() |
||
597 | |||
598 | public function reload(): void |
||
606 | |||
607 | /** |
||
608 | * @return string |
||
609 | */ |
||
610 | public function pBuildInsert() |
||
675 | |||
676 | /** |
||
677 | * @return string |
||
678 | */ |
||
679 | public function pBuildUpdate() |
||
708 | |||
709 | /** |
||
710 | * @param string $field |
||
711 | * |
||
712 | * @return bool |
||
713 | */ |
||
714 | public function saveField($field) |
||
743 | } |
||
744 |
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.