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 AbstractDbEntity 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 AbstractDbEntity, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | abstract class AbstractDbEntity implements \Serializable |
||
20 | { |
||
21 | /** |
||
22 | * The database table name (meant to be overridden). |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | protected static $dbTableName; |
||
27 | |||
28 | /** |
||
29 | * Entity's database properties and their attributes (meant to be overridden). |
||
30 | * Example format: |
||
31 | * |
||
32 | * $dbProperties = [ |
||
33 | * 'productId' => ['type' => 'int'], |
||
34 | * 'otherId' => ['type' => 'int', 'required' => true,], |
||
35 | * 'name' => ['type' => 'string', 'maxLength' => 10, 'required' => true, 'default' => 'Some name'], |
||
36 | * ]; |
||
37 | * |
||
38 | * 'type' => 'int' Corresponding PHP type (required). |
||
39 | * 'required' => true The value have to be set (not '', null, false) |
||
40 | * 'nonEmpty' => true The value should not be empty ('', 0, null) |
||
41 | * |
||
42 | * Properties correspond to database table's columns but words are |
||
43 | * camel cased instead of separated with underscore (_) as in the database. |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | protected static $dbProperties = []; |
||
48 | |||
49 | /** |
||
50 | * Object database field name that is used for primary key (meant to be overridden). |
||
51 | * Should be camel cased as it maps to the dbFields array. |
||
52 | * |
||
53 | * @var string|array |
||
54 | */ |
||
55 | protected static $primaryDbPropertyKey; |
||
56 | |||
57 | /** |
||
58 | * @var array |
||
59 | */ |
||
60 | private static $cachedDefaultDbData = []; |
||
61 | |||
62 | /** |
||
63 | * @var array |
||
64 | */ |
||
65 | private static $cachedDbPropertyNames; |
||
66 | |||
67 | /** |
||
68 | * @var array |
||
69 | */ |
||
70 | private static $cachedDbFieldNames; |
||
71 | |||
72 | /** |
||
73 | * @var array |
||
74 | */ |
||
75 | private static $typeDefaults = [ |
||
76 | 'string' => '', |
||
77 | 'int' => 0, |
||
78 | 'float' => 0.0, |
||
79 | 'bool' => false, |
||
80 | 'dateTime' => null, |
||
81 | ]; |
||
82 | |||
83 | /** |
||
84 | * Database row data with field names and their values. |
||
85 | * |
||
86 | * @var array |
||
87 | */ |
||
88 | private $dbData = []; |
||
89 | |||
90 | /** |
||
91 | * The primary key value currently set in database. This |
||
92 | * can be different from the value in $dbData if the primary value |
||
93 | * is changed. |
||
94 | * |
||
95 | * @var mixed |
||
96 | */ |
||
97 | private $primaryDbValue; |
||
98 | |||
99 | /** |
||
100 | * Database fields that has had their value modified since init/load. |
||
101 | * |
||
102 | * @var array |
||
103 | */ |
||
104 | private $modifiedDbProperties = []; |
||
105 | |||
106 | /** |
||
107 | * @var bool |
||
108 | */ |
||
109 | private $deleteFromDbOnSave = false; |
||
110 | |||
111 | /** |
||
112 | * @var bool |
||
113 | */ |
||
114 | private $deleted = false; |
||
115 | |||
116 | /** |
||
117 | * @var bool |
||
118 | */ |
||
119 | private $forceDbInsertOnSave = false; |
||
120 | |||
121 | /** |
||
122 | * Constructor. |
||
123 | * |
||
124 | * @param mixed $primaryDbValueOrRowData |
||
125 | */ |
||
126 | 68 | public function __construct($primaryDbValueOrRowData = null) |
|
138 | |||
139 | /** |
||
140 | * Make sure that class has all necessary static properties set. |
||
141 | */ |
||
142 | 68 | private static function checkStaticProperties() |
|
159 | |||
160 | /** |
||
161 | * @param mixed $primaryDbValueOrRowData |
||
162 | */ |
||
163 | 13 | public function setPrimaryDbValueOrRowData($primaryDbValueOrRowData = null) |
|
173 | |||
174 | /** |
||
175 | * Get all default database values. |
||
176 | * |
||
177 | * @return array |
||
178 | */ |
||
179 | 67 | public function getDefaultDbData() |
|
191 | |||
192 | /** |
||
193 | * Get default db value (can be overridden if non static default values need to be used). |
||
194 | * |
||
195 | * @param string $propertyName |
||
196 | * @return mixed |
||
197 | */ |
||
198 | 7 | public function getDefaultDbPropertyValue($propertyName) |
|
210 | |||
211 | /** |
||
212 | * The primary key value currently set in database. |
||
213 | * |
||
214 | * This can be different from the value/values in $dbData |
||
215 | * if a primary value is changed. |
||
216 | * |
||
217 | * @return mixed |
||
218 | */ |
||
219 | 24 | public function getPrimaryDbValue() |
|
223 | |||
224 | /** |
||
225 | * @param mixed $primaryDbValue |
||
226 | */ |
||
227 | 23 | public function setPrimaryDbValue($primaryDbValue) |
|
234 | |||
235 | /** |
||
236 | * @param mixed $primaryDbValue |
||
237 | */ |
||
238 | 22 | protected function setDbDataPrimaryValue($primaryDbValue) |
|
250 | |||
251 | /** |
||
252 | * @return bool |
||
253 | */ |
||
254 | 10 | public function isNewDbEntity() |
|
265 | |||
266 | /** |
||
267 | * @return bool |
||
268 | */ |
||
269 | 9 | public function shouldInsertOnDbSave() |
|
274 | |||
275 | /** |
||
276 | * Set a row field value. |
||
277 | * |
||
278 | * @param string $property |
||
279 | * @param mixed $value |
||
280 | * @param bool $setAsModified |
||
281 | * @param bool $force |
||
282 | */ |
||
283 | 27 | protected function setDbValue($property, $value, $setAsModified = true, $force = false) |
|
295 | |||
296 | /** |
||
297 | * @param string $property |
||
298 | * @param mixed $value |
||
299 | * @return mixed |
||
300 | */ |
||
301 | 39 | private function getValueWithPropertyType(string $property, $value) { |
|
325 | |||
326 | /** |
||
327 | * @param string $property |
||
|
|||
328 | * @param mixed $value |
||
329 | * @return mixed |
||
330 | */ |
||
331 | 23 | private function getPrimaryDbValueWithPropertyType($primaryDbValue) { |
|
358 | |||
359 | /** |
||
360 | * @param string $value |
||
361 | * @return \DateTime|\Carbon\Carbon|null |
||
362 | */ |
||
363 | 1 | protected function createDateTimeDbValue($value) |
|
374 | |||
375 | /** |
||
376 | * Get a database field value. |
||
377 | * |
||
378 | * @param string $property |
||
379 | * @return mixed |
||
380 | */ |
||
381 | 9 | protected function getDbValue($property) |
|
385 | |||
386 | /** |
||
387 | * Get raw (with underscore as word separator as it is formatted in database) |
||
388 | * field name from a object field property name (camelcased). |
||
389 | * |
||
390 | * @param string $propertyName |
||
391 | * @return string |
||
392 | */ |
||
393 | 24 | public static function getDbFieldName($propertyName) |
|
401 | |||
402 | /** |
||
403 | * Get object field property name (camelCased) from database field name (underscore separated). |
||
404 | * |
||
405 | * @param string $dbFieldName |
||
406 | * @return string |
||
407 | */ |
||
408 | 7 | public static function getDbPropertyName($dbFieldName) |
|
416 | |||
417 | /** |
||
418 | * @return bool |
||
419 | */ |
||
420 | 5 | public function hasModifiedDbProperties() |
|
424 | |||
425 | /** |
||
426 | * @param string $property |
||
427 | * @return bool |
||
428 | */ |
||
429 | 17 | public function isDbPropertyModified($property) |
|
433 | |||
434 | /** |
||
435 | * @return array |
||
436 | */ |
||
437 | 6 | public function getModifiedDbData() |
|
441 | |||
442 | /** |
||
443 | * @param string $property |
||
444 | */ |
||
445 | public function clearModifiedDbProperty($property) |
||
451 | |||
452 | 4 | public function clearModifiedDbProperties() |
|
456 | |||
457 | 1 | public function setAllDbPropertiesAsModified() |
|
461 | |||
462 | /** |
||
463 | * Magic method used to automate getters & setters for row data. |
||
464 | * |
||
465 | * @param string $name |
||
466 | * @param array $arguments |
||
467 | * @return mixed |
||
468 | */ |
||
469 | 21 | public function __call($name, array $arguments = []) |
|
486 | |||
487 | /** |
||
488 | * Set database fields' data. |
||
489 | * |
||
490 | * @param array $data |
||
491 | */ |
||
492 | 3 | public function setDbData(array $data) |
|
500 | |||
501 | /** |
||
502 | * Set db data from raw database row data with field names in database format. |
||
503 | * |
||
504 | * @param array $rowData |
||
505 | */ |
||
506 | 7 | public function setDbDataFromRow(array $rowData) |
|
531 | |||
532 | /** |
||
533 | * @return mixed |
||
534 | */ |
||
535 | 7 | protected function getPrimaryDbValueFromRow(array $rowData) |
|
551 | |||
552 | /** |
||
553 | * @return array |
||
554 | */ |
||
555 | 11 | public function getDbData() |
|
559 | |||
560 | /** |
||
561 | * @return array |
||
562 | */ |
||
563 | 1 | public function getDbRowData() |
|
573 | |||
574 | /** |
||
575 | * @return array |
||
576 | */ |
||
577 | 2 | public function getDbDataWithoutPrimary() |
|
591 | |||
592 | /** |
||
593 | * @param bool $deleteFromDbOnSave |
||
594 | */ |
||
595 | 3 | public function setDeleteFromDbOnSave($deleteFromDbOnSave = true) |
|
599 | |||
600 | /** |
||
601 | * @return bool |
||
602 | */ |
||
603 | 11 | public function shouldBeDeletedFromDbOnSave() |
|
607 | |||
608 | /** |
||
609 | * @return bool |
||
610 | */ |
||
611 | 1 | public function isDeleted() |
|
615 | |||
616 | /** |
||
617 | * @param bool $deleted |
||
618 | */ |
||
619 | 3 | public function setDeleted($deleted = true) |
|
623 | |||
624 | /** |
||
625 | * @param bool $forceDbInsertOnSave |
||
626 | */ |
||
627 | 5 | public function setForceDbInsertOnSave($forceDbInsertOnSave) |
|
631 | |||
632 | /** |
||
633 | * @return bool |
||
634 | */ |
||
635 | 5 | public function shouldForceDbInsertOnSave() |
|
639 | |||
640 | /** |
||
641 | * @return array |
||
642 | */ |
||
643 | 1 | public static function getDbProperties() |
|
647 | |||
648 | /** |
||
649 | * @param string $propertyName |
||
650 | * @return int|null |
||
651 | */ |
||
652 | 6 | public static function getDbPropertyMaxLength($propertyName) |
|
658 | |||
659 | /** |
||
660 | * @param string $propertyName |
||
661 | * @return bool |
||
662 | */ |
||
663 | 3 | public static function getDbPropertyRequired($propertyName) |
|
669 | |||
670 | /** |
||
671 | * @param string $propertyName |
||
672 | * @return bool |
||
673 | */ |
||
674 | 6 | public static function getDbPropertyNonEmpty($propertyName) |
|
680 | |||
681 | /** |
||
682 | * @return string|array |
||
683 | */ |
||
684 | 14 | public static function getPrimaryDbPropertyKey() |
|
688 | |||
689 | /** |
||
690 | * @return string|array |
||
691 | */ |
||
692 | 10 | public static function getPrimaryDbFieldKey() |
|
707 | |||
708 | /** |
||
709 | * Return array with db property names. |
||
710 | * |
||
711 | * @param array $exclude |
||
712 | * @return array |
||
713 | */ |
||
714 | 1 | public static function getDbPropertyNames(array $exclude = []) |
|
720 | |||
721 | /** |
||
722 | * Return array with raw db field names. |
||
723 | * |
||
724 | * @param array $exclude |
||
725 | * @return array |
||
726 | */ |
||
727 | 3 | public static function getDbFieldNames(array $exclude = []) |
|
736 | |||
737 | |||
738 | /** |
||
739 | * Get raw database field names prefixed (id, name becomes t.id, t.name etc.). |
||
740 | * |
||
741 | * @param string $dbTableAlias |
||
742 | * @param array $exclude |
||
743 | * @return array |
||
744 | */ |
||
745 | 1 | public static function getPrefixedDbFieldNames($dbTableAlias, array $exclude = []) |
|
749 | |||
750 | /** |
||
751 | * Get database columns transformed from e.g. "productId, date" to "p.product_id AS p_product_id, p.date AS p_date". |
||
752 | * |
||
753 | * @param string $dbTableAlias |
||
754 | * @param array $exclude |
||
755 | * @return array |
||
756 | */ |
||
757 | 1 | public static function getAliasedDbFieldNames($dbTableAlias, array $exclude = []) |
|
768 | |||
769 | /** |
||
770 | * Filters a full db item array by it's table alias and the strips the table alias. |
||
771 | * |
||
772 | * @param array $rowData |
||
773 | * @param string $dbTableAlias |
||
774 | * @param bool $skipStrip For cases when you want to filter only (no stripping) |
||
775 | * @return array |
||
776 | */ |
||
777 | 1 | public static function filterStripDbRowData(array $rowData, $dbTableAlias, $skipStrip = false) |
|
791 | |||
792 | /** |
||
793 | * @return string |
||
794 | */ |
||
795 | 8 | public static function getDbTableName() |
|
799 | |||
800 | /** |
||
801 | * Method to handle the serialization of this object. |
||
802 | * |
||
803 | * Implementation of Serializable interface. If descendant private properties |
||
804 | * should be serialized, they need to be visible to this parent (i.e. not private). |
||
805 | * |
||
806 | * @return string |
||
807 | */ |
||
808 | 2 | public function serialize() |
|
812 | |||
813 | /** |
||
814 | * Method to handle the unserialization of this object. |
||
815 | * |
||
816 | * Implementation of Serializable interface. If descendant private properties |
||
817 | * should be unserialized, they need to be visible to this parent (i.e. not private). |
||
818 | * |
||
819 | * @param string $serializedObject |
||
820 | */ |
||
821 | 1 | public function unserialize($serializedObject) |
|
829 | |||
830 | /** |
||
831 | * Merges other object's modified database data into this object. |
||
832 | * |
||
833 | * @param AbstractDbEntity $otherEntity |
||
834 | */ |
||
835 | 1 | public function mergeWith(AbstractDbEntity $otherEntity) |
|
840 | } |
||
841 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.