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 | 29 | 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) |
|
| 119 | |||
| 120 | /** |
||
| 121 | * Cast data into int, float or string. |
||
| 122 | * |
||
| 123 | * <p> |
||
| 124 | * <br /> |
||
| 125 | * INFO: install / use "mysqlnd"-driver for better performance |
||
| 126 | * </p> |
||
| 127 | * |
||
| 128 | * @param array|object $data |
||
| 129 | * |
||
| 130 | * @return array|object|false <p><strong>false</strong> on error</p> |
||
| 131 | */ |
||
| 132 | 25 | private function cast(&$data) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Fetch all results as array. |
||
| 192 | * |
||
| 193 | * @return array |
||
| 194 | */ |
||
| 195 | 10 | View Code Duplication | public function fetchAllArray() |
| 215 | |||
| 216 | /** |
||
| 217 | * Fetch all results as "Arrayy"-object. |
||
| 218 | * |
||
| 219 | * @return Arrayy |
||
| 220 | */ |
||
| 221 | 3 | View Code Duplication | public function fetchAllArrayy() |
| 241 | |||
| 242 | /** |
||
| 243 | * Check if the result is empty. |
||
| 244 | * |
||
| 245 | * @return bool |
||
| 246 | */ |
||
| 247 | 12 | public function is_empty() |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Reset the offset (data_seek) for the results. |
||
| 258 | * |
||
| 259 | * @return Result |
||
| 260 | */ |
||
| 261 | 11 | public function reset() |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Fetch all results as "json"-string. |
||
| 272 | * |
||
| 273 | * @return string |
||
| 274 | */ |
||
| 275 | 1 | public function json() |
|
| 281 | |||
| 282 | /** |
||
| 283 | * __destruct |
||
| 284 | * |
||
| 285 | */ |
||
| 286 | 29 | public function __destruct() |
|
| 290 | |||
| 291 | /** |
||
| 292 | * free the memory |
||
| 293 | */ |
||
| 294 | 29 | public function free() |
|
| 298 | |||
| 299 | /** |
||
| 300 | * alias for "Result->fetch()" |
||
| 301 | * |
||
| 302 | * @see Result::fetch() |
||
| 303 | * |
||
| 304 | * @return array|object|false <p><strong>false</strong> on error</p> |
||
| 305 | */ |
||
| 306 | 1 | public function get() |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Fetch. |
||
| 313 | * |
||
| 314 | * <p> |
||
| 315 | * <br /> |
||
| 316 | * INFO: this will return an object by default, not an array<br /> |
||
| 317 | * and you can change the behaviour via "Result->setDefaultResultType()" |
||
| 318 | * </p> |
||
| 319 | * |
||
| 320 | * @param $reset |
||
| 321 | * |
||
| 322 | * @return array|object|false <p><strong>false</strong> on error</p> |
||
| 323 | */ |
||
| 324 | 2 | public function fetch($reset = false) |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Fetch as object. |
||
| 341 | * |
||
| 342 | * @param string $class |
||
| 343 | * @param null|array $params |
||
| 344 | * @param bool $reset |
||
| 345 | * |
||
| 346 | * @return object|false <p><strong>false</strong> on error</p> |
||
| 347 | */ |
||
| 348 | 7 | public function fetchObject($class = '', $params = null, $reset = false) |
|
| 364 | |||
| 365 | /** |
||
| 366 | * Fetch as array. |
||
| 367 | * |
||
| 368 | * @param bool $reset |
||
| 369 | * |
||
| 370 | * @return array|false <p><strong>false</strong> on error</p> |
||
| 371 | */ |
||
| 372 | 12 | View Code Duplication | public function fetchArray($reset = false) |
| 373 | { |
||
| 374 | 12 | if ($reset === true) { |
|
| 375 | 1 | $this->reset(); |
|
| 376 | 1 | } |
|
| 377 | |||
| 378 | 12 | $row = \mysqli_fetch_assoc($this->_result); |
|
| 379 | 12 | if ($row) { |
|
| 380 | 12 | return $this->cast($row); |
|
| 381 | } |
||
| 382 | |||
| 383 | 1 | return false; |
|
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Fetch as "Arrayy"-object. |
||
| 388 | * |
||
| 389 | * @param bool $reset |
||
| 390 | * |
||
| 391 | * @return Arrayy|false <p><strong>false</strong> on error</p> |
||
| 392 | */ |
||
| 393 | 1 | View Code Duplication | public function fetchArrayy($reset = false) |
| 406 | |||
| 407 | /** |
||
| 408 | * alias for "Result->fetchAll()" |
||
| 409 | * |
||
| 410 | * @see Result::fetchAll() |
||
| 411 | * |
||
| 412 | * @return array |
||
| 413 | */ |
||
| 414 | 1 | public function getAll() |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Fetch all results. |
||
| 421 | * |
||
| 422 | * <p> |
||
| 423 | * <br /> |
||
| 424 | * INFO: this will return an object by default, not an array<br /> |
||
| 425 | * and you can change the behaviour via "Result->setDefaultResultType()" |
||
| 426 | * </p> |
||
| 427 | * |
||
| 428 | * @return array |
||
| 429 | */ |
||
| 430 | 2 | public function fetchAll() |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Fetch all results as array with objects. |
||
| 447 | * |
||
| 448 | * @param string $class |
||
| 449 | * @param null|array $params |
||
| 450 | * |
||
| 451 | * @return array |
||
| 452 | */ |
||
| 453 | 3 | public function fetchAllObject($class = '', $params = null) |
|
| 481 | |||
| 482 | /** |
||
| 483 | * alias for "Result->fetchAllObject()" |
||
| 484 | * |
||
| 485 | * @see Result::fetchAllObject() |
||
| 486 | * |
||
| 487 | * @return array of mysql-objects |
||
| 488 | */ |
||
| 489 | 1 | public function getObject() |
|
| 493 | |||
| 494 | /** |
||
| 495 | * alias for "Result->fetchAllArrayy()" |
||
| 496 | * |
||
| 497 | * @see Result::fetchAllArrayy() |
||
| 498 | * |
||
| 499 | * @return Arrayy |
||
| 500 | */ |
||
| 501 | public function getArrayy() |
||
| 505 | |||
| 506 | /** |
||
| 507 | * alias for "Result->fetchAllArray()" |
||
| 508 | * |
||
| 509 | * @see Result::fetchAllArray() |
||
| 510 | * |
||
| 511 | * @return array |
||
| 512 | */ |
||
| 513 | 1 | public function getArray() |
|
| 517 | |||
| 518 | /** |
||
| 519 | * Fetch a single column as an 1-dimension array. |
||
| 520 | * |
||
| 521 | * @param string $column |
||
| 522 | * @param bool $skipNullValues <p>Skip "NULL"-values. | default: false</p> |
||
| 523 | * |
||
| 524 | * @return array <p>Return an empty array if the "$column" wasn't found</p> |
||
| 525 | */ |
||
| 526 | 1 | public function fetchAllColumn($column, $skipNullValues = false) |
|
| 530 | |||
| 531 | /** |
||
| 532 | * alias for "Result->fetchAllColumn()" |
||
| 533 | * |
||
| 534 | * @see Result::fetchAllColumn() |
||
| 535 | * |
||
| 536 | * @param string $column |
||
| 537 | * @param bool $skipNullValues |
||
| 538 | * |
||
| 539 | * @return array |
||
| 540 | */ |
||
| 541 | public function getAllColumn($column, $skipNullValues = false) |
||
| 545 | |||
| 546 | /** |
||
| 547 | * alias for "Result->fetchColumn()" |
||
| 548 | * |
||
| 549 | * @see Result::fetchColumn() |
||
| 550 | * |
||
| 551 | * @param $column |
||
| 552 | * @param $asArray |
||
| 553 | * @param $skipNullValues |
||
| 554 | * |
||
| 555 | * @return string|array <p>Return a empty string or an empty array if the "$column" wasn't found, depend on |
||
| 556 | * "$asArray"</p> |
||
| 557 | */ |
||
| 558 | 1 | public function getColumn($column, $skipNullValues = true, $asArray = false) |
|
| 562 | |||
| 563 | /** |
||
| 564 | * Fetch a single column as string (or as 1-dimension array). |
||
| 565 | * |
||
| 566 | * @param string $column |
||
| 567 | * @param bool $skipNullValues <p>Skip "NULL"-values. | default: true</p> |
||
| 568 | * @param bool $asArray <p>Get all values and not only the last one. | default: false</p> |
||
| 569 | * |
||
| 570 | * @return string|array <p>Return a empty string or an empty array if the "$column" wasn't found, depend on |
||
| 571 | * "$asArray"</p> |
||
| 572 | */ |
||
| 573 | 2 | public function fetchColumn($column = '', $skipNullValues = true, $asArray = false) |
|
| 621 | |||
| 622 | /** |
||
| 623 | * Get the current "num_rows" as string. |
||
| 624 | * |
||
| 625 | * @return string |
||
| 626 | */ |
||
| 627 | public function __toString() |
||
| 631 | } |
||
| 632 |
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.