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) |
|
| 273 | { |
||
| 274 | 23 | if (!isset(static::$dbProperties[$property])) { |
|
| 275 | 1 | throw new \InvalidArgumentException("No database entity property[{$property}] exists"); |
|
| 276 | } |
||
| 277 | |||
| 278 | // Don't set type if value is null and allowed (allowed currently indicated by default => null) |
||
| 279 | 22 | $nullIsAllowed = (array_key_exists('default', static::$dbProperties[$property]) |
|
| 280 | 22 | && static::$dbProperties[$property]['default'] === null); |
|
| 281 | 22 | if (!($value === null && $nullIsAllowed)) { |
|
| 282 | 22 | $type = static::$dbProperties[$property]['type']; |
|
| 283 | // Set null when empty and default is null |
||
| 284 | 22 | if ($value === '' && $nullIsAllowed) { |
|
| 285 | 1 | $value = null; |
|
| 286 | 1 | } else { |
|
| 287 | 22 | settype($value, $type); |
|
| 288 | } |
||
| 289 | 22 | } |
|
| 290 | |||
| 291 | 22 | if ($this->dbData[$property] !== $value) { |
|
| 292 | 21 | $this->dbData[$property] = $value; |
|
| 293 | |||
| 294 | 21 | if ($setAsModified && !$this->isDbPropertyModified($property)) { |
|
| 295 | 14 | $this->modifiedDbProperties[] = $property; |
|
| 296 | 14 | } |
|
| 297 | 21 | } |
|
| 298 | 22 | } |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Get a database field value. |
||
| 302 | * |
||
| 303 | * @param string $property |
||
| 304 | * @return mixed |
||
| 305 | */ |
||
| 306 | 8 | protected function getDbValue($property) |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Get raw (with underscore as word separator as it is formatted in database) |
||
| 313 | * field name from a object field property name (camelcased). |
||
| 314 | * |
||
| 315 | * @param string $propertyName |
||
| 316 | * @return string |
||
| 317 | */ |
||
| 318 | 18 | public static function getDbFieldName($propertyName) |
|
| 319 | { |
||
| 320 | 18 | if (!isset(self::$cachedDbFieldNames[$propertyName])) { |
|
| 321 | 5 | self::$cachedDbFieldNames[$propertyName] = Str::camelToSeparator($propertyName); |
|
| 322 | 5 | } |
|
| 323 | |||
| 324 | 18 | return self::$cachedDbFieldNames[$propertyName]; |
|
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Get object field property name (camelCased) from database field name (underscore separated). |
||
| 329 | * |
||
| 330 | * @param string $dbFieldName |
||
| 331 | * @return string |
||
| 332 | */ |
||
| 333 | 7 | public static function getDbPropertyName($dbFieldName) |
|
| 334 | { |
||
| 335 | 7 | if (!isset(self::$cachedDbPropertyNames[$dbFieldName])) { |
|
| 336 | 2 | self::$cachedDbPropertyNames[$dbFieldName] = Str::separatorToCamel($dbFieldName); |
|
| 337 | 2 | } |
|
| 338 | |||
| 339 | 7 | return self::$cachedDbPropertyNames[$dbFieldName]; |
|
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @return bool |
||
| 344 | */ |
||
| 345 | 5 | public function hasModifiedDbProperties() |
|
| 349 | |||
| 350 | /** |
||
| 351 | * @param string $property |
||
| 352 | * @return bool |
||
| 353 | */ |
||
| 354 | 14 | public function isDbPropertyModified($property) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * @return array |
||
| 361 | */ |
||
| 362 | 5 | public function getModifiedDbData() |
|
| 366 | |||
| 367 | /** |
||
| 368 | * @param string $property |
||
| 369 | */ |
||
| 370 | public function clearModifiedDbProperty($property) |
||
| 371 | { |
||
| 372 | if (($key = array_search($property, $this->modifiedDbProperties))) { |
||
| 373 | unset($this->modifiedDbProperties[$key]); |
||
| 374 | } |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | */ |
||
| 379 | 4 | public function clearModifiedDbProperties() |
|
| 383 | |||
| 384 | /** |
||
| 385 | * Magic method used to automate getters & setters for row data. |
||
| 386 | * |
||
| 387 | * @param string $name |
||
| 388 | * @param array $arguments |
||
| 389 | * @return mixed |
||
| 390 | */ |
||
| 391 | 17 | public function __call($name, array $arguments = []) |
|
| 408 | |||
| 409 | /** |
||
| 410 | * Set database fields' data. |
||
| 411 | * |
||
| 412 | * @param array $data |
||
| 413 | */ |
||
| 414 | 3 | public function setDbData(array $data) |
|
| 415 | { |
||
| 416 | 3 | View Code Duplication | foreach (array_keys(static::$dbProperties) as $propertyName) { |
| 417 | 3 | if (array_key_exists($propertyName, $data)) { |
|
| 418 | 3 | $this->setDbValue($propertyName, $data[$propertyName], true); |
|
| 419 | 3 | } |
|
| 420 | 3 | } |
|
| 421 | 3 | } |
|
| 422 | |||
| 423 | /** |
||
| 424 | * Set db data from raw database row data with field names in database format. |
||
| 425 | * |
||
| 426 | * @param array $rowData |
||
| 427 | */ |
||
| 428 | 7 | public function setDbDataFromRow(array $rowData) |
|
| 429 | { |
||
| 430 | // If there are less row data than properties, use rows as starting point (optimization) |
||
| 431 | 7 | if (count($rowData) < count(static::$dbProperties)) { |
|
| 432 | 6 | foreach ($rowData as $dbFieldName => $value) { |
|
| 433 | 6 | $propertyName = static::getDbPropertyName($dbFieldName); |
|
| 434 | 6 | if (isset(static::$dbProperties[$propertyName])) { |
|
| 435 | 6 | $this->setDbValue($propertyName, $value, false); |
|
| 436 | 6 | } |
|
| 437 | 6 | } |
|
| 438 | // If there are more row data than properties, use properties as starting point |
||
| 439 | 6 | } else { |
|
| 440 | 2 | View Code Duplication | foreach (array_keys(static::$dbProperties) as $propertyName) { |
| 441 | 2 | $fieldName = static::getDbFieldName($propertyName); |
|
| 442 | 2 | if (array_key_exists($fieldName, $rowData)) { |
|
| 443 | 2 | $this->setDbValue($propertyName, $rowData[$fieldName], false); |
|
| 444 | 2 | } |
|
| 445 | 2 | } |
|
| 446 | } |
||
| 447 | 7 | } |
|
| 448 | |||
| 449 | /** |
||
| 450 | * @return array |
||
| 451 | */ |
||
| 452 | 8 | public function getDbData() |
|
| 456 | |||
| 457 | /** |
||
| 458 | * @return array |
||
| 459 | */ |
||
| 460 | 2 | public function getDbDataWithoutPrimary() |
|
| 461 | { |
||
| 462 | 2 | $dbDataWithoutPrimary = $this->dbData; |
|
| 463 | |||
| 464 | 2 | if (is_array(static::$primaryDbPropertyKey)) { |
|
| 465 | 1 | foreach (static::$primaryDbPropertyKey as $keyPart) { |
|
| 466 | 1 | unset($dbDataWithoutPrimary[$keyPart]); |
|
| 467 | 1 | } |
|
| 468 | 1 | } else { |
|
| 469 | 1 | unset($dbDataWithoutPrimary[static::$primaryDbPropertyKey]); |
|
| 470 | } |
||
| 471 | |||
| 472 | 2 | return $dbDataWithoutPrimary; |
|
| 473 | } |
||
| 474 | |||
| 475 | /** |
||
| 476 | * @param bool $deleteFromDbOnSave |
||
| 477 | */ |
||
| 478 | 3 | public function setDeleteFromDbOnSave($deleteFromDbOnSave = true) |
|
| 482 | |||
| 483 | /** |
||
| 484 | * @return bool |
||
| 485 | */ |
||
| 486 | 10 | public function shouldBeDeletedFromDbOnSave() |
|
| 490 | |||
| 491 | /** |
||
| 492 | * @return bool; |
||
| 493 | */ |
||
| 494 | 1 | public function isDeleted() |
|
| 498 | |||
| 499 | /** |
||
| 500 | * @param bool $deleted |
||
| 501 | */ |
||
| 502 | 3 | public function setDeleted($deleted = true) |
|
| 506 | |||
| 507 | /** |
||
| 508 | * @param bool $forceDbInsertOnSave |
||
| 509 | */ |
||
| 510 | 5 | public function setForceDbInsertOnSave($forceDbInsertOnSave) |
|
| 514 | |||
| 515 | /** |
||
| 516 | * @return bool |
||
| 517 | */ |
||
| 518 | 9 | public function shouldForceDbInsertOnSave() |
|
| 522 | |||
| 523 | /** |
||
| 524 | * @param string $propertyName |
||
| 525 | * @return int|null |
||
| 526 | */ |
||
| 527 | 6 | public static function getDbPropertyMaxLength($propertyName) |
|
| 533 | |||
| 534 | /** |
||
| 535 | * @param string $propertyName |
||
| 536 | * @return bool |
||
| 537 | */ |
||
| 538 | 2 | public static function getDbPropertyRequired($propertyName) |
|
| 544 | |||
| 545 | /** |
||
| 546 | * @param string $propertyName |
||
| 547 | * @return bool |
||
| 548 | */ |
||
| 549 | 3 | public static function getDbPropertyNonEmpty($propertyName) |
|
| 555 | |||
| 556 | /** |
||
| 557 | * Get validator for object's database data. |
||
| 558 | * |
||
| 559 | * @param ValidatorTranslatorInterface|SymfonyTranslatorInterface|null $translator |
||
| 560 | * @return Validator |
||
| 561 | */ |
||
| 562 | 3 | public function getDbDataValidator($translator = null) |
|
| 563 | { |
||
| 564 | 3 | $validRuleProperties = Validator::getValidRuleProperties(); |
|
| 565 | 3 | $fieldsRuleProperties = []; |
|
| 566 | 3 | foreach (static::$dbProperties as $propertyName => $attributes) { |
|
| 567 | // Always validate if validate is not explicitly set to false |
||
| 568 | 3 | if (!isset($attributes['validate']) || $attributes['validate'] === true) { |
|
| 569 | 3 | $fieldsRuleProperties[$propertyName] = []; |
|
| 570 | 3 | foreach ($validRuleProperties as $ruleName) { |
|
| 571 | 3 | if (isset($attributes[$ruleName])) { |
|
| 572 | 3 | $fieldsRuleProperties[$propertyName][$ruleName] = $attributes[$ruleName]; |
|
| 573 | 3 | } |
|
| 574 | 3 | } |
|
| 575 | 3 | } |
|
| 576 | 3 | } |
|
| 577 | |||
| 578 | 3 | $validator = new Validator($fieldsRuleProperties, $translator); |
|
| 579 | |||
| 580 | 3 | return $validator; |
|
| 581 | } |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Validate and (if no error messages) set database data. |
||
| 585 | * |
||
| 586 | * @param array $data The data (e.g. from a form post) to be validated and set |
||
| 587 | * @param ValidatorTranslatorInterface|SymfonyTranslatorInterface|null $translator |
||
| 588 | * @return array An array with all (if any) of error messages |
||
| 589 | */ |
||
| 590 | 2 | public function validateAndSetDbData(array $data, $translator = null) |
|
| 591 | { |
||
| 592 | // Get arguments this method was called with, to allow passthrough to getDbDataValidator() |
||
| 593 | 2 | $args = array_slice(func_get_args(), 1); |
|
| 594 | |||
| 595 | 2 | $validator = call_user_func_array([$this, 'getDbDataValidator'], $args); |
|
| 596 | 2 | $errorMessages = $validator->validate($data); |
|
| 597 | |||
| 598 | 2 | if (empty($errorMessages)) { |
|
| 599 | // Set db data from validated data because validator normalizes data as well (like trim) |
||
| 600 | 1 | $this->setDbData($validator->getValidatedData()); |
|
| 601 | 1 | } |
|
| 602 | |||
| 603 | 2 | return $errorMessages; |
|
| 604 | } |
||
| 605 | |||
| 606 | /** |
||
| 607 | * @return string|array |
||
| 608 | */ |
||
| 609 | 17 | public static function getPrimaryDbPropertyKey() |
|
| 613 | |||
| 614 | /** |
||
| 615 | * @return string|array |
||
| 616 | */ |
||
| 617 | 9 | public static function getPrimaryDbFieldKey() |
|
| 618 | { |
||
| 619 | 9 | $primaryDbPropertyKey = static::getPrimaryDbPropertyKey(); |
|
| 620 | |||
| 621 | 9 | if (is_array($primaryDbPropertyKey)) { |
|
| 622 | 2 | $primaryDbFieldKey = []; |
|
| 623 | 2 | foreach ($primaryDbPropertyKey as $propertyName) { |
|
| 624 | 2 | $primaryDbFieldKey[] = static::getDbFieldName($propertyName); |
|
| 625 | 2 | } |
|
| 626 | |||
| 627 | 2 | return $primaryDbFieldKey; |
|
| 628 | } else { |
||
| 629 | 7 | return static::getDbFieldName($primaryDbPropertyKey); |
|
| 630 | } |
||
| 631 | } |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Return array with db property names. |
||
| 635 | * |
||
| 636 | * @param array $exclude |
||
| 637 | * @return array |
||
| 638 | */ |
||
| 639 | 1 | public static function getDbPropertyNames(array $exclude = []) |
|
| 645 | |||
| 646 | /** |
||
| 647 | * Return array with raw db field names. |
||
| 648 | * |
||
| 649 | * @param array $exclude |
||
| 650 | * @return array |
||
| 651 | */ |
||
| 652 | 3 | public static function getDbFieldNames(array $exclude = []) |
|
| 653 | { |
||
| 654 | 3 | $fieldNames = []; |
|
| 655 | 3 | foreach (array_keys(static::$dbProperties) as $propertyName) { |
|
| 656 | 3 | $fieldNames[] = static::getDbFieldName($propertyName); |
|
| 657 | 3 | } |
|
| 658 | |||
| 659 | 3 | return $exclude ? array_diff($fieldNames, $exclude) : $fieldNames; |
|
| 660 | } |
||
| 661 | |||
| 662 | |||
| 663 | /** |
||
| 664 | * Get raw database field names prefixed (id, name becomes t.id, t.name etc.). |
||
| 665 | * |
||
| 666 | * @param string $dbTableAlias |
||
| 667 | * @param array $exclude |
||
| 668 | * @return array |
||
| 669 | */ |
||
| 670 | 1 | public static function getPrefixedDbFieldNames($dbTableAlias, array $exclude = []) |
|
| 674 | |||
| 675 | /** |
||
| 676 | * Get database columns transformed from e.g. "productId, date" to "p.product_id AS p_product_id, p.date AS p_date". |
||
| 677 | * |
||
| 678 | * @param string $dbTableAlias |
||
| 679 | * @param array $exclude |
||
| 680 | * @return array |
||
| 681 | */ |
||
| 682 | 1 | public static function getAliasedDbFieldNames($dbTableAlias, array $exclude = []) |
|
| 683 | { |
||
| 684 | 1 | $newArray = []; |
|
| 685 | 1 | foreach (static::getDbFieldNames($exclude) as $dbFieldName) { |
|
| 686 | 1 | $fromCol = $dbTableAlias . '.' . $dbFieldName; |
|
| 687 | 1 | $toCol = $dbTableAlias . '_' . $dbFieldName; |
|
| 688 | 1 | $newArray[] = $fromCol . ' AS ' . $toCol; |
|
| 689 | 1 | } |
|
| 690 | |||
| 691 | 1 | return $newArray; |
|
| 692 | } |
||
| 693 | |||
| 694 | /** |
||
| 695 | * Filters a full db item array by it's table alias and the strips the table alias. |
||
| 696 | * |
||
| 697 | * @param array $rowData |
||
| 698 | * @param string $dbTableAlias |
||
| 699 | * @param bool $skipStrip For cases when you want to filter only (no stripping) |
||
| 700 | * @return array |
||
| 701 | */ |
||
| 702 | 1 | public static function filterStripDbRowData(array $rowData, $dbTableAlias, $skipStrip = false) |
|
| 703 | { |
||
| 704 | 1 | $columnPrefix = $dbTableAlias . '_'; |
|
| 705 | |||
| 706 | 1 | $filteredAndStrippedRowData = []; |
|
| 707 | 1 | foreach ($rowData as $key => $val) { |
|
| 708 | 1 | if (strpos($key, $columnPrefix) === 0) { |
|
| 709 | 1 | $strippedKey = $skipStrip ? $key : Str::stripLeft($key, $columnPrefix); |
|
| 710 | 1 | $filteredAndStrippedRowData[$strippedKey] = $val; |
|
| 711 | 1 | } |
|
| 712 | 1 | } |
|
| 713 | |||
| 714 | 1 | return $filteredAndStrippedRowData; |
|
| 715 | } |
||
| 716 | |||
| 717 | /** |
||
| 718 | * @return string |
||
| 719 | */ |
||
| 720 | 8 | public static function getDbTableName() |
|
| 724 | |||
| 725 | /** |
||
| 726 | * Method to handle the serialization of this object. |
||
| 727 | * |
||
| 728 | * Implementation of Serializable interface. If descendant private properties |
||
| 729 | * should be serialized, they need to be visible to this parent (i.e. not private). |
||
| 730 | * |
||
| 731 | * @return string |
||
| 732 | */ |
||
| 733 | 2 | public function serialize() |
|
| 737 | |||
| 738 | /** |
||
| 739 | * Method to handle the unserialization of this object. |
||
| 740 | * |
||
| 741 | * Implementation of Serializable interface. If descendant private properties |
||
| 742 | * should be unserialized, they need to be visible to this parent (i.e. not private). |
||
| 743 | * |
||
| 744 | * @param string $serializedObject |
||
| 745 | */ |
||
| 746 | 1 | public function unserialize($serializedObject) |
|
| 747 | { |
||
| 748 | 1 | $objectVars = unserialize($serializedObject); |
|
| 749 | |||
| 750 | 1 | foreach ($objectVars as $key => $value) { |
|
| 751 | 1 | $this->{$key} = $value; |
|
| 752 | 1 | } |
|
| 753 | 1 | } |
|
| 754 | |||
| 755 | /** |
||
| 756 | * Merges other object's modified database data into this object. |
||
| 757 | * |
||
| 758 | * @param AbstractDbEntity $otherEntity |
||
| 759 | */ |
||
| 760 | 1 | public function mergeWith(AbstractDbEntity $otherEntity) |
|
| 765 | } |
||
| 766 |
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.