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:
Complex classes like Result often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Result, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | final class Result |
||
| 15 | { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var int |
||
| 19 | */ |
||
| 20 | public $num_rows; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | public $sql; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var \mysqli_result |
||
| 29 | */ |
||
| 30 | private $_result; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | private $_default_result_type = 'object'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Result constructor. |
||
| 39 | * |
||
| 40 | * @param string $sql |
||
| 41 | * @param \mysqli_result $result |
||
| 42 | */ |
||
| 43 | 28 | public function __construct($sql = '', \mysqli_result $result) |
|
| 50 | |||
| 51 | /** |
||
| 52 | * @return string |
||
| 53 | */ |
||
| 54 | 1 | public function getDefaultResultType() |
|
| 58 | |||
| 59 | /** |
||
| 60 | * You can set the default result-type to 'object', 'array' or 'Arrayy'. |
||
| 61 | * |
||
| 62 | * INFO: used for "fetch()" and "fetchAll()" |
||
| 63 | * |
||
| 64 | * @param string $default_result_type |
||
| 65 | */ |
||
| 66 | 2 | public function setDefaultResultType($default_result_type = 'object') |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Fetch data as a key/value pair array. |
||
| 81 | * |
||
| 82 | * <p> |
||
| 83 | * <br /> |
||
| 84 | * INFO: both "key" and "value" must exists in the fetched data |
||
| 85 | * the key will be the new key of the result-array |
||
| 86 | * <br /><br /> |
||
| 87 | * </p> |
||
| 88 | * |
||
| 89 | * e.g.: |
||
| 90 | * <code> |
||
| 91 | * fetchArrayPair('some_id', 'some_value'); |
||
| 92 | * // array(127 => 'some value', 128 => 'some other value') |
||
| 93 | * </code> |
||
| 94 | * |
||
| 95 | * @param string $key |
||
| 96 | * @param string $value |
||
| 97 | * |
||
| 98 | * @return array |
||
| 99 | */ |
||
| 100 | 1 | public function fetchArrayPair($key, $value) |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Cast data into int, float or string. |
||
| 118 | * |
||
| 119 | * <p> |
||
| 120 | * <br /> |
||
| 121 | * INFO: install / use "mysqlnd"-driver for better performance |
||
| 122 | * </p> |
||
| 123 | * |
||
| 124 | * @param array|object $data |
||
| 125 | * |
||
| 126 | * @return array|object|false false on error |
||
| 127 | */ |
||
| 128 | 24 | private function cast(&$data) |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Fetch all results as array. |
||
| 188 | * |
||
| 189 | * @return array |
||
| 190 | */ |
||
| 191 | 10 | View Code Duplication | public function fetchAllArray() |
| 211 | |||
| 212 | /** |
||
| 213 | * Fetch all results as "Arrayy"-object. |
||
| 214 | * |
||
| 215 | * @return Arrayy |
||
| 216 | */ |
||
| 217 | 1 | View Code Duplication | public function fetchAllArrayy() |
| 237 | |||
| 238 | /** |
||
| 239 | * Check if the result is empty. |
||
| 240 | * |
||
| 241 | * @return bool |
||
| 242 | */ |
||
| 243 | 12 | public function is_empty() |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Reset the offset (data_seek) for the results. |
||
| 254 | * |
||
| 255 | * @return Result |
||
| 256 | */ |
||
| 257 | 11 | public function reset() |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Fetch all results as "json"-string. |
||
| 268 | * |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | 1 | public function json() |
|
| 277 | |||
| 278 | /** |
||
| 279 | * __destruct |
||
| 280 | * |
||
| 281 | */ |
||
| 282 | 28 | public function __destruct() |
|
| 286 | |||
| 287 | /** |
||
| 288 | * free the memory |
||
| 289 | */ |
||
| 290 | 28 | public function free() |
|
| 294 | |||
| 295 | /** |
||
| 296 | * alias for "Result->fetch()" |
||
| 297 | * |
||
| 298 | * @see Result::fetch() |
||
| 299 | * |
||
| 300 | * @return array|object|false false on error |
||
| 301 | */ |
||
| 302 | 1 | public function get() |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Fetch. |
||
| 309 | * |
||
| 310 | * <p> |
||
| 311 | * <br /> |
||
| 312 | * INFO: this will return an object by default, not an array<br /> |
||
| 313 | * and you can change the behaviour via "Result->setDefaultResultType()" |
||
| 314 | * </p> |
||
| 315 | * |
||
| 316 | * @param $reset |
||
| 317 | * |
||
| 318 | * @return array|object|false false on error |
||
| 319 | */ |
||
| 320 | 2 | public function fetch($reset = false) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Fetch as object. |
||
| 337 | * |
||
| 338 | * @param string $class |
||
| 339 | * @param null|array $params |
||
| 340 | * @param bool $reset |
||
| 341 | * |
||
| 342 | * @return object|false false on error |
||
| 343 | */ |
||
| 344 | 7 | public function fetchObject($class = '', $params = null, $reset = false) |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Fetch as array. |
||
| 363 | * |
||
| 364 | * @param bool $reset |
||
| 365 | * |
||
| 366 | * @return array|false false on error |
||
| 367 | */ |
||
| 368 | 11 | View Code Duplication | public function fetchArray($reset = false) |
| 381 | |||
| 382 | /** |
||
| 383 | * Fetch as "Arrayy"-object. |
||
| 384 | * |
||
| 385 | * @param bool $reset |
||
| 386 | * |
||
| 387 | * @return Arrayy|false false on error |
||
| 388 | */ |
||
| 389 | 1 | View Code Duplication | public function fetchArrayy($reset = false) |
| 402 | |||
| 403 | /** |
||
| 404 | * alias for "Result->fetchAll()" |
||
| 405 | * |
||
| 406 | * @see Result::fetchAll() |
||
| 407 | * |
||
| 408 | * @return array |
||
| 409 | */ |
||
| 410 | 1 | public function getAll() |
|
| 414 | |||
| 415 | /** |
||
| 416 | * Fetch all results. |
||
| 417 | * |
||
| 418 | * <p> |
||
| 419 | * <br /> |
||
| 420 | * INFO: this will return an object by default, not an array<br /> |
||
| 421 | * and you can change the behaviour via "Result->setDefaultResultType()" |
||
| 422 | * </p> |
||
| 423 | * |
||
| 424 | * @return array |
||
| 425 | */ |
||
| 426 | 2 | public function fetchAll() |
|
| 440 | |||
| 441 | /** |
||
| 442 | * Fetch all results as array with objects. |
||
| 443 | * |
||
| 444 | * @param string $class |
||
| 445 | * @param null|array $params |
||
| 446 | * |
||
| 447 | * @return array |
||
| 448 | */ |
||
| 449 | 3 | public function fetchAllObject($class = '', $params = null) |
|
| 477 | |||
| 478 | /** |
||
| 479 | * alias for "Result->fetchAllObject()" |
||
| 480 | * |
||
| 481 | * @see Result::fetchAllObject() |
||
| 482 | * |
||
| 483 | * @return array of mysql-objects |
||
| 484 | */ |
||
| 485 | 1 | public function getObject() |
|
| 489 | |||
| 490 | /** |
||
| 491 | * alias for "Result->fetchAllArrayy()" |
||
| 492 | * |
||
| 493 | * @see Result::fetchAllArrayy() |
||
| 494 | * |
||
| 495 | * @return Arrayy |
||
| 496 | */ |
||
| 497 | public function getArrayy() |
||
| 501 | |||
| 502 | /** |
||
| 503 | * alias for "Result->fetchAllArray()" |
||
| 504 | * |
||
| 505 | * @see Result::fetchAllArray() |
||
| 506 | * |
||
| 507 | * @return array |
||
| 508 | */ |
||
| 509 | 1 | public function getArray() |
|
| 513 | |||
| 514 | /** |
||
| 515 | * alias for "Result->fetchColumn()" |
||
| 516 | * |
||
| 517 | * @see Result::fetchColumn() |
||
| 518 | * |
||
| 519 | * @param $key |
||
| 520 | * |
||
| 521 | * @return string |
||
| 522 | */ |
||
| 523 | 1 | public function getColumn($key) |
|
| 527 | |||
| 528 | /** |
||
| 529 | * Fetch a single column in an 1-dimension array. |
||
| 530 | * |
||
| 531 | * @param string $column |
||
| 532 | * |
||
| 533 | * @return string empty string if the $column wasn't found |
||
| 534 | */ |
||
| 535 | 2 | public function fetchColumn($column = '') |
|
| 548 | |||
| 549 | /** |
||
| 550 | * Get the current "num_rows" as string. |
||
| 551 | * |
||
| 552 | * @return string |
||
| 553 | */ |
||
| 554 | public function __toString() |
||
| 558 | } |
||
| 559 |
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.