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 |
||
| 7 | trait HasMeta |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @return \Illuminate\Database\Eloquent\Relations\morphMany |
||
| 11 | */ |
||
| 12 | public function meta() |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Get meta by key. |
||
| 19 | * |
||
| 20 | * @param $key |
||
| 21 | * @param $group null |
||
| 22 | * @return mixed |
||
| 23 | */ |
||
| 24 | public function getMeta($key, $group = null) |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Set meta. |
||
| 34 | * |
||
| 35 | * @param $key |
||
| 36 | * @param $value |
||
| 37 | * @param null $group |
||
| 38 | * @return mixed |
||
| 39 | */ |
||
| 40 | View Code Duplication | public function setMeta($key, $value, $group = null) |
|
| 48 | |||
| 49 | /** |
||
| 50 | * Update Meta. |
||
| 51 | * |
||
| 52 | * @param $meta |
||
| 53 | * @param $key |
||
| 54 | * @param $value |
||
| 55 | * @param null $group |
||
| 56 | * @return mixed |
||
| 57 | */ |
||
| 58 | View Code Duplication | public function updateMeta($meta, $key, $value, $group = null) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Delete meta. |
||
| 74 | * |
||
| 75 | * @param $key |
||
| 76 | * @param null $group |
||
| 77 | * @return mixed |
||
| 78 | */ |
||
| 79 | public function unsetMeta($key, $group = null) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Alias for unsetMeta method. |
||
| 86 | * |
||
| 87 | * @param $key |
||
| 88 | * @param null $group |
||
| 89 | * @return mixed |
||
| 90 | */ |
||
| 91 | public function removeMeta($key, $group = null) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Remove by id. |
||
| 101 | * |
||
| 102 | * @param $id |
||
| 103 | */ |
||
| 104 | public function removeMetaById($id) |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Remove group of meta. |
||
| 111 | * |
||
| 112 | * @param $groups |
||
| 113 | * @return $this |
||
| 114 | */ |
||
| 115 | View Code Duplication | public function removeMetaGroups($groups) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * @param $group |
||
| 130 | * @return $this |
||
| 131 | */ |
||
| 132 | public function removeGroup($group) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Get other meta. |
||
| 139 | * |
||
| 140 | * @return mixed |
||
| 141 | */ |
||
| 142 | public function getOtherMeta() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Get other meta. |
||
| 149 | * |
||
| 150 | * @alias: self::getOtherMeta() |
||
| 151 | * @return mixed |
||
| 152 | */ |
||
| 153 | public function getSimpleMeta() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Get other meta. |
||
| 160 | * |
||
| 161 | * @return mixed |
||
| 162 | */ |
||
| 163 | public function metaWithoutGroup() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Get all meta from specific group. |
||
| 170 | * |
||
| 171 | * @param $group |
||
| 172 | * @return mixed |
||
| 173 | */ |
||
| 174 | public function getMetaFromGroup($group) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param $group |
||
| 181 | * @return mixed |
||
| 182 | */ |
||
| 183 | public function getMetaGroup($group) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Get meta repository |
||
| 190 | * |
||
| 191 | * @return Repository |
||
| 192 | */ |
||
| 193 | private function getRepository() |
||
| 197 | } |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.