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 | 70 | public function __construct($primaryDbValueOrRowData = null) |
|
138 | |||
139 | /** |
||
140 | * Make sure that class has all necessary static properties set. |
||
141 | */ |
||
142 | 70 | private static function checkStaticProperties() |
|
143 | { |
||
144 | 70 | static $checkedClasses = []; |
|
145 | 70 | if (!in_array(static::class, $checkedClasses)) { |
|
146 | 8 | if (empty(static::$dbTableName) |
|
147 | 7 | || empty(static::$dbProperties) |
|
148 | 7 | || empty(static::$primaryDbPropertyKey) |
|
149 | 7 | || (is_scalar(static::$primaryDbPropertyKey) |
|
150 | 7 | && !isset(static::$dbProperties[static::$primaryDbPropertyKey]['type'])) |
|
151 | 7 | || (is_array(static::$primaryDbPropertyKey) |
|
152 | 8 | && !Arr::allIn(static::$primaryDbPropertyKey, array_keys(static::$dbProperties))) |
|
153 | ) { |
||
154 | 1 | throw new \LogicException("All db entity's static properties not set"); |
|
155 | } |
||
156 | 7 | $checkedClasses[] = static::class; |
|
157 | } |
||
158 | 69 | } |
|
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 | 69 | 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 | 26 | public function getPrimaryDbValue() |
|
223 | |||
224 | /** |
||
225 | * @param mixed $primaryDbValue |
||
226 | */ |
||
227 | 24 | public function setPrimaryDbValue($primaryDbValue) |
|
234 | |||
235 | /** |
||
236 | * @param mixed $primaryDbValue |
||
237 | */ |
||
238 | 23 | protected function setDbDataPrimaryValue($primaryDbValue) |
|
250 | |||
251 | /** |
||
252 | * Update primary database value with data from set database data. |
||
253 | */ |
||
254 | 10 | public function updatePrimaryDbValueFromDbData() |
|
262 | |||
263 | /** |
||
264 | * @return mixed|null Returns value/values or null if not set |
||
265 | */ |
||
266 | 10 | protected function getPrimaryDbValueFromDbData() |
|
283 | |||
284 | /** |
||
285 | * @return bool |
||
286 | */ |
||
287 | 10 | public function isNewDbEntity() |
|
298 | |||
299 | /** |
||
300 | * @return bool |
||
301 | */ |
||
302 | 9 | public function shouldInsertOnDbSave() |
|
307 | |||
308 | /** |
||
309 | * Set a row field value. |
||
310 | * |
||
311 | * @param string $property |
||
312 | * @param mixed $value |
||
313 | * @param bool $setAsModified |
||
314 | * @param bool $force |
||
315 | */ |
||
316 | 29 | protected function setDbValue($property, $value, $setAsModified = true, $force = false) |
|
328 | |||
329 | /** |
||
330 | * @param string $property |
||
331 | * @param mixed $value |
||
332 | * @return mixed |
||
333 | */ |
||
334 | 41 | private function getValueWithPropertyType(string $property, $value) { |
|
358 | |||
359 | /** |
||
360 | * @param string $property |
||
|
|||
361 | * @param mixed $value |
||
362 | * @return mixed |
||
363 | */ |
||
364 | 24 | private function getPrimaryDbValueWithPropertyType($primaryDbValue) { |
|
391 | |||
392 | /** |
||
393 | * @param string $value |
||
394 | * @return \DateTime|\Carbon\Carbon|null |
||
395 | */ |
||
396 | 1 | protected function createDateTimeDbValue($value) |
|
407 | |||
408 | /** |
||
409 | * Get a database field value. |
||
410 | * |
||
411 | * @param string $property |
||
412 | * @return mixed |
||
413 | */ |
||
414 | 17 | protected function getDbValue($property) |
|
418 | |||
419 | /** |
||
420 | * Get raw (with underscore as word separator as it is formatted in database) |
||
421 | * field name from a object field property name (camelcased). |
||
422 | * |
||
423 | * @param string $propertyName |
||
424 | * @return string |
||
425 | */ |
||
426 | 22 | public static function getDbFieldName($propertyName) |
|
434 | |||
435 | /** |
||
436 | * Get object field property name (camelCased) from database field name (underscore separated). |
||
437 | * |
||
438 | * @param string $dbFieldName |
||
439 | * @return string |
||
440 | */ |
||
441 | 7 | public static function getDbPropertyName($dbFieldName) |
|
449 | |||
450 | /** |
||
451 | * @return bool |
||
452 | */ |
||
453 | 5 | public function hasModifiedDbProperties() |
|
457 | |||
458 | /** |
||
459 | * @param string $property |
||
460 | * @return bool |
||
461 | */ |
||
462 | 18 | public function isDbPropertyModified($property) |
|
466 | |||
467 | /** |
||
468 | * @return array |
||
469 | */ |
||
470 | 6 | public function getModifiedDbData() |
|
474 | |||
475 | /** |
||
476 | * @param string $property |
||
477 | */ |
||
478 | public function clearModifiedDbProperty($property) |
||
484 | |||
485 | 4 | public function clearModifiedDbProperties() |
|
489 | |||
490 | 1 | public function setAllDbPropertiesAsModified() |
|
494 | |||
495 | /** |
||
496 | * Magic method used to automate getters & setters for row data. |
||
497 | * |
||
498 | * @param string $name |
||
499 | * @param array $arguments |
||
500 | * @return mixed |
||
501 | */ |
||
502 | 23 | public function __call($name, array $arguments = []) |
|
519 | |||
520 | /** |
||
521 | * Set database fields' data. |
||
522 | * |
||
523 | * @param array $data |
||
524 | */ |
||
525 | 3 | public function setDbData(array $data) |
|
533 | |||
534 | /** |
||
535 | * Set db data from raw database row data with field names in database format. |
||
536 | * |
||
537 | * @param array $rowData |
||
538 | */ |
||
539 | 7 | public function setDbDataFromRow(array $rowData) |
|
561 | |||
562 | /** |
||
563 | * @return mixed |
||
564 | */ |
||
565 | protected function getPrimaryDbValueFromRow(array $rowData) |
||
581 | |||
582 | /** |
||
583 | * @return array |
||
584 | */ |
||
585 | 11 | public function getDbData() |
|
589 | |||
590 | /** |
||
591 | * @return array |
||
592 | */ |
||
593 | 1 | public function getDbRowData() |
|
603 | |||
604 | /** |
||
605 | * @return array |
||
606 | */ |
||
607 | 2 | public function getDbDataWithoutPrimary() |
|
621 | |||
622 | /** |
||
623 | * @param bool $deleteFromDbOnSave |
||
624 | */ |
||
625 | 3 | public function setDeleteFromDbOnSave($deleteFromDbOnSave = true) |
|
629 | |||
630 | /** |
||
631 | * @return bool |
||
632 | */ |
||
633 | 11 | public function shouldBeDeletedFromDbOnSave() |
|
637 | |||
638 | /** |
||
639 | * @return bool |
||
640 | */ |
||
641 | 1 | public function isDeleted() |
|
645 | |||
646 | /** |
||
647 | * @param bool $deleted |
||
648 | */ |
||
649 | 3 | public function setDeleted($deleted = true) |
|
653 | |||
654 | /** |
||
655 | * @param bool $forceDbInsertOnSave |
||
656 | */ |
||
657 | 5 | public function setForceDbInsertOnSave($forceDbInsertOnSave) |
|
661 | |||
662 | /** |
||
663 | * @return bool |
||
664 | */ |
||
665 | 5 | public function shouldForceDbInsertOnSave() |
|
669 | |||
670 | /** |
||
671 | * @return array |
||
672 | */ |
||
673 | 1 | public static function getDbProperties() |
|
677 | |||
678 | /** |
||
679 | * @param string $propertyName |
||
680 | * @return int|null |
||
681 | */ |
||
682 | 6 | public static function getDbPropertyMaxLength($propertyName) |
|
688 | |||
689 | /** |
||
690 | * @param string $propertyName |
||
691 | * @return bool |
||
692 | */ |
||
693 | 3 | public static function getDbPropertyRequired($propertyName) |
|
699 | |||
700 | /** |
||
701 | * @param string $propertyName |
||
702 | * @return bool |
||
703 | */ |
||
704 | 6 | public static function getDbPropertyNonEmpty($propertyName) |
|
710 | |||
711 | /** |
||
712 | * @return string|array |
||
713 | */ |
||
714 | 14 | public static function getPrimaryDbPropertyKey() |
|
718 | |||
719 | /** |
||
720 | * @return string|array |
||
721 | */ |
||
722 | 10 | public static function getPrimaryDbFieldKey() |
|
737 | |||
738 | /** |
||
739 | * Return array with db property names. |
||
740 | * |
||
741 | * @param array $exclude |
||
742 | * @return array |
||
743 | */ |
||
744 | 1 | public static function getDbPropertyNames(array $exclude = []) |
|
750 | |||
751 | /** |
||
752 | * Return array with raw db field names. |
||
753 | * |
||
754 | * @param array $exclude |
||
755 | * @return array |
||
756 | */ |
||
757 | 3 | public static function getDbFieldNames(array $exclude = []) |
|
766 | |||
767 | |||
768 | /** |
||
769 | * Get raw database field names prefixed (id, name becomes t.id, t.name etc.). |
||
770 | * |
||
771 | * @param string $dbTableAlias |
||
772 | * @param array $exclude |
||
773 | * @return array |
||
774 | */ |
||
775 | 1 | public static function getPrefixedDbFieldNames($dbTableAlias, array $exclude = []) |
|
779 | |||
780 | /** |
||
781 | * Get database columns transformed from e.g. "productId, date" to "p.product_id AS p_product_id, p.date AS p_date". |
||
782 | * |
||
783 | * @param string $dbTableAlias |
||
784 | * @param array $exclude |
||
785 | * @return array |
||
786 | */ |
||
787 | 1 | public static function getAliasedDbFieldNames($dbTableAlias, array $exclude = []) |
|
798 | |||
799 | /** |
||
800 | * Filters a full db item array by it's table alias and the strips the table alias. |
||
801 | * |
||
802 | * @param array $rowData |
||
803 | * @param string $dbTableAlias |
||
804 | * @param bool $skipStrip For cases when you want to filter only (no stripping) |
||
805 | * @return array |
||
806 | */ |
||
807 | 1 | public static function filterStripDbRowData(array $rowData, $dbTableAlias, $skipStrip = false) |
|
821 | |||
822 | /** |
||
823 | * @return string |
||
824 | */ |
||
825 | 8 | public static function getDbTableName() |
|
829 | |||
830 | /** |
||
831 | * Method to handle the serialization of this object. |
||
832 | * |
||
833 | * Implementation of Serializable interface. If descendant private properties |
||
834 | * should be serialized, they need to be visible to this parent (i.e. not private). |
||
835 | * |
||
836 | * @return string |
||
837 | */ |
||
838 | 2 | public function serialize() |
|
842 | |||
843 | /** |
||
844 | * Method to handle the unserialization of this object. |
||
845 | * |
||
846 | * Implementation of Serializable interface. If descendant private properties |
||
847 | * should be unserialized, they need to be visible to this parent (i.e. not private). |
||
848 | * |
||
849 | * @param string $serializedObject |
||
850 | */ |
||
851 | 1 | public function unserialize($serializedObject) |
|
859 | |||
860 | /** |
||
861 | * Merges other object's modified database data into this object. |
||
862 | * |
||
863 | * @param AbstractDbEntity $otherEntity |
||
864 | */ |
||
865 | 1 | public function mergeWith(AbstractDbEntity $otherEntity) |
|
870 | } |
||
871 |
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.