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 |
||
| 5 | class EmbedsOne extends EmbeddedRelationship |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * The relation attribute on the parent object. |
||
| 9 | * |
||
| 10 | * @var string |
||
| 11 | */ |
||
| 12 | protected $relation; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Transform attributes into embedded object(s), and |
||
| 16 | * match it into the given resultset. |
||
| 17 | * |
||
| 18 | * @param array $results |
||
| 19 | * |
||
| 20 | * @return array |
||
| 21 | */ |
||
| 22 | public function match(array $results) : array |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Match a single database row's attributes to a single |
||
| 29 | * object, and return the updated attributes. |
||
| 30 | * |
||
| 31 | * @param array $attributes |
||
| 32 | * |
||
| 33 | * @return array |
||
| 34 | */ |
||
| 35 | public function matchSingleResult(array $attributes) : array |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Match array attribute from parent to an embedded object, |
||
| 42 | * and return the updated attributes. |
||
| 43 | * |
||
| 44 | * @param array $attributes |
||
| 45 | * |
||
| 46 | * @return array |
||
| 47 | */ |
||
| 48 | View Code Duplication | protected function matchAsArray(array $attributes) : array |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Transform attributes from the parent entity result into |
||
| 65 | * an embedded object, and return the updated attributes. |
||
| 66 | * |
||
| 67 | * @param array $attributes |
||
| 68 | * |
||
| 69 | * @return array |
||
| 70 | */ |
||
| 71 | protected function matchAsAttributes(array $attributes) : array |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Return a dictionnary of attributes key on parent Entity. |
||
| 100 | * |
||
| 101 | * @return array |
||
| 102 | */ |
||
| 103 | protected function getAttributesDictionnary() : array |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Transform embedded object into DB column(s). |
||
| 120 | * |
||
| 121 | * @param mixed $object |
||
| 122 | * |
||
| 123 | * @return array $columns |
||
| 124 | */ |
||
| 125 | public function normalize($object) : array |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Normalize object an array containing raw attributes |
||
| 132 | * |
||
| 133 | * @param mixed $object |
||
| 134 | * @return array |
||
| 135 | */ |
||
| 136 | protected function normalizeAsArray($object) : array |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Normalize object as parent's attributes |
||
| 145 | * |
||
| 146 | * @param mixed $object |
||
| 147 | * @return array |
||
| 148 | */ |
||
| 149 | protected function normalizeAsAttributes($object) : array |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Set all object attributes to null. |
||
| 170 | * |
||
| 171 | * @return array |
||
| 172 | */ |
||
| 173 | protected function nullObjectAttributes() : array |
||
| 185 | } |
||
| 186 |
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.