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 |
||
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 | * |
||
44 | * @var DbRow[] |
||
45 | */ |
||
46 | protected $dbRows = []; |
||
47 | |||
48 | /** |
||
49 | * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
||
50 | * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
||
51 | * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
||
52 | * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | private $status; |
||
57 | |||
58 | /** |
||
59 | * Array storing beans related via many to many relationships (pivot tables). |
||
60 | * |
||
61 | * @var \SplObjectStorage[] Key: pivot table name, value: SplObjectStorage |
||
62 | */ |
||
63 | private $relationships = []; |
||
64 | |||
65 | /** |
||
66 | * @var bool[] Key: pivot table name, value: whether a query was performed to load the data |
||
67 | */ |
||
68 | private $loadedRelationships = []; |
||
69 | |||
70 | /** |
||
71 | * Array storing beans related via many to one relationships (this bean is pointed by external beans). |
||
72 | * |
||
73 | * @var AlterableResultIterator[] Key: [external_table]___[external_column], value: SplObjectStorage |
||
74 | */ |
||
75 | private $manyToOneRelationships = []; |
||
76 | |||
77 | /** |
||
78 | * Used with $primaryKeys when we want to retrieve an existing object |
||
79 | * and $primaryKeys=[] if we want a new object. |
||
80 | * |
||
81 | * @param string $tableName |
||
82 | * @param array $primaryKeys |
||
83 | * @param TDBMService $tdbmService |
||
84 | * |
||
85 | * @throws TDBMException |
||
86 | * @throws TDBMInvalidOperationException |
||
87 | */ |
||
88 | public function __construct($tableName = null, array $primaryKeys = [], TDBMService $tdbmService = null) |
||
106 | |||
107 | /** |
||
108 | * Alternative constructor called when data is fetched from database via a SELECT. |
||
109 | * |
||
110 | * @param array $beanData array<table, array<column, value>> |
||
111 | * @param TDBMService $tdbmService |
||
112 | */ |
||
113 | public function _constructFromData(array $beanData, TDBMService $tdbmService) |
||
123 | |||
124 | /** |
||
125 | * Alternative constructor called when bean is lazily loaded. |
||
126 | * |
||
127 | * @param string $tableName |
||
128 | * @param array $primaryKeys |
||
129 | * @param TDBMService $tdbmService |
||
130 | */ |
||
131 | public function _constructLazy($tableName, array $primaryKeys, TDBMService $tdbmService) |
||
139 | |||
140 | public function _attach(TDBMService $tdbmService) |
||
165 | |||
166 | /** |
||
167 | * Sets the state of the TDBM Object |
||
168 | * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
||
169 | * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
||
170 | * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
||
171 | * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
||
172 | * |
||
173 | * @param string $state |
||
174 | */ |
||
175 | public function _setStatus($state) |
||
176 | { |
||
177 | $this->status = $state; |
||
178 | |||
179 | // TODO: we might ignore the loaded => dirty state here! dirty status comes from the db_row itself. |
||
180 | foreach ($this->dbRows as $dbRow) { |
||
181 | $dbRow->_setStatus($state); |
||
182 | } |
||
183 | |||
184 | if ($state === TDBMObjectStateEnum::STATE_DELETED) { |
||
185 | $this->onDelete(); |
||
186 | } |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Checks that $tableName is ok, or returns the only possible table name if "$tableName = null" |
||
191 | * or throws an error. |
||
192 | * |
||
193 | * @param string $tableName |
||
194 | * |
||
195 | * @return string |
||
196 | */ |
||
197 | private function checkTableName($tableName = null) |
||
198 | { |
||
199 | View Code Duplication | if ($tableName === null) { |
|
|
|||
200 | if (count($this->dbRows) > 1) { |
||
201 | throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
||
202 | } elseif (count($this->dbRows) === 1) { |
||
203 | $tableName = array_keys($this->dbRows)[0]; |
||
204 | } |
||
205 | } |
||
206 | |||
207 | return $tableName; |
||
208 | } |
||
209 | |||
210 | protected function get($var, $tableName = null) |
||
211 | { |
||
212 | $tableName = $this->checkTableName($tableName); |
||
213 | |||
214 | if (!isset($this->dbRows[$tableName])) { |
||
215 | return; |
||
216 | } |
||
217 | |||
218 | return $this->dbRows[$tableName]->get($var); |
||
219 | } |
||
220 | |||
221 | protected function set($var, $value, $tableName = null) |
||
222 | { |
||
223 | View Code Duplication | if ($tableName === null) { |
|
224 | if (count($this->dbRows) > 1) { |
||
225 | throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
||
226 | } elseif (count($this->dbRows) === 1) { |
||
227 | $tableName = array_keys($this->dbRows)[0]; |
||
228 | } else { |
||
229 | throw new TDBMException('Please specify a table for this object.'); |
||
230 | } |
||
231 | } |
||
232 | |||
233 | if (!isset($this->dbRows[$tableName])) { |
||
234 | $this->registerTable($tableName); |
||
235 | } |
||
236 | |||
237 | $this->dbRows[$tableName]->set($var, $value); |
||
238 | if ($this->dbRows[$tableName]->_getStatus() === TDBMObjectStateEnum::STATE_DIRTY) { |
||
239 | $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
||
240 | } |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * @param string $foreignKeyName |
||
245 | * @param AbstractTDBMObject $bean |
||
246 | */ |
||
247 | protected function setRef($foreignKeyName, AbstractTDBMObject $bean = null, $tableName = null) |
||
248 | { |
||
249 | View Code Duplication | if ($tableName === null) { |
|
250 | if (count($this->dbRows) > 1) { |
||
251 | throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
||
252 | } elseif (count($this->dbRows) === 1) { |
||
253 | $tableName = array_keys($this->dbRows)[0]; |
||
254 | } else { |
||
255 | throw new TDBMException('Please specify a table for this object.'); |
||
256 | } |
||
257 | } |
||
258 | |||
259 | if (!isset($this->dbRows[$tableName])) { |
||
260 | $this->registerTable($tableName); |
||
261 | } |
||
262 | |||
263 | $oldLinkedBean = $this->dbRows[$tableName]->getRef($foreignKeyName); |
||
264 | if ($oldLinkedBean !== null) { |
||
265 | $oldLinkedBean->removeManyToOneRelationship($tableName, $foreignKeyName, $this); |
||
266 | } |
||
267 | |||
268 | $this->dbRows[$tableName]->setRef($foreignKeyName, $bean); |
||
269 | if ($this->dbRows[$tableName]->_getStatus() === TDBMObjectStateEnum::STATE_DIRTY) { |
||
270 | $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
||
271 | } |
||
272 | |||
273 | if ($bean !== null) { |
||
274 | $bean->setManyToOneRelationship($tableName, $foreignKeyName, $this); |
||
275 | } |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * @param string $foreignKeyName A unique name for this reference |
||
280 | * |
||
281 | * @return AbstractTDBMObject|null |
||
282 | */ |
||
283 | protected function getRef($foreignKeyName, $tableName = null) |
||
289 | |||
290 | /** |
||
291 | * Adds a many to many relationship to this bean. |
||
292 | * |
||
293 | * @param string $pivotTableName |
||
294 | * @param AbstractTDBMObject $remoteBean |
||
295 | */ |
||
296 | protected function addRelationship($pivotTableName, AbstractTDBMObject $remoteBean) |
||
300 | |||
301 | /** |
||
302 | * Returns true if there is a relationship to this bean. |
||
303 | * |
||
304 | * @param string $pivotTableName |
||
305 | * @param AbstractTDBMObject $remoteBean |
||
306 | * |
||
307 | * @return bool |
||
308 | */ |
||
309 | protected function hasRelationship($pivotTableName, AbstractTDBMObject $remoteBean) |
||
321 | |||
322 | /** |
||
323 | * Internal TDBM method. Removes a many to many relationship from this bean. |
||
324 | * |
||
325 | * @param string $pivotTableName |
||
326 | * @param AbstractTDBMObject $remoteBean |
||
327 | */ |
||
328 | public function _removeRelationship($pivotTableName, AbstractTDBMObject $remoteBean) |
||
337 | |||
338 | /** |
||
339 | * Sets many to many relationships for this bean. |
||
340 | * Adds new relationships and removes unused ones. |
||
341 | * |
||
342 | * @param $pivotTableName |
||
343 | * @param array $remoteBeans |
||
344 | */ |
||
345 | protected function setRelationships($pivotTableName, array $remoteBeans) |
||
363 | |||
364 | /** |
||
365 | * Returns the list of objects linked to this bean via $pivotTableName. |
||
366 | * |
||
367 | * @param $pivotTableName |
||
368 | * |
||
369 | * @return \SplObjectStorage |
||
370 | */ |
||
371 | private function retrieveRelationshipsStorage($pivotTableName) |
||
394 | |||
395 | /** |
||
396 | * Internal TDBM method. Returns the list of objects linked to this bean via $pivotTableName. |
||
397 | * |
||
398 | * @param $pivotTableName |
||
399 | * |
||
400 | * @return AbstractTDBMObject[] |
||
401 | */ |
||
402 | public function _getRelationships($pivotTableName) |
||
406 | |||
407 | private function relationshipStorageToArray(\SplObjectStorage $storage) |
||
419 | |||
420 | /** |
||
421 | * Declares a relationship between. |
||
422 | * |
||
423 | * @param string $pivotTableName |
||
424 | * @param AbstractTDBMObject $remoteBean |
||
425 | * @param string $status |
||
426 | */ |
||
427 | private function setRelationship($pivotTableName, AbstractTDBMObject $remoteBean, $status) |
||
438 | |||
439 | /** |
||
440 | * Returns the SplObjectStorage associated to this relationship (creates it if it does not exists). |
||
441 | * |
||
442 | * @param string $pivotTableName |
||
443 | * |
||
444 | * @return \SplObjectStorage |
||
445 | */ |
||
446 | private function getRelationshipStorage(string $pivotTableName) : \SplObjectStorage |
||
450 | |||
451 | /** |
||
452 | * Returns the SplObjectStorage associated to this relationship (creates it if it does not exists). |
||
453 | * |
||
454 | * @param string $tableName |
||
455 | * @param string $foreignKeyName |
||
456 | * |
||
457 | * @return AlterableResultIterator |
||
458 | */ |
||
459 | private function getManyToOneAlterableResultIterator(string $tableName, string $foreignKeyName) : AlterableResultIterator |
||
465 | |||
466 | /** |
||
467 | * Declares a relationship between this bean and the bean pointing to it. |
||
468 | * |
||
469 | * @param string $tableName |
||
470 | * @param string $foreignKeyName |
||
471 | * @param AbstractTDBMObject $remoteBean |
||
472 | */ |
||
473 | private function setManyToOneRelationship(string $tableName, string $foreignKeyName, AbstractTDBMObject $remoteBean) |
||
478 | |||
479 | /** |
||
480 | * Declares a relationship between this bean and the bean pointing to it. |
||
481 | * |
||
482 | * @param string $tableName |
||
483 | * @param string $foreignKeyName |
||
484 | * @param AbstractTDBMObject $remoteBean |
||
485 | */ |
||
486 | private function removeManyToOneRelationship(string $tableName, string $foreignKeyName, AbstractTDBMObject $remoteBean) |
||
491 | |||
492 | /** |
||
493 | * Returns the list of objects linked to this bean via a given foreign key. |
||
494 | * |
||
495 | * @param string $tableName |
||
496 | * @param string $foreignKeyName |
||
497 | * @param string $searchTableName |
||
498 | * @param array $searchFilter |
||
499 | * @param string $orderString The ORDER BY part of the query. All columns must be prefixed by the table name (in the form: table.column). WARNING : This parameter is not kept when there is an additionnal or removal object ! |
||
500 | * |
||
501 | * @return AlterableResultIterator |
||
502 | */ |
||
503 | protected function retrieveManyToOneRelationshipsStorage(string $tableName, string $foreignKeyName, string $searchTableName, array $searchFilter, $orderString = null) : AlterableResultIterator |
||
517 | |||
518 | /** |
||
519 | * Reverts any changes made to the object and resumes it to its DB state. |
||
520 | * This can only be called on objects that come from database and that have not been deleted. |
||
521 | * Otherwise, this will throw an exception. |
||
522 | * |
||
523 | * @throws TDBMException |
||
524 | */ |
||
525 | public function discardChanges() |
||
537 | |||
538 | /** |
||
539 | * Method used internally by TDBM. You should not use it directly. |
||
540 | * This method returns the status of the TDBMObject. |
||
541 | * This is one of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
||
542 | * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
||
543 | * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
||
544 | * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
||
545 | * |
||
546 | * @return string |
||
547 | */ |
||
548 | public function _getStatus() |
||
552 | |||
553 | /** |
||
554 | * Override the native php clone function for TDBMObjects. |
||
555 | */ |
||
556 | public function __clone() |
||
588 | |||
589 | /** |
||
590 | * Returns raw database rows. |
||
591 | * |
||
592 | * @return DbRow[] Key: table name, Value: DbRow object |
||
593 | */ |
||
594 | public function _getDbRows() |
||
598 | |||
599 | private function registerTable($tableName) |
||
617 | |||
618 | /** |
||
619 | * Internal function: return the list of relationships. |
||
620 | * |
||
621 | * @return \SplObjectStorage[] |
||
622 | */ |
||
623 | public function _getCachedRelationships() |
||
627 | |||
628 | /** |
||
629 | * Returns an array of used tables by this bean (from parent to child relationship). |
||
630 | * |
||
631 | * @return string[] |
||
632 | */ |
||
633 | abstract protected function getUsedTables(); |
||
634 | |||
635 | /** |
||
636 | * Method called when the bean is removed from database. |
||
637 | */ |
||
638 | protected function onDelete() |
||
639 | { |
||
641 | } |
||
642 |
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.