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 |
||
| 12 | class BelongsTo extends Relation { |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Set the default keys for the relation if they have not yet been set. |
||
| 16 | */ |
||
| 17 | View Code Duplication | protected function setDefaultKeys() { |
|
| 24 | |||
| 25 | /** |
||
| 26 | * Retrieve the default filter for this relation. |
||
| 27 | * |
||
| 28 | * @return array |
||
| 29 | */ |
||
| 30 | protected function defaultConstraint() { |
||
| 31 | return array($this->localKey => $this->parent->get($this->foreignKey)); |
||
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Eagerly load the related models for the given parent instances. |
||
| 36 | * |
||
| 37 | * Returns the given instances with their related models loaded. |
||
| 38 | * |
||
| 39 | * @param array $instances |
||
| 40 | * @param string $name TODO: Remove this and store as a property |
||
| 41 | * @return array |
||
| 42 | */ |
||
| 43 | View Code Duplication | public function eager(array $instances, $name) { |
|
| 70 | |||
| 71 | /** |
||
| 72 | * Retrieve the related model. |
||
| 73 | * |
||
| 74 | * @return Record|null |
||
| 75 | */ |
||
| 76 | public function retrieve() { |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Associate the given model. |
||
| 84 | * |
||
| 85 | * @param Record $instance |
||
| 86 | * @return bool |
||
| 87 | */ |
||
| 88 | public function associate(Record $instance) { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Dissociate the related model. |
||
| 98 | * |
||
| 99 | * @return bool |
||
| 100 | */ |
||
| 101 | public function dissociate() { |
||
| 107 | |||
| 108 | } |
||
| 109 |
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.