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 |
||
14 | class Model extends AbstractModel |
||
15 | { |
||
16 | /** |
||
17 | * Enables/disables collection auto-initialization on iteration. |
||
18 | * Will not load/fill the collection from the database if false. |
||
19 | * Is useful for large hasMany iterations where only id and type are required (ala serialization). |
||
20 | * |
||
21 | * @var bool |
||
22 | */ |
||
23 | protected $collectionAutoInit = true; |
||
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 | * The id value of this model. |
||
41 | * Always converted to a string when in the model context. |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $identifier; |
||
46 | |||
47 | |||
48 | /** |
||
49 | * The metadata that defines this Model. |
||
50 | * |
||
51 | * @var EntityMetadata |
||
52 | */ |
||
53 | protected $metadata; |
||
54 | |||
55 | /** |
||
56 | * Constructor. |
||
57 | * |
||
58 | * @param EntityMetadata $metadata The internal entity metadata that supports this Model. |
||
59 | * @param string $identifier The database identifier. |
||
60 | * @param Store $store The model store service for handling persistence operations. |
||
61 | * @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. |
||
62 | */ |
||
63 | public function __construct(EntityMetadata $metadata, $identifier, Store $store, array $properties = null) |
||
68 | |||
69 | /** |
||
70 | * Cloner. |
||
71 | * Ensures sub objects are also cloned. |
||
72 | * |
||
73 | */ |
||
74 | public function __clone() |
||
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | * |
||
84 | * Overloaded to support relationships. |
||
85 | * |
||
86 | */ |
||
87 | public function apply(array $properties) |
||
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | * |
||
121 | * Overloaded to support relationships. |
||
122 | */ |
||
123 | View Code Duplication | public function clear($key) |
|
139 | |||
140 | /** |
||
141 | * Enables or disables has-many collection auto-initialization from the database. |
||
142 | * |
||
143 | * @param bool $bit Whether to enable/disable. |
||
144 | * @return self |
||
145 | */ |
||
146 | public function enableCollectionAutoInit($bit = true) |
||
151 | |||
152 | /** |
||
153 | * Marks the record for deletion. |
||
154 | * Will not remove from the database until $this->save() is called. |
||
155 | * |
||
156 | * @api |
||
157 | * @return self |
||
158 | * @throws \RuntimeException If a new (unsaved) model is deleted. |
||
159 | */ |
||
160 | public function delete() |
||
171 | |||
172 | /** |
||
173 | * {@inheritdoc} |
||
174 | * |
||
175 | * Overloaded to support relationships. |
||
176 | * |
||
177 | */ |
||
178 | public function get($key) |
||
185 | |||
186 | /** |
||
187 | * {@inheritdoc} |
||
188 | * |
||
189 | * Overloaded to support relationships. |
||
190 | * |
||
191 | */ |
||
192 | public function getChangeSet() |
||
199 | |||
200 | /** |
||
201 | * Gets the composite key of the model by combining the model type with the unique id. |
||
202 | * |
||
203 | * @api |
||
204 | * @return string |
||
205 | */ |
||
206 | public function getCompositeKey() |
||
210 | |||
211 | /** |
||
212 | * Gets the unique identifier of this model. |
||
213 | * |
||
214 | * @api |
||
215 | * @return string |
||
216 | */ |
||
217 | public function getId() |
||
221 | |||
222 | /** |
||
223 | * Gets the metadata for this model. |
||
224 | * |
||
225 | * @api |
||
226 | * @return EntityMetadata |
||
227 | */ |
||
228 | public function getMetadata() |
||
232 | |||
233 | /** |
||
234 | * Gets the model type. |
||
235 | * |
||
236 | * @api |
||
237 | * @return string |
||
238 | */ |
||
239 | public function getType() |
||
243 | |||
244 | /** |
||
245 | * {@inheritdoc} |
||
246 | * |
||
247 | * Overloaded to support relationships. |
||
248 | * |
||
249 | */ |
||
250 | public function initialize(array $properties = null) |
||
281 | |||
282 | /** |
||
283 | * {@inheritdoc} |
||
284 | * |
||
285 | * Overloaded to support relationships. |
||
286 | * |
||
287 | */ |
||
288 | public function isDirty() |
||
295 | |||
296 | /** |
||
297 | * Determines if a property key is a has-many relationship. |
||
298 | * |
||
299 | * @api |
||
300 | * @param string $key The property key. |
||
301 | * @return bool |
||
302 | */ |
||
303 | public function isHasMany($key) |
||
310 | |||
311 | /** |
||
312 | * Determines if a property key is a has-one relationship. |
||
313 | * |
||
314 | * @api |
||
315 | * @param string $key The property key. |
||
316 | * @return bool |
||
317 | */ |
||
318 | public function isHasOne($key) |
||
325 | |||
326 | /** |
||
327 | * Determines if a property key is a an inverse relationship. |
||
328 | * |
||
329 | * @api |
||
330 | * @param string $key The property key. |
||
331 | * @return bool |
||
332 | */ |
||
333 | public function isInverse($key) |
||
340 | |||
341 | /** |
||
342 | * Determines if a property key is a relationship (either has-one or has-many). |
||
343 | * |
||
344 | * @api |
||
345 | * @param string $key The property key. |
||
346 | * @return bool |
||
347 | */ |
||
348 | public function isRelationship($key) |
||
352 | |||
353 | /** |
||
354 | * Pushes a Model into a has-many relationship collection. |
||
355 | * This method must be used for has-many relationships. Direct set is not supported. |
||
356 | * To completely replace a has-many, call clear() first and then push() the new Models. |
||
357 | * |
||
358 | * @api |
||
359 | * @param string $key |
||
360 | * @param Model $model |
||
361 | * @return self |
||
362 | */ |
||
363 | View Code Duplication | public function push($key, Model $model) |
|
380 | |||
381 | /** |
||
382 | * Reloads the model from the database. |
||
383 | * |
||
384 | * @api |
||
385 | * @return self |
||
386 | */ |
||
387 | public function reload() |
||
391 | |||
392 | /** |
||
393 | * Removes a specific Model from a has-many relationship collection. |
||
394 | * |
||
395 | * @api |
||
396 | * @param string $key The has-many relationship key. |
||
397 | * @param Model $model The model to remove from the collection. |
||
398 | * @return self |
||
399 | */ |
||
400 | View Code Duplication | public function remove($key, Model $model) |
|
414 | |||
415 | /** |
||
416 | * {@inheritdoc} |
||
417 | * Overloaded to support relationship rollback. |
||
418 | */ |
||
419 | public function rollback() |
||
425 | |||
426 | /** |
||
427 | * {@inheritdoc} |
||
428 | * |
||
429 | * Overloaded to support relationships. |
||
430 | * Sets a model property: an attribute value, a has-one model, or an entire has-many model collection. |
||
431 | * Note: To push/remove a single Model into a has-many collection, or clear a collection, use @see push(), remove() and clear(). |
||
432 | * |
||
433 | */ |
||
434 | public function set($key, $value) |
||
441 | |||
442 | /** |
||
443 | * Saves the model. |
||
444 | * |
||
445 | * @api |
||
446 | * @param Implement cascade relationship saves. Or should the store handle this? |
||
447 | * @return self |
||
448 | */ |
||
449 | public function save() |
||
457 | |||
458 | /** |
||
459 | * {@inheritdoc} |
||
460 | * |
||
461 | * Overloaded to support relationships. |
||
462 | */ |
||
463 | protected function filterNotSavedProperties(array $properties) |
||
473 | |||
474 | /** |
||
475 | * {@inheritdoc} |
||
476 | * |
||
477 | * Overloaded to support global model defaults. |
||
478 | * |
||
479 | */ |
||
480 | protected function applyDefaultAttrValues(array $attributes = []) |
||
493 | |||
494 | /** |
||
495 | * Gets a relationship value. |
||
496 | * |
||
497 | * @param string $key The relationship key (field) name. |
||
498 | * @return Model|array|null |
||
499 | * @throws \RuntimeException If hasMany relationships are accessed directly. |
||
500 | */ |
||
501 | protected function getRelationship($key) |
||
517 | |||
518 | /** |
||
519 | * Sets a has-one relationship. |
||
520 | * |
||
521 | * @param string $key The relationship key (field) name. |
||
522 | * @param Model|null $model The model to relate. |
||
523 | * @return self |
||
524 | */ |
||
525 | View Code Duplication | protected function setHasOne($key, Model $model = null) |
|
538 | |||
539 | /** |
||
540 | * Sets a relationship value. |
||
541 | * |
||
542 | * @param string $key |
||
543 | * @param Model|null $value |
||
544 | * @return self |
||
545 | */ |
||
546 | protected function setRelationship($key, $value) |
||
556 | |||
557 | /** |
||
558 | * {@inheritdoc} |
||
559 | * |
||
560 | * Overloaded to handle loading from the database. |
||
561 | * If the model is currently empty, it will query the database and fill/load the model. |
||
562 | * |
||
563 | */ |
||
564 | protected function touch($force = false) |
||
576 | |||
577 | /** |
||
578 | * Validates that the model type (from a Model or Collection instance) can be set to the relationship field. |
||
579 | * |
||
580 | * @param string $relKey The relationship field key. |
||
581 | * @param string $type The model type that is being related. |
||
582 | * @return self |
||
583 | */ |
||
584 | protected function validateRelSet($relKey, $type) |
||
591 | } |
||
592 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.