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 AbstractModel 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 AbstractModel, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 60 | abstract class AbstractModel extends AbstractEntity implements |
||
| 61 | ModelInterface, |
||
| 62 | DescribableInterface, |
||
| 63 | DescribablePropertyInterface, |
||
| 64 | LoggerAwareInterface, |
||
| 65 | StorableInterface, |
||
| 66 | ValidatableInterface, |
||
| 67 | ViewableInterface |
||
| 68 | { |
||
| 69 | use LoggerAwareTrait; |
||
| 70 | use DescribableTrait; |
||
| 71 | use DescribablePropertyTrait; |
||
| 72 | use StorableTrait; |
||
| 73 | use ValidatableTrait; |
||
| 74 | use ViewableTrait; |
||
| 75 | |||
| 76 | const DEFAULT_SOURCE_TYPE = 'database'; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @param array $data Dependencies. |
||
| 80 | */ |
||
| 81 | public function __construct(array $data = null) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Inject dependencies from a DI Container. |
||
| 117 | * |
||
| 118 | * @param Container $container A dependencies container instance. |
||
| 119 | * @return void |
||
| 120 | */ |
||
| 121 | public function setDependencies(Container $container) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Set the object's ID from an associative array map (or any other Traversable). |
||
| 128 | * |
||
| 129 | * Useful for setting the object ID before the rest of the object's data. |
||
| 130 | * |
||
| 131 | * @param array|\Traversable $data The object data. |
||
| 132 | * @return array|\Traversable The object data without the pre-set ID. |
||
| 133 | */ |
||
| 134 | protected function setIdFromData($data) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Sets the object data, from an associative array map (or any other Traversable). |
||
| 147 | * |
||
| 148 | * @param array|\Traversable $data The entity data. Will call setters. |
||
| 149 | * @return AbstractModel Chainable |
||
| 150 | */ |
||
| 151 | public function setData($data) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Return the object data as an array. |
||
| 160 | * |
||
| 161 | * @param array $propertyFilters Optional. Property filter. |
||
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | public function data(array $propertyFilters = null) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @param mixed $val The value to serialize. |
||
| 179 | * @return mixed |
||
| 180 | */ |
||
| 181 | private function serializedValue($val) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Override's `\Charcoal\Config\AbstractEntity`'s `setData` method to take properties into consideration. |
||
| 194 | * |
||
| 195 | * Also add a special case, to merge values for l10n properties. |
||
| 196 | * |
||
| 197 | * @param array|\Traversable $data The data to merge. |
||
| 198 | * @return EntityInterface Chainable |
||
| 199 | * @see \Charcoal\Config\AbstractEntity::offsetSet() |
||
| 200 | */ |
||
| 201 | public function mergeData($data) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @return array |
||
| 230 | */ |
||
| 231 | public function defaultData() |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Sets the data |
||
| 239 | * |
||
| 240 | * This function takes a 1-dimensional array and fill the object with its value. |
||
| 241 | * |
||
| 242 | * @param array $flatData The data, as a flat (1-dimension) array. |
||
| 243 | * @return AbstractModel Chainable |
||
| 244 | */ |
||
| 245 | public function setFlatData(array $flatData) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @return array |
||
| 284 | * @todo Implement retrieval of flattened data |
||
| 285 | */ |
||
| 286 | public function flatData() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param string $propertyIdent The property ident to get the value from. |
||
| 293 | * @return mixed |
||
| 294 | */ |
||
| 295 | public function propertyValue($propertyIdent) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @param array $properties Optional array of properties to save. If null, use all object's properties. |
||
| 311 | * @return boolean |
||
| 312 | */ |
||
| 313 | public function saveProperties(array $properties = null) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Load an object from the database from its l10n key $key. |
||
| 335 | * Also retrieve and return the actual language that matched. |
||
| 336 | * |
||
| 337 | * @param string $key Key pointing a column's l10n base ident. |
||
| 338 | * @param mixed $value Value to search in all languages. |
||
| 339 | * @param array $langs List of languages (code, ex: "en") to check into. |
||
| 340 | * @throws Exception If the PDO query fails. |
||
| 341 | * @return string The matching language. |
||
| 342 | */ |
||
| 343 | public function loadFromL10n($key, $value, array $langs) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Save the object's current state to storage. |
||
| 387 | * |
||
| 388 | * Overrides default StorableTrait save() method to also save properties. |
||
| 389 | * |
||
| 390 | * @see Charcoal\Source\StorableTrait::save() For the "create" event. |
||
| 391 | * @return boolean |
||
| 392 | * @todo Enable model validation. |
||
| 393 | */ |
||
| 394 | public function save() |
||
| 418 | |||
| 419 | /** |
||
| 420 | * StorableTrait > preSave(). Save hook called before saving the model. |
||
| 421 | * |
||
| 422 | * @return boolean |
||
| 423 | */ |
||
| 424 | protected function preSave() |
||
| 428 | |||
| 429 | /** |
||
| 430 | * StorableTrait > preUpdate(). Update hook called before updating the model. |
||
| 431 | * |
||
| 432 | * @param string[] $properties Optional. The properties to update. |
||
| 433 | * @return boolean |
||
| 434 | */ |
||
| 435 | protected function preUpdate(array $properties = null) |
||
| 441 | |||
| 442 | /** |
||
| 443 | * DescribableTrait > createMetadata(). |
||
| 444 | * |
||
| 445 | * @return MetadataInterface |
||
| 446 | */ |
||
| 447 | protected function createMetadata() |
||
| 451 | |||
| 452 | /** |
||
| 453 | * StorableInterface > createSource() |
||
| 454 | * |
||
| 455 | * @throws Exception If the metadata source can not be found. |
||
| 456 | * @return SourceInterface |
||
| 457 | */ |
||
| 458 | protected function createSource() |
||
| 479 | |||
| 480 | /** |
||
| 481 | * ValidatableInterface > create_validator(). |
||
| 482 | * |
||
| 483 | * @param array $data Optional. |
||
| 484 | * @return ValidatorInterface |
||
| 485 | */ |
||
| 486 | protected function createValidator(array $data = null) |
||
| 494 | |||
| 495 | /** |
||
| 496 | * @param array $data Optional. View data. |
||
| 497 | * @return ViewInterface |
||
| 498 | */ |
||
| 499 | public function createView(array $data = null) |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Convert the current class name in "type-ident" format. |
||
| 513 | * |
||
| 514 | * @return string |
||
| 515 | */ |
||
| 516 | public function objType() |
||
| 522 | } |
||
| 523 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.