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 BIP32Path implements \ArrayAccess { |
||
| 13 | protected $path; |
||
| 14 | |||
| 15 | public function __construct($path) { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @return int |
||
| 25 | */ |
||
| 26 | public function depth() { |
||
| 29 | |||
| 30 | public function insert($insert, $offset) { |
||
| 37 | |||
| 38 | /** |
||
| 39 | * increase the last level of the path by 1 and return the new path |
||
| 40 | * |
||
| 41 | * @return BIP32Path |
||
| 42 | */ |
||
| 43 | public function next() { |
||
| 63 | |||
| 64 | /** |
||
| 65 | * pop off one level of the path and return the new path |
||
| 66 | * |
||
| 67 | * @return BIP32Path |
||
| 68 | */ |
||
| 69 | public function parent() { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * get child $child of the current path and return the new path |
||
| 83 | * |
||
| 84 | * @param $child |
||
| 85 | * @return BIP32Path |
||
| 86 | */ |
||
| 87 | public function child($child) { |
||
| 94 | |||
| 95 | /** |
||
| 96 | * pop off one level of the path and add $last and return the new path |
||
| 97 | * |
||
| 98 | * @param $last |
||
| 99 | * @return BIP32Path |
||
| 100 | */ |
||
| 101 | public function last($last) { |
||
| 109 | |||
| 110 | /** |
||
| 111 | * harden the last level of the path and return the new path |
||
| 112 | * |
||
| 113 | * @return BIP32Path |
||
| 114 | */ |
||
| 115 | View Code Duplication | public function hardened() { |
|
| 130 | |||
| 131 | /** |
||
| 132 | * unharden the last level of the path and return the new path |
||
| 133 | * |
||
| 134 | * @return BIP32Path |
||
| 135 | */ |
||
| 136 | View Code Duplication | public function unhardenedLast() { |
|
| 147 | |||
| 148 | /** |
||
| 149 | * unharden all levels of the path and return the new path |
||
| 150 | * |
||
| 151 | * @return BIP32Path |
||
| 152 | */ |
||
| 153 | public function unhardenedPath() { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * change the path to be for the public key (starting with M/) and return the new path |
||
| 165 | * |
||
| 166 | * @return BIP32Path |
||
| 167 | */ |
||
| 168 | View Code Duplication | public function publicPath() { |
|
| 179 | |||
| 180 | /** |
||
| 181 | * change the path to be for the private key (starting with m/) and return the new path |
||
| 182 | * |
||
| 183 | * @return BIP32Path |
||
| 184 | */ |
||
| 185 | View Code Duplication | public function privatePath() { |
|
| 196 | |||
| 197 | /** |
||
| 198 | * get the string representation of the path |
||
| 199 | * |
||
| 200 | * @return string |
||
| 201 | */ |
||
| 202 | public function getPath() { |
||
| 205 | |||
| 206 | /** |
||
| 207 | * get the last part of the path |
||
| 208 | * |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | public function getLast() { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * check if the last level of the path is hardened |
||
| 217 | * |
||
| 218 | * @return bool |
||
| 219 | */ |
||
| 220 | public function isHardened() { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * check if the last level of the path is hardened |
||
| 230 | * |
||
| 231 | * @return bool |
||
| 232 | */ |
||
| 233 | public function isPublicPath() { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * check if this path is parent path of the provided path |
||
| 241 | * |
||
| 242 | * @param string|BIP32Path $path |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | public function isParentOf($path) { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * static method to initialize class |
||
| 253 | * |
||
| 254 | * @param $path |
||
| 255 | * @return BIP32Path |
||
| 256 | */ |
||
| 257 | public static function path($path) { |
||
| 264 | |||
| 265 | public function getKeyIndex() { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * count the levels in the path (including master) |
||
| 271 | * |
||
| 272 | * @return int |
||
| 273 | */ |
||
| 274 | public function count() { |
||
| 277 | |||
| 278 | |||
| 279 | public function offsetExists($offset) { |
||
| 282 | |||
| 283 | public function offsetGet($offset) { |
||
| 286 | |||
| 287 | public function offsetSet($offset, $value) { |
||
| 290 | |||
| 291 | public function offsetUnset($offset) { |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @return string |
||
| 297 | */ |
||
| 298 | public function __toString() { |
||
| 301 | } |
||
| 302 |
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.