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:
| 1 | <?php |
||
| 13 | class Has extends Relation |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Set the default keys for the relation if they have not yet been set. |
||
| 17 | */ |
||
| 18 | protected function setDefaultKeys() |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Eagerly load the related models for the given parent instances. |
||
| 29 | * |
||
| 30 | * Returns the given instances with their related models loaded. |
||
| 31 | * |
||
| 32 | * @param array $instances |
||
| 33 | * @return array |
||
| 34 | */ |
||
| 35 | View Code Duplication | public function eager(array $instances) |
|
| 63 | |||
| 64 | /** |
||
| 65 | * Retrieve the related model. |
||
| 66 | * |
||
| 67 | * @return Record|null |
||
| 68 | */ |
||
| 69 | public function retrieve() |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Associate the given model. |
||
| 76 | * |
||
| 77 | * Dissociates any currently associated model beforehand. |
||
| 78 | * |
||
| 79 | * Returns the number of successfully associated models. |
||
| 80 | * |
||
| 81 | * @param Record[]|Record $instances |
||
| 82 | * @return int |
||
| 83 | */ |
||
| 84 | View Code Duplication | public function associate($instances) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Dissociate the related model. |
||
| 109 | * |
||
| 110 | * Returns true if the model was successfully dissociated. |
||
| 111 | * |
||
| 112 | * @param Record[]|Record $instances [optional] |
||
| 113 | * @return int |
||
| 114 | */ |
||
| 115 | public function dissociate($instances = array()) |
||
| 138 | } |
||
| 139 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.