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 AbstractTDBMObject 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 AbstractTDBMObject, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | abstract class AbstractTDBMObject implements \JsonSerializable, FilterInterface { |
||
33 | |||
34 | /** |
||
35 | * The service this object is bound to. |
||
36 | * |
||
37 | * @var TDBMService |
||
38 | */ |
||
39 | protected $tdbmService; |
||
40 | |||
41 | /** |
||
42 | * An array of DbRow, indexed by table name. |
||
43 | * @var DbRow[] |
||
44 | */ |
||
45 | protected $dbRows = array(); |
||
46 | |||
47 | /** |
||
48 | * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
||
49 | * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
||
50 | * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
||
51 | * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | private $status; |
||
56 | |||
57 | /** |
||
58 | * True if an error has occurred while saving. The user will have to call save() explicitly or to modify one of its members to save it again. |
||
59 | * TODO: hide this with getters and setters |
||
60 | * |
||
61 | * @var boolean |
||
62 | */ |
||
63 | public $db_onerror; |
||
64 | |||
65 | private $db_connection; |
||
66 | |||
67 | /** |
||
68 | * True to automatically save the object. |
||
69 | * If false, the user must explicitly call the save() method to save the object. |
||
70 | * TODO: hide this with getters and setters |
||
71 | * |
||
72 | * @var boolean |
||
73 | */ |
||
74 | public $db_autosave; |
||
75 | |||
76 | /** |
||
77 | * Array storing beans related via many to many relationships (pivot tables) |
||
78 | * @var \SplObjectStorage[] Key: pivot table name, value: SplObjectStorage |
||
79 | */ |
||
80 | private $relationships = []; |
||
81 | |||
82 | /** |
||
83 | * |
||
84 | * @var bool[] Key: pivot table name, value: whether a query was performed to load the data. |
||
85 | */ |
||
86 | private $loadedRelationships = []; |
||
87 | |||
88 | /** |
||
89 | * Used with $primaryKeys when we want to retrieve an existing object |
||
90 | * and $primaryKeys=[] if we want a new object |
||
91 | * |
||
92 | * @param string $tableName |
||
93 | * @param array $primaryKeys |
||
94 | * @param TDBMService $tdbmService |
||
95 | * @throws TDBMException |
||
96 | * @throws TDBMInvalidOperationException |
||
97 | */ |
||
98 | public function __construct($tableName=null, array $primaryKeys=array(), TDBMService $tdbmService=null) { |
||
115 | |||
116 | /** |
||
117 | * Alternative constructor called when data is fetched from database via a SELECT. |
||
118 | * |
||
119 | * @param array $beanData array<table, array<column, value>> |
||
120 | * @param TDBMService $tdbmService |
||
121 | */ |
||
122 | public function _constructFromData(array $beanData, TDBMService $tdbmService) { |
||
131 | |||
132 | /** |
||
133 | * Alternative constructor called when bean is lazily loaded. |
||
134 | * |
||
135 | * @param string $tableName |
||
136 | * @param array $primaryKeys |
||
137 | * @param TDBMService $tdbmService |
||
138 | */ |
||
139 | public function _constructLazy($tableName, array $primaryKeys, TDBMService $tdbmService) { |
||
146 | |||
147 | public function _attach(TDBMService $tdbmService) { |
||
173 | |||
174 | /** |
||
175 | * Returns true if the object will save automatically, false if an explicit call to save() is required. |
||
176 | * |
||
177 | * @return boolean |
||
178 | */ |
||
179 | public function getAutoSaveMode() { |
||
182 | |||
183 | /** |
||
184 | * Sets the autosave mode: |
||
185 | * true if the object will save automatically, |
||
186 | * false if an explicit call to save() is required. |
||
187 | * |
||
188 | * @param boolean $autoSave |
||
189 | */ |
||
190 | public function setAutoSaveMode($autoSave) { |
||
193 | |||
194 | /** |
||
195 | * Sets the state of the TDBM Object |
||
196 | * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
||
197 | * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
||
198 | * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
||
199 | * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
||
200 | * @param string $state |
||
201 | */ |
||
202 | public function _setStatus($state){ |
||
210 | |||
211 | View Code Duplication | public function get($var, $tableName = null) { |
|
230 | |||
231 | /** |
||
232 | * Returns true if a column is set, false otherwise. |
||
233 | * |
||
234 | * @param string $var |
||
235 | * @return boolean |
||
236 | */ |
||
237 | View Code Duplication | public function has($var, $tableName = null) { |
|
256 | |||
257 | View Code Duplication | public function set($var, $value, $tableName = null) { |
|
277 | |||
278 | /** |
||
279 | * @param string $foreignKeyName |
||
280 | * @param AbstractTDBMObject $bean |
||
281 | */ |
||
282 | View Code Duplication | public function setRef($foreignKeyName, AbstractTDBMObject $bean, $tableName = null) { |
|
302 | |||
303 | /** |
||
304 | * @param string $foreignKeyName A unique name for this reference |
||
305 | * @return AbstractTDBMObject|null |
||
306 | */ |
||
307 | View Code Duplication | public function getRef($foreignKeyName, $tableName = null) { |
|
326 | |||
327 | /** |
||
328 | * Adds a many to many relationship to this bean. |
||
329 | * @param string $pivotTableName |
||
330 | * @param AbstractTDBMObject $remoteBean |
||
331 | */ |
||
332 | protected function addRelationship($pivotTableName, AbstractTDBMObject $remoteBean) { |
||
335 | |||
336 | /** |
||
337 | * Returns true if there is a relationship to this bean. |
||
338 | * @param string $pivotTableName |
||
339 | * @param AbstractTDBMObject $remoteBean |
||
340 | * @return bool |
||
341 | */ |
||
342 | protected function hasRelationship($pivotTableName, AbstractTDBMObject $remoteBean) { |
||
352 | |||
353 | /** |
||
354 | * Internal TDBM method. Removes a many to many relationship from this bean. |
||
355 | * @param string $pivotTableName |
||
356 | * @param AbstractTDBMObject $remoteBean |
||
357 | */ |
||
358 | public function _removeRelationship($pivotTableName, AbstractTDBMObject $remoteBean) { |
||
366 | |||
367 | /** |
||
368 | * Returns the list of objects linked to this bean via $pivotTableName |
||
369 | * @param $pivotTableName |
||
370 | * @return \SplObjectStorage |
||
371 | */ |
||
372 | private function retrieveRelationshipsStorage($pivotTableName) { |
||
395 | |||
396 | /** |
||
397 | * Internal TDBM method. Returns the list of objects linked to this bean via $pivotTableName |
||
398 | * @param $pivotTableName |
||
399 | * @return AbstractTDBMObject[] |
||
400 | */ |
||
401 | public function _getRelationships($pivotTableName) { |
||
404 | |||
405 | private function relationshipStorageToArray(\SplObjectStorage $storage) { |
||
415 | |||
416 | /** |
||
417 | * Declares a relationship between |
||
418 | * @param string $pivotTableName |
||
419 | * @param AbstractTDBMObject $remoteBean |
||
420 | * @param string $status |
||
421 | */ |
||
422 | private function setRelationship($pivotTableName, AbstractTDBMObject $remoteBean, $status) { |
||
432 | |||
433 | /** |
||
434 | * Returns the SplObjectStorage associated to this relationship (creates it if it does not exists) |
||
435 | * @param $pivotTableName |
||
436 | * @return \SplObjectStorage |
||
437 | */ |
||
438 | private function getRelationshipStorage($pivotTableName) { |
||
447 | |||
448 | /*public function __destruct() { |
||
449 | // In a destructor, no exception can be thrown (PHP 5 limitation) |
||
450 | // So we print the error instead |
||
451 | try { |
||
452 | if (!$this->db_onerror && $this->db_autosave) |
||
453 | { |
||
454 | $this->save(); |
||
455 | } |
||
456 | } catch (\Exception $e) { |
||
457 | trigger_error($e->getMessage(), E_USER_ERROR); |
||
458 | } |
||
459 | }*/ |
||
460 | |||
461 | |||
462 | /** |
||
463 | * Reverts any changes made to the object and resumes it to its DB state. |
||
464 | * This can only be called on objects that come from database and that have not been deleted. |
||
465 | * Otherwise, this will throw an exception. |
||
466 | * |
||
467 | */ |
||
468 | public function discardChanges() { |
||
479 | |||
480 | /** |
||
481 | * Method used internally by TDBM. You should not use it directly. |
||
482 | * This method returns the status of the TDBMObject. |
||
483 | * This is one of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
||
484 | * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
||
485 | * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
||
486 | * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
||
487 | * |
||
488 | * @return string |
||
489 | */ |
||
490 | public function _getStatus() { |
||
493 | |||
494 | |||
495 | /** |
||
496 | * Implement the unique JsonSerializable method |
||
497 | * @return array |
||
498 | */ |
||
499 | public function jsonSerialize(){ |
||
504 | |||
505 | /** |
||
506 | * Returns the SQL of the filter (the SQL WHERE clause). |
||
507 | * |
||
508 | * @param Connection $dbConnection |
||
509 | * @return string |
||
510 | */ |
||
511 | public function toSql(Connection $dbConnection) { |
||
514 | |||
515 | /** |
||
516 | * Returns the tables used in the filter in an array. |
||
517 | * |
||
518 | * @return array<string> |
||
519 | */ |
||
520 | public function getUsedTables() { |
||
523 | |||
524 | /** |
||
525 | * Returns Where statement to query this object |
||
526 | * |
||
527 | * @return string |
||
528 | */ |
||
529 | private function getPrimaryKeyWhereStatement () { |
||
549 | |||
550 | /** |
||
551 | * Override the native php clone function for TDBMObjects |
||
552 | */ |
||
553 | View Code Duplication | public function __clone(){ |
|
567 | |||
568 | /** |
||
569 | * Returns raw database rows. |
||
570 | * |
||
571 | * @return DbRow[] Key: table name, Value: DbRow object |
||
572 | */ |
||
573 | public function _getDbRows() { |
||
576 | |||
577 | private function registerTable($tableName) { |
||
594 | |||
595 | /** |
||
596 | * Internal function: return the list of relationships |
||
597 | * @return \SplObjectStorage[] |
||
598 | */ |
||
599 | public function _getCachedRelationships() { |
||
602 | } |
||
603 |
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.