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 Model 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 Model, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Model extends AbstractModel |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * The id value of this model. |
||
| 19 | * Always converted to a string when in the model context. |
||
| 20 | * |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | protected $identifier; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The Model's has-one relationships |
||
| 27 | * |
||
| 28 | * @var Relationships\HasOne |
||
| 29 | */ |
||
| 30 | protected $hasOneRelationships; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The Model's has-many relationships |
||
| 34 | * |
||
| 35 | * @var Relationships\HasMany |
||
| 36 | */ |
||
| 37 | protected $hasManyRelationships; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Enables/disables collection auto-initialization on iteration. |
||
| 41 | * Will not load/fill the collection from the database if false. |
||
| 42 | * Is useful for large hasMany iterations where only id and type are required (ala serialization). |
||
| 43 | * |
||
| 44 | * @var bool |
||
| 45 | */ |
||
| 46 | protected $collectionAutoInit = true; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Constructor. |
||
| 50 | * |
||
| 51 | * @param EntityMetadata $metadata The internal entity metadata that supports this Model. |
||
| 52 | * @param string $identifier The database identifier. |
||
| 53 | * @param Store $store The model store service for handling persistence operations. |
||
| 54 | * @param array|null $properties The model's properties from the db layer to init the model with. New models will constructed with a null record. |
||
| 55 | */ |
||
| 56 | public function __construct(EntityMetadata $metadata, $identifier, Store $store, array $properties = null) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Cloner. |
||
| 64 | * Ensures sub objects are also cloned. |
||
| 65 | * |
||
| 66 | */ |
||
| 67 | public function __clone() |
||
| 73 | |||
| 74 | /** |
||
| 75 | * {@inheritdoc} |
||
| 76 | * |
||
| 77 | * Overloaded to support relationships. |
||
| 78 | * |
||
| 79 | */ |
||
| 80 | public function apply(array $properties) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * {@inheritdoc} |
||
| 113 | * |
||
| 114 | * Overloaded to support relationships. |
||
| 115 | */ |
||
| 116 | View Code Duplication | public function clear($key) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Enables or disables has-many collection auto-initialization from the database. |
||
| 135 | * |
||
| 136 | * @param bool $bit Whether to enable/disable. |
||
| 137 | * @return self |
||
| 138 | */ |
||
| 139 | public function enableCollectionAutoInit($bit = true) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Marks the record for deletion. |
||
| 147 | * Will not remove from the database until $this->save() is called. |
||
| 148 | * |
||
| 149 | * @api |
||
| 150 | * @return self |
||
| 151 | * @throws \RuntimeException If a new (unsaved) model is deleted. |
||
| 152 | */ |
||
| 153 | public function delete() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * {@inheritdoc} |
||
| 167 | * |
||
| 168 | * Overloaded to support relationships. |
||
| 169 | * |
||
| 170 | */ |
||
| 171 | public function get($key) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * {@inheritdoc} |
||
| 181 | * |
||
| 182 | * Overloaded to support relationships. |
||
| 183 | * |
||
| 184 | */ |
||
| 185 | public function getChangeSet() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Gets the composite key of the model by combining the model type with the unique id. |
||
| 195 | * |
||
| 196 | * @api |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | public function getCompositeKey() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Gets the unique identifier of this model. |
||
| 206 | * |
||
| 207 | * @api |
||
| 208 | * @return string |
||
| 209 | */ |
||
| 210 | public function getId() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Gets the model type. |
||
| 217 | * |
||
| 218 | * @api |
||
| 219 | * @return string |
||
| 220 | */ |
||
| 221 | public function getType() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * {@inheritdoc} |
||
| 228 | * |
||
| 229 | * Overloaded to support relationships. |
||
| 230 | * |
||
| 231 | */ |
||
| 232 | public function initialize(array $properties = null) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * {@inheritdoc} |
||
| 266 | * |
||
| 267 | * Overloaded to support relationships. |
||
| 268 | * |
||
| 269 | */ |
||
| 270 | public function isDirty() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Determines if a property key is a has-many relationship. |
||
| 280 | * |
||
| 281 | * @api |
||
| 282 | * @param string $key The property key. |
||
| 283 | * @return bool |
||
| 284 | */ |
||
| 285 | public function isHasMany($key) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Determines if a property key is a has-one relationship. |
||
| 295 | * |
||
| 296 | * @api |
||
| 297 | * @param string $key The property key. |
||
| 298 | * @return bool |
||
| 299 | */ |
||
| 300 | public function isHasOne($key) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Determines if a property key is a an inverse relationship. |
||
| 310 | * |
||
| 311 | * @api |
||
| 312 | * @param string $key The property key. |
||
| 313 | * @return bool |
||
| 314 | */ |
||
| 315 | public function isInverse($key) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Determines if a property key is a relationship (either has-one or has-many). |
||
| 325 | * |
||
| 326 | * @api |
||
| 327 | * @param string $key The property key. |
||
| 328 | * @return bool |
||
| 329 | */ |
||
| 330 | public function isRelationship($key) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Pushes a Model into a has-many relationship collection. |
||
| 337 | * This method must be used for has-many relationships. Direct set is not supported. |
||
| 338 | * To completely replace a has-many, call clear() first and then push() the new Models. |
||
| 339 | * |
||
| 340 | * @api |
||
| 341 | * @param string $key |
||
| 342 | * @param Model $model |
||
| 343 | * @return self |
||
| 344 | */ |
||
| 345 | View Code Duplication | public function push($key, Model $model) |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Reloads the model from the database. |
||
| 365 | * |
||
| 366 | * @api |
||
| 367 | * @return self |
||
| 368 | */ |
||
| 369 | public function reload() |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Removes a specific Model from a has-many relationship collection. |
||
| 376 | * |
||
| 377 | * @api |
||
| 378 | * @param string $key The has-many relationship key. |
||
| 379 | * @param Model $model The model to remove from the collection. |
||
| 380 | * @return self |
||
| 381 | */ |
||
| 382 | View Code Duplication | public function remove($key, Model $model) |
|
| 396 | |||
| 397 | /** |
||
| 398 | * {@inheritdoc} |
||
| 399 | * Overloaded to support relationship rollback. |
||
| 400 | */ |
||
| 401 | public function rollback() |
||
| 407 | |||
| 408 | /** |
||
| 409 | * {@inheritdoc} |
||
| 410 | * |
||
| 411 | * Overloaded to support relationships. |
||
| 412 | * Sets a model property: an attribute value, a has-one model, or an entire has-many model collection. |
||
| 413 | * Note: To push/remove a single Model into a has-many collection, or clear a collection, use @see push(), remove() and clear(). |
||
| 414 | * |
||
| 415 | */ |
||
| 416 | public function set($key, $value) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Saves the model. |
||
| 426 | * |
||
| 427 | * @api |
||
| 428 | * @param Implement cascade relationship saves. Or should the store handle this? |
||
| 429 | * @return self |
||
| 430 | */ |
||
| 431 | public function save() |
||
| 439 | |||
| 440 | /** |
||
| 441 | * {@inheritdoc} |
||
| 442 | * |
||
| 443 | * Overloaded to support relationships. |
||
| 444 | */ |
||
| 445 | protected function filterNotSavedProperties(array $properties) |
||
| 455 | |||
| 456 | /** |
||
| 457 | * {@inheritdoc} |
||
| 458 | * |
||
| 459 | * Overloaded to support global model defaults. |
||
| 460 | * |
||
| 461 | */ |
||
| 462 | protected function applyDefaultAttrValues(array $attributes = []) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Gets a relationship value. |
||
| 478 | * |
||
| 479 | * @param string $key The relationship key (field) name. |
||
| 480 | * @return Model|array|null |
||
| 481 | * @throws \RuntimeException If hasMany relationships are accessed directly. |
||
| 482 | */ |
||
| 483 | protected function getRelationship($key) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Sets a has-one relationship. |
||
| 502 | * |
||
| 503 | * @param string $key The relationship key (field) name. |
||
| 504 | * @param Model|null $model The model to relate. |
||
| 505 | * @return self |
||
| 506 | */ |
||
| 507 | View Code Duplication | protected function setHasOne($key, Model $model = null) |
|
| 520 | |||
| 521 | /** |
||
| 522 | * Sets a relationship value. |
||
| 523 | * |
||
| 524 | * @param string $key |
||
| 525 | * @param Model|null $value |
||
| 526 | * @return self |
||
| 527 | */ |
||
| 528 | protected function setRelationship($key, $value) |
||
| 538 | |||
| 539 | /** |
||
| 540 | * {@inheritdoc} |
||
| 541 | * |
||
| 542 | * Overloaded to handle loading from the database. |
||
| 543 | * If the model is currently empty, it will query the database and fill/load the model. |
||
| 544 | * |
||
| 545 | */ |
||
| 546 | protected function touch($force = false) |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Validates that the model type (from a Model or Collection instance) can be set to the relationship field. |
||
| 561 | * |
||
| 562 | * @param string $relKey The relationship field key. |
||
| 563 | * @param string $type The model type that is being related. |
||
| 564 | * @return self |
||
| 565 | */ |
||
| 566 | protected function validateRelSet($relKey, $type) |
||
| 573 | } |
||
| 574 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: