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 |
||
| 22 | abstract class AbstractDbEntity implements \Serializable |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * The database table name (meant to be overridden). |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected static $dbTableName; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Entity's database properties and their attributes (meant to be overridden). |
||
| 33 | * Example format: |
||
| 34 | * |
||
| 35 | * $dbProperties = [ |
||
| 36 | * 'productId' => ['type' => 'int'], |
||
| 37 | * 'otherId' => ['type' => 'int', 'required' => true, 'validate' => false], |
||
| 38 | * 'name' => ['type' => 'string', 'maxLength' => 10, 'required' => true, 'default' => 'Some name'], |
||
| 39 | * ]; |
||
| 40 | * |
||
| 41 | * 'type' => 'int' Corresponding PHP type (required). |
||
| 42 | * 'validate' => false Turn off data validation, for example on required key fields that are set internally. |
||
| 43 | * 'required' => true The value have to be set (not '', null, false) |
||
| 44 | * 'nonEmpty' => true The value should not be empty ('', 0, null) |
||
| 45 | * |
||
| 46 | * Properties correspond to database table's columns but words are |
||
| 47 | * camel cased instead of separated with underscore (_) as in the database. |
||
| 48 | * |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | protected static $dbProperties = []; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Object database field name that is used for primary key (meant to be overridden). |
||
| 55 | * Should be camel cased as it maps to the dbFields array. |
||
| 56 | * |
||
| 57 | * @var string|array |
||
| 58 | */ |
||
| 59 | protected static $primaryDbPropertyKey; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | private static $cachedDefaultDbData = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | private static $cachedDbPropertyNames; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | private static $cachedDbFieldNames; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | private static $typeDefaults = [ |
||
| 80 | 'string' => '', |
||
| 81 | 'int' => 0, |
||
| 82 | 'float' => 0.0, |
||
| 83 | 'bool' => false, |
||
| 84 | ]; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Database row data with field names and their values. |
||
| 88 | * |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | private $dbData = []; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Database fields that has had their value modified since init/load. |
||
| 95 | * |
||
| 96 | * @var array |
||
| 97 | */ |
||
| 98 | private $modifiedDbProperties = []; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var bool |
||
| 102 | */ |
||
| 103 | private $deleteFromDbOnSave = false; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var bool |
||
| 107 | */ |
||
| 108 | private $deleted = false; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @var bool |
||
| 112 | */ |
||
| 113 | private $forceDbInsertOnSave = false; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Constructor. |
||
| 117 | * |
||
| 118 | * @param mixed $primaryDbValueOrRowData |
||
| 119 | */ |
||
| 120 | 64 | public function __construct($primaryDbValueOrRowData = null) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Make sure that class has all necessary static properties set. |
||
| 135 | */ |
||
| 136 | 64 | private static function checkStaticProperties() |
|
| 153 | |||
| 154 | /** |
||
| 155 | * @param mixed $primaryDbValueOrRowData |
||
| 156 | */ |
||
| 157 | 13 | public function setPrimaryDbValueOrRowData($primaryDbValueOrRowData = null) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Get all default database values. |
||
| 169 | * |
||
| 170 | * @return array |
||
| 171 | */ |
||
| 172 | 63 | public function getDefaultDbData() |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Get default db value (can be overridden if non static default values need to be used). |
||
| 187 | * |
||
| 188 | * @param string $propertyName |
||
| 189 | * @return mixed |
||
| 190 | */ |
||
| 191 | 6 | public function getDefaultDbPropertyValue($propertyName) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * @return mixed |
||
| 206 | */ |
||
| 207 | 21 | public function getPrimaryDbValue() |
|
| 220 | |||
| 221 | /** |
||
| 222 | * @param mixed $primaryDbValue |
||
| 223 | */ |
||
| 224 | 17 | public function setPrimaryDbValue($primaryDbValue) |
|
| 240 | |||
| 241 | /** |
||
| 242 | * @return bool |
||
| 243 | */ |
||
| 244 | 9 | public function isNewDbEntity() |
|
| 255 | |||
| 256 | /** |
||
| 257 | * @return bool |
||
| 258 | */ |
||
| 259 | 8 | public function shouldInsertOnDbSave() |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Set a row field value. |
||
| 267 | * |
||
| 268 | * @param string $property |
||
| 269 | * @param mixed $value |
||
| 270 | * @param bool $setAsModified |
||
| 271 | */ |
||
| 272 | 23 | protected function setDbValue($property, $value, $setAsModified = true) |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Get a database field value. |
||
| 300 | * |
||
| 301 | * @param string $property |
||
| 302 | * @return mixed |
||
| 303 | */ |
||
| 304 | 8 | protected function getDbValue($property) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Get raw (with underscore as word separator as it is formatted in database) |
||
| 311 | * field name from a object field property name (camelcased). |
||
| 312 | * |
||
| 313 | * @param string $propertyName |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | 18 | public static function getDbFieldName($propertyName) |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Get object field property name (camelCased) from database field name (underscore separated). |
||
| 327 | * |
||
| 328 | * @param string $dbFieldName |
||
| 329 | * @return string |
||
| 330 | */ |
||
| 331 | 7 | public static function getDbPropertyName($dbFieldName) |
|
| 339 | |||
| 340 | /** |
||
| 341 | * @return bool |
||
| 342 | */ |
||
| 343 | 5 | public function hasModifiedDbValues() |
|
| 347 | |||
| 348 | /** |
||
| 349 | * @param string $property |
||
| 350 | * @return bool |
||
| 351 | */ |
||
| 352 | 15 | public function isDbValueModified($property) |
|
| 356 | |||
| 357 | /** |
||
| 358 | * @return array |
||
| 359 | */ |
||
| 360 | 5 | public function getModifiedDbData() |
|
| 364 | |||
| 365 | /** |
||
| 366 | */ |
||
| 367 | 4 | public function clearModifiedDbProperties() |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Magic method used to automate getters & setters for row data. |
||
| 374 | * |
||
| 375 | * @param string $name |
||
| 376 | * @param array $arguments |
||
| 377 | * @return mixed |
||
| 378 | */ |
||
| 379 | 17 | public function __call($name, array $arguments = []) |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Set database fields' data. |
||
| 399 | * |
||
| 400 | * @param array $data |
||
| 401 | */ |
||
| 402 | 3 | public function setDbData(array $data) |
|
| 410 | |||
| 411 | /** |
||
| 412 | * Set db data from raw database row data with field names in database format. |
||
| 413 | * |
||
| 414 | * @param array $rowData |
||
| 415 | */ |
||
| 416 | 7 | public function setDbDataFromRow(array $rowData) |
|
| 436 | |||
| 437 | /** |
||
| 438 | * @return array |
||
| 439 | */ |
||
| 440 | 8 | public function getDbData() |
|
| 444 | |||
| 445 | /** |
||
| 446 | * @return array |
||
| 447 | */ |
||
| 448 | 2 | public function getDbDataWithoutPrimary() |
|
| 462 | |||
| 463 | /** |
||
| 464 | * @param bool $deleteFromDbOnSave |
||
| 465 | */ |
||
| 466 | 3 | public function setDeleteFromDbOnSave($deleteFromDbOnSave = true) |
|
| 470 | |||
| 471 | /** |
||
| 472 | * @return bool |
||
| 473 | */ |
||
| 474 | 10 | public function shouldBeDeletedFromDbOnSave() |
|
| 478 | |||
| 479 | /** |
||
| 480 | * @return bool; |
||
| 481 | */ |
||
| 482 | 1 | public function isDeleted() |
|
| 486 | |||
| 487 | /** |
||
| 488 | * @param bool $deleted |
||
| 489 | */ |
||
| 490 | 3 | public function setDeleted($deleted = true) |
|
| 494 | |||
| 495 | /** |
||
| 496 | * @param bool $forceDbInsertOnSave |
||
| 497 | */ |
||
| 498 | 5 | public function setForceDbInsertOnSave($forceDbInsertOnSave) |
|
| 502 | |||
| 503 | /** |
||
| 504 | * @return bool |
||
| 505 | */ |
||
| 506 | 9 | public function shouldForceDbInsertOnSave() |
|
| 510 | |||
| 511 | /** |
||
| 512 | * @param string $propertyName |
||
| 513 | * @return int|null |
||
| 514 | */ |
||
| 515 | 6 | public static function getDbPropertyMaxLength($propertyName) |
|
| 521 | |||
| 522 | /** |
||
| 523 | * @param string $propertyName |
||
| 524 | * @return bool |
||
| 525 | */ |
||
| 526 | 2 | public static function getDbPropertyRequired($propertyName) |
|
| 532 | |||
| 533 | /** |
||
| 534 | * @param string $propertyName |
||
| 535 | * @return bool |
||
| 536 | */ |
||
| 537 | 3 | public static function getDbPropertyNonEmpty($propertyName) |
|
| 543 | |||
| 544 | /** |
||
| 545 | * Get validator for object's database data. |
||
| 546 | * |
||
| 547 | * @param ValidatorTranslatorInterface|SymfonyTranslatorInterface|null $translator |
||
| 548 | * @return Validator |
||
| 549 | */ |
||
| 550 | 3 | public function getDbDataValidator($translator = null) |
|
| 570 | |||
| 571 | /** |
||
| 572 | * Validate and (if no error messages) set database data. |
||
| 573 | * |
||
| 574 | * @param array $data The data (e.g. from a form post) to be validated and set |
||
| 575 | * @param ValidatorTranslatorInterface|SymfonyTranslatorInterface|null $translator |
||
| 576 | * @return array An array with all (if any) of error messages |
||
| 577 | */ |
||
| 578 | 2 | public function validateAndSetDbData(array $data, $translator = null) |
|
| 593 | |||
| 594 | /** |
||
| 595 | * @return string|array |
||
| 596 | */ |
||
| 597 | 17 | public static function getPrimaryDbPropertyKey() |
|
| 601 | |||
| 602 | /** |
||
| 603 | * @return string|array |
||
| 604 | */ |
||
| 605 | 9 | public static function getPrimaryDbFieldKey() |
|
| 620 | |||
| 621 | /** |
||
| 622 | * Return array with db property names. |
||
| 623 | * |
||
| 624 | * @param array $exclude |
||
| 625 | * @return array |
||
| 626 | */ |
||
| 627 | 1 | public static function getDbPropertyNames(array $exclude = []) |
|
| 633 | |||
| 634 | /** |
||
| 635 | * Return array with raw db field names. |
||
| 636 | * |
||
| 637 | * @param array $exclude |
||
| 638 | * @return array |
||
| 639 | */ |
||
| 640 | 3 | public static function getDbFieldNames(array $exclude = []) |
|
| 649 | |||
| 650 | |||
| 651 | /** |
||
| 652 | * Get raw database field names prefixed (id, name becomes t.id, t.name etc.). |
||
| 653 | * |
||
| 654 | * @param string $dbTableAlias |
||
| 655 | * @param array $exclude |
||
| 656 | * @return array |
||
| 657 | */ |
||
| 658 | 1 | public static function getPrefixedDbFieldNames($dbTableAlias, array $exclude = []) |
|
| 662 | |||
| 663 | /** |
||
| 664 | * Get database columns transformed from e.g. "productId, date" to "p.product_id AS p_product_id, p.date AS p_date". |
||
| 665 | * |
||
| 666 | * @param string $dbTableAlias |
||
| 667 | * @param array $exclude |
||
| 668 | * @return array |
||
| 669 | */ |
||
| 670 | 1 | public static function getAliasedDbFieldNames($dbTableAlias, array $exclude = []) |
|
| 681 | |||
| 682 | /** |
||
| 683 | * Filters a full db item array by it's table alias and the strips the table alias. |
||
| 684 | * |
||
| 685 | * @param array $rowData |
||
| 686 | * @param string $dbTableAlias |
||
| 687 | * @param bool $skipStrip For cases when you want to filter only (no stripping) |
||
| 688 | * @return array |
||
| 689 | */ |
||
| 690 | 1 | public static function filterStripDbRowData(array $rowData, $dbTableAlias, $skipStrip = false) |
|
| 704 | |||
| 705 | /** |
||
| 706 | * @return string |
||
| 707 | */ |
||
| 708 | 8 | public static function getDbTableName() |
|
| 712 | |||
| 713 | /** |
||
| 714 | * Method to handle the serialization of this object. |
||
| 715 | * |
||
| 716 | * Implementation of Serializable interface. If descendant private properties |
||
| 717 | * should be serialized, they need to be visible to this parent (i.e. not private). |
||
| 718 | * |
||
| 719 | * @return string |
||
| 720 | */ |
||
| 721 | 2 | public function serialize() |
|
| 725 | |||
| 726 | /** |
||
| 727 | * Method to handle the unserialization of this object. |
||
| 728 | * |
||
| 729 | * Implementation of Serializable interface. If descendant private properties |
||
| 730 | * should be unserialized, they need to be visible to this parent (i.e. not private). |
||
| 731 | * |
||
| 732 | * @param string $serializedObject |
||
| 733 | */ |
||
| 734 | 1 | public function unserialize($serializedObject) |
|
| 742 | |||
| 743 | /** |
||
| 744 | * Merges other object's modified database data into this object. |
||
| 745 | * |
||
| 746 | * @param AbstractDbEntity $otherEntity |
||
| 747 | */ |
||
| 748 | 1 | public function mergeWith(AbstractDbEntity $otherEntity) |
|
| 753 | } |
||
| 754 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.