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 |
||
20 | abstract class Model implements Arrayable, \ArrayAccess, Jsonable |
||
21 | { |
||
22 | /** |
||
23 | * The connection name for the model. |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $connection; |
||
28 | |||
29 | /** |
||
30 | * The primary key for the model. |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $primaryKey = 'Id'; |
||
35 | |||
36 | /** |
||
37 | * The connection resolver instance. |
||
38 | * |
||
39 | * @var \Magister\Services\Database\ConnectionResolverInterface |
||
40 | */ |
||
41 | protected static $resolver; |
||
42 | |||
43 | /** |
||
44 | * The model's attributes. |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $attributes = []; |
||
49 | |||
50 | /** |
||
51 | * The loaded relationships for the model. |
||
52 | * |
||
53 | * @var array |
||
54 | */ |
||
55 | protected $relations = []; |
||
56 | |||
57 | /** |
||
58 | * The attributes that should be mutated to dates. |
||
59 | * |
||
60 | * @var array |
||
61 | */ |
||
62 | protected $dates = []; |
||
63 | |||
64 | /** |
||
65 | * Create a new model instance. |
||
66 | * |
||
67 | * @param array $attributes |
||
68 | */ |
||
69 | public function __construct(array $attributes = []) |
||
73 | |||
74 | /** |
||
75 | * Fill the model with an array of attributes. |
||
76 | * |
||
77 | * @param array $attributes |
||
78 | * |
||
79 | * @return $this |
||
80 | */ |
||
81 | public function fill(array $attributes) |
||
89 | |||
90 | /** |
||
91 | * Get all of the items from the model. |
||
92 | * |
||
93 | * @return \Magister\Services\Support\Collection |
||
94 | */ |
||
95 | public static function all() |
||
101 | |||
102 | /** |
||
103 | * Find a model by its primary key. |
||
104 | * |
||
105 | * @param mixed $id |
||
106 | * |
||
107 | * @return \Magister\Services\Support\Collection|static|null |
||
108 | */ |
||
109 | public static function find($id) |
||
113 | |||
114 | /** |
||
115 | * Begin querying the model on a given connection. |
||
116 | * |
||
117 | * @param string|null $connection |
||
118 | * |
||
119 | * @return \Magister\Services\Database\Elegant\Builder |
||
120 | */ |
||
121 | public static function on($connection = null) |
||
129 | |||
130 | /** |
||
131 | * Define a one-to-one relationship. |
||
132 | * |
||
133 | * @param string $related |
||
134 | * @param string $foreignKey |
||
135 | * @param string $localKey |
||
136 | * |
||
137 | * @return \Magister\Services\Database\Elegant\Relations\HasOne |
||
138 | */ |
||
139 | View Code Duplication | public function hasOne($related, $foreignKey = null, $localKey = null) |
|
149 | |||
150 | /** |
||
151 | * Define a one-to-many relationship. |
||
152 | * |
||
153 | * @param string $related |
||
154 | * @param string $foreignKey |
||
155 | * @param string $localKey |
||
156 | * |
||
157 | * @return \Magister\Services\Database\Elegant\Relations\HasMany |
||
158 | */ |
||
159 | View Code Duplication | public function hasMany($related, $foreignKey = null, $localKey = null) |
|
169 | |||
170 | /** |
||
171 | * Create a new instance of the given model. |
||
172 | * |
||
173 | * @param array $attributes |
||
174 | * |
||
175 | * @return static |
||
176 | */ |
||
177 | public function newInstance($attributes = []) |
||
181 | |||
182 | /** |
||
183 | * Create a new model instance. |
||
184 | * |
||
185 | * @param array $attributes |
||
186 | * @param string|null $connection |
||
187 | * |
||
188 | * @return static |
||
189 | */ |
||
190 | public function newFromBuilder($attributes = [], $connection = null) |
||
200 | |||
201 | /** |
||
202 | * Create a collection of models from plain arrays. |
||
203 | * |
||
204 | * @param array $items |
||
205 | * @param string|null $connection |
||
206 | * |
||
207 | * @return \Magister\Services\Support\Collection |
||
208 | */ |
||
209 | public static function hydrate(array $items, $connection = null) |
||
219 | |||
220 | /** |
||
221 | * Begin querying the model. |
||
222 | * |
||
223 | * @return \Magister\Services\Database\Elegant\Builder |
||
224 | */ |
||
225 | public static function query() |
||
229 | |||
230 | /** |
||
231 | * Get a new query builder. |
||
232 | * |
||
233 | * @return \Magister\Services\Database\Elegant\Builder |
||
234 | */ |
||
235 | public function newQuery() |
||
243 | |||
244 | /** |
||
245 | * Create a new Elegant builder instance. |
||
246 | * |
||
247 | * @param \Magister\Services\Database\Query\Builder $query |
||
248 | * |
||
249 | * @return \Magister\Services\Database\Elegant\Builder |
||
250 | */ |
||
251 | public function newElegantBuilder($query) |
||
255 | |||
256 | /** |
||
257 | * Create a new query builder instance. |
||
258 | * |
||
259 | * @return \Magister\Services\Database\Query\Builder |
||
260 | */ |
||
261 | public function newQueryBuilder() |
||
267 | |||
268 | /** |
||
269 | * Create a new collection instance. |
||
270 | * |
||
271 | * @param array $models |
||
272 | * |
||
273 | * @return \Magister\Services\Support\Collection |
||
274 | */ |
||
275 | public function newCollection(array $models = []) |
||
279 | |||
280 | /** |
||
281 | * Set a given attribute on the model. |
||
282 | * |
||
283 | * @param string $key |
||
284 | * @param mixed $value |
||
285 | * |
||
286 | * @return void |
||
287 | */ |
||
288 | public function setAttribute($key, $value) |
||
292 | |||
293 | /** |
||
294 | * Set the array of model attributes without checking. |
||
295 | * |
||
296 | * @param array $attributes |
||
297 | * |
||
298 | * @return void |
||
299 | */ |
||
300 | public function setRawAttributes(array $attributes) |
||
304 | |||
305 | /** |
||
306 | * Get an attribute from the model. |
||
307 | * |
||
308 | * @param string $key |
||
309 | * |
||
310 | * @return mixed |
||
311 | */ |
||
312 | public function getAttribute($key) |
||
320 | |||
321 | /** |
||
322 | * Get a plain attribute (not a relationship). |
||
323 | * |
||
324 | * @param string $key |
||
325 | * |
||
326 | * @return mixed |
||
327 | */ |
||
328 | public function getAttributeValue($key) |
||
340 | |||
341 | /** |
||
342 | * Get a relationship. |
||
343 | * |
||
344 | * @param string $key |
||
345 | * |
||
346 | * @return mixed |
||
347 | */ |
||
348 | public function getRelationValue($key) |
||
364 | |||
365 | /** |
||
366 | * Get an attribute from the $attributes array. |
||
367 | * |
||
368 | * @param string $key |
||
369 | * |
||
370 | * @return mixed |
||
371 | */ |
||
372 | protected function getAttributeFromArray($key) |
||
376 | |||
377 | /** |
||
378 | * Get a relationship value from a method. |
||
379 | * |
||
380 | * @param string $method |
||
381 | * |
||
382 | * @throws \LogicException |
||
383 | * |
||
384 | * @return mixed |
||
385 | */ |
||
386 | protected function getRelationshipFromMethod($method) |
||
396 | |||
397 | /** |
||
398 | * Get the attributes that should be converted to dates. |
||
399 | * |
||
400 | * @return array |
||
401 | */ |
||
402 | public function getDates() |
||
406 | |||
407 | /** |
||
408 | * Return a timestamp as a DateTime object. |
||
409 | * |
||
410 | * @param mixed $value |
||
411 | * |
||
412 | * @return \DateTime |
||
413 | */ |
||
414 | protected function asDateTime($value) |
||
418 | |||
419 | /** |
||
420 | * Get all of the current attributes on the model. |
||
421 | * |
||
422 | * @return array |
||
423 | */ |
||
424 | public function getAttributes() |
||
428 | |||
429 | /** |
||
430 | * Get all the loaded relations for the instance. |
||
431 | * |
||
432 | * @return array |
||
433 | */ |
||
434 | public function getRelations() |
||
438 | |||
439 | /** |
||
440 | * Get a specified relationship. |
||
441 | * |
||
442 | * @param string $relation |
||
443 | * |
||
444 | * @return mixed |
||
445 | */ |
||
446 | public function getRelation($relation) |
||
450 | |||
451 | /** |
||
452 | * Determine if the given relation is loaded. |
||
453 | * |
||
454 | * @param string $key |
||
455 | * |
||
456 | * @return bool |
||
457 | */ |
||
458 | public function relationLoaded($key) |
||
462 | |||
463 | /** |
||
464 | * Set the specific relationship in the model. |
||
465 | * |
||
466 | * @param string $relation |
||
467 | * @param mixed $value |
||
468 | * |
||
469 | * @return $this |
||
470 | */ |
||
471 | public function setRelation($relation, $value) |
||
477 | |||
478 | /** |
||
479 | * Set the entire relations array on the model. |
||
480 | * |
||
481 | * @param array $relations |
||
482 | * |
||
483 | * @return $this |
||
484 | */ |
||
485 | public function setRelations(array $relations) |
||
491 | |||
492 | /** |
||
493 | * Get the connection for the model. |
||
494 | * |
||
495 | * @return \Magister\Services\Database\Connection |
||
496 | */ |
||
497 | public function getConnection() |
||
501 | |||
502 | /** |
||
503 | * Get the current connection name for the model. |
||
504 | * |
||
505 | * @return string |
||
506 | */ |
||
507 | public function getConnectionName() |
||
511 | |||
512 | /** |
||
513 | * Set the connection associated with the model. |
||
514 | * |
||
515 | * @param string $name |
||
516 | * |
||
517 | * @return $this |
||
518 | */ |
||
519 | public function setConnection($name) |
||
525 | |||
526 | /** |
||
527 | * Resolve a connection instance. |
||
528 | * |
||
529 | * @param string $connection |
||
530 | * |
||
531 | * @return \Magister\Services\Database\Connection |
||
532 | */ |
||
533 | public static function resolveConnection($connection = null) |
||
537 | |||
538 | /** |
||
539 | * Get the connection resolver instance. |
||
540 | * |
||
541 | * @return \Magister\Services\Database\ConnectionResolverInterface |
||
542 | */ |
||
543 | public static function getConnectionResolver() |
||
547 | |||
548 | /** |
||
549 | * Set the connection resolver instance. |
||
550 | * |
||
551 | * @param \Magister\Services\Database\ConnectionResolverInterface $resolver |
||
552 | * |
||
553 | * @return void |
||
554 | */ |
||
555 | public static function setConnectionResolver(Resolver $resolver) |
||
559 | |||
560 | /** |
||
561 | * Get the url associated with the model. |
||
562 | * |
||
563 | * @throws \RuntimeException |
||
564 | * |
||
565 | * @return void |
||
566 | */ |
||
567 | public function getUrl() |
||
571 | |||
572 | /** |
||
573 | * Get the value of the model's primary key. |
||
574 | * |
||
575 | * @return mixed |
||
576 | */ |
||
577 | public function getKey() |
||
581 | |||
582 | /** |
||
583 | * Get the primary key for the model. |
||
584 | * |
||
585 | * @return string |
||
586 | */ |
||
587 | public function getKeyName() |
||
591 | |||
592 | /** |
||
593 | * Set the primary key for the model. |
||
594 | * |
||
595 | * @param string $key |
||
596 | * |
||
597 | * @return void |
||
598 | */ |
||
599 | public function setKeyName($key) |
||
603 | |||
604 | /** |
||
605 | * Get the default foreign key name for the model. |
||
606 | * |
||
607 | * @return string |
||
608 | */ |
||
609 | public function getForeignKey() |
||
613 | |||
614 | /** |
||
615 | * Convert the model instance to JSON. |
||
616 | * |
||
617 | * @param int $options |
||
618 | * |
||
619 | * @return string |
||
620 | */ |
||
621 | public function toJson($options = 0) |
||
625 | |||
626 | /** |
||
627 | * Convert the model instance to an array. |
||
628 | * |
||
629 | * @return array |
||
630 | */ |
||
631 | public function toArray() |
||
635 | |||
636 | /** |
||
637 | * Dynamically set attributes on the model. |
||
638 | * |
||
639 | * @param string $key |
||
640 | * @param mixed $value |
||
641 | * |
||
642 | * @return void |
||
643 | */ |
||
644 | public function __set($key, $value) |
||
648 | |||
649 | /** |
||
650 | * Dynamically retrieve attributes on the model. |
||
651 | * |
||
652 | * @param string $key |
||
653 | * |
||
654 | * @return mixed |
||
655 | */ |
||
656 | public function __get($key) |
||
660 | |||
661 | /** |
||
662 | * Determine if an attribute exists on the model. |
||
663 | * |
||
664 | * @param string $key |
||
665 | * |
||
666 | * @return bool |
||
667 | */ |
||
668 | public function __isset($key) |
||
672 | |||
673 | /** |
||
674 | * Unset an attribute on the model. |
||
675 | * |
||
676 | * @param string $key |
||
677 | * |
||
678 | * @return void |
||
679 | */ |
||
680 | public function __unset($key) |
||
684 | |||
685 | /** |
||
686 | * Set the value for a given offset. |
||
687 | * |
||
688 | * @param mixed $offset |
||
689 | * @param mixed $value |
||
690 | * |
||
691 | * @return void |
||
692 | */ |
||
693 | public function offsetSet($offset, $value) |
||
697 | |||
698 | /** |
||
699 | * Get the value for a given offset. |
||
700 | * |
||
701 | * @param mixed $offset |
||
702 | * |
||
703 | * @return mixed |
||
704 | */ |
||
705 | public function offsetGet($offset) |
||
709 | |||
710 | /** |
||
711 | * Determine if the given attribute exists. |
||
712 | * |
||
713 | * @param mixed $offset |
||
714 | * |
||
715 | * @return bool |
||
716 | */ |
||
717 | public function offsetExists($offset) |
||
721 | |||
722 | /** |
||
723 | * Unset the value for a given offset. |
||
724 | * |
||
725 | * @param mixed $offset |
||
726 | * |
||
727 | * @return void |
||
728 | */ |
||
729 | public function offsetUnset($offset) |
||
733 | |||
734 | /** |
||
735 | * Handle dynamic method calls into the model. |
||
736 | * |
||
737 | * @param string $method |
||
738 | * @param array $parameters |
||
739 | * |
||
740 | * @return mixed |
||
741 | */ |
||
742 | public function __call($method, $parameters) |
||
748 | |||
749 | /** |
||
750 | * Handle dynamic static method calls into the method. |
||
751 | * |
||
752 | * @param string $method |
||
753 | * @param array $parameters |
||
754 | * |
||
755 | * @return mixed |
||
756 | */ |
||
757 | public static function __callStatic($method, $parameters) |
||
763 | |||
764 | /** |
||
765 | * Convert the model to its string representation. |
||
766 | * |
||
767 | * @return string |
||
768 | */ |
||
769 | public function __toString() |
||
773 | } |
||
774 |