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 |
||
| 8 | class EmbedsMany extends EmbedsOne |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * Match a single database row's attributes to a single |
||
| 12 | * object, and return the updated attributes. |
||
| 13 | * |
||
| 14 | * @param array $attributes |
||
| 15 | * |
||
| 16 | * @return array |
||
| 17 | */ |
||
| 18 | public function matchSingleResult(array $attributes) : array |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Match array attribute from parent to an embedded object, |
||
| 31 | * and return the updated attributes. |
||
| 32 | * |
||
| 33 | * @param array $attributes |
||
| 34 | * |
||
| 35 | * @return array |
||
| 36 | */ |
||
| 37 | View Code Duplication | protected function matchAsArray(array $attributes) : array |
|
| 51 | |||
| 52 | /** |
||
| 53 | * Build an embedded collection and returns it. |
||
| 54 | * |
||
| 55 | * @param array $rows |
||
| 56 | * |
||
| 57 | * @return Collection |
||
| 58 | */ |
||
| 59 | protected function buildEmbeddedCollection($rows) : Collection |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Transform embedded object into db column(s). |
||
| 72 | * |
||
| 73 | * @param mixed $object |
||
| 74 | * |
||
| 75 | * @return array $columns |
||
| 76 | */ |
||
| 77 | public function normalize($objects) : array |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Normalize object an array containing raw attributes |
||
| 88 | * |
||
| 89 | * @param mixed $object |
||
| 90 | * @return array |
||
| 91 | */ |
||
| 92 | protected function normalizeAsArray($objects) : array |
||
| 114 | } |
||
| 115 |
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.