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 implements \Countable, \SeekableIterator, \ArrayAccess |
||
| 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 int |
||
| 34 | */ |
||
| 35 | private $current_row; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var \Closure|null |
||
| 39 | */ |
||
| 40 | private $_mapper; |
||
| 41 | |||
| 42 | /** |
||
| 43 | 30 | * @var string |
|
| 44 | */ |
||
| 45 | 30 | private $_default_result_type = 'object'; |
|
| 46 | |||
| 47 | 30 | /** |
|
| 48 | 30 | * Result constructor. |
|
| 49 | 30 | * |
|
| 50 | * @param string $sql |
||
| 51 | * @param \mysqli_result $result |
||
| 52 | * @param \Closure $mapper Optional callback mapper for the "fetch_callable()" method |
||
| 53 | */ |
||
| 54 | 1 | public function __construct($sql = '', \mysqli_result $result, $mapper = null) |
|
| 65 | |||
| 66 | 2 | /** |
|
| 67 | * __destruct |
||
| 68 | * |
||
| 69 | */ |
||
| 70 | 2 | public function __destruct() |
|
| 74 | 2 | ||
| 75 | 2 | /** |
|
| 76 | 2 | * Runs a user-provided callback with the MySQLi_Result object given as |
|
| 77 | 2 | * argument and returns the result, or returns the MySQLi_Result object if |
|
| 78 | * called without an argument. |
||
| 79 | * |
||
| 80 | * @param callable $callback User-provided callback (optional) |
||
| 81 | * |
||
| 82 | * @return mixed|\mysqli_result |
||
| 83 | */ |
||
| 84 | public function __invoke($callback = null) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Get the current "num_rows" as string. |
||
| 95 | * |
||
| 96 | * @return string |
||
| 97 | */ |
||
| 98 | public function __toString() |
||
| 102 | 1 | ||
| 103 | 1 | /** |
|
| 104 | * Cast data into int, float or string. |
||
| 105 | 1 | * |
|
| 106 | * <p> |
||
| 107 | 1 | * <br /> |
|
| 108 | 1 | * INFO: install / use "mysqlnd"-driver for better performance |
|
| 109 | 1 | * </p> |
|
| 110 | 1 | * |
|
| 111 | 1 | * @param array|object $data |
|
| 112 | 1 | * |
|
| 113 | 1 | * @return array|object|false <p><strong>false</strong> on error</p> |
|
| 114 | 1 | */ |
|
| 115 | 1 | private function cast(&$data) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Countable interface implementation. |
||
| 175 | * |
||
| 176 | * @return int The number of rows in the result |
||
| 177 | */ |
||
| 178 | public function count() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Iterator interface implementation. |
||
| 185 | * |
||
| 186 | * @return mixed The current element |
||
| 187 | */ |
||
| 188 | public function current() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Iterator interface implementation. |
||
| 195 | 10 | * |
|
| 196 | * @return int The current element key (row index; zero-based) |
||
| 197 | */ |
||
| 198 | 10 | public function key() |
|
| 202 | 10 | ||
| 203 | 10 | /** |
|
| 204 | 10 | * Iterator interface implementation. |
|
| 205 | 10 | * |
|
| 206 | * @return void |
||
| 207 | */ |
||
| 208 | 10 | public function next() |
|
| 212 | |||
| 213 | 10 | /** |
|
| 214 | * Iterator interface implementation. |
||
| 215 | * |
||
| 216 | * @param int $row Row position to rewind to; defaults to 0 |
||
| 217 | * |
||
| 218 | * @return void |
||
| 219 | */ |
||
| 220 | public function rewind($row = 0) |
||
| 226 | |||
| 227 | 3 | /** |
|
| 228 | 3 | * Moves the internal pointer to the specified row position. |
|
| 229 | 3 | * |
|
| 230 | 3 | * @param int $row Row position; zero-based and set to 0 by default |
|
| 231 | 3 | * |
|
| 232 | * @return bool Boolean true on success, false otherwise |
||
| 233 | */ |
||
| 234 | 3 | public function seek($row = 0) |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Iterator interface implementation. |
||
| 245 | * |
||
| 246 | * @return bool Boolean true if the current index is valid, false otherwise |
||
| 247 | 12 | */ |
|
| 248 | public function valid() |
||
| 252 | |||
| 253 | 1 | /** |
|
| 254 | * Fetch. |
||
| 255 | * |
||
| 256 | * <p> |
||
| 257 | * <br /> |
||
| 258 | * INFO: this will return an object by default, not an array<br /> |
||
| 259 | * and you can change the behaviour via "Result->setDefaultResultType()" |
||
| 260 | * </p> |
||
| 261 | 11 | * |
|
| 262 | * @param $reset |
||
| 263 | 11 | * |
|
| 264 | 11 | * @return array|object|false <p><strong>false</strong> on error</p> |
|
| 265 | 11 | */ |
|
| 266 | public function fetch($reset = false) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Fetch all results. |
||
| 283 | * |
||
| 284 | * <p> |
||
| 285 | * <br /> |
||
| 286 | 30 | * INFO: this will return an object by default, not an array<br /> |
|
| 287 | * and you can change the behaviour via "Result->setDefaultResultType()" |
||
| 288 | 30 | * </p> |
|
| 289 | 30 | * |
|
| 290 | * @return array |
||
| 291 | */ |
||
| 292 | public function fetchAll() |
||
| 306 | 1 | ||
| 307 | /** |
||
| 308 | 1 | * Fetch all results as array. |
|
| 309 | * |
||
| 310 | * @return array |
||
| 311 | */ |
||
| 312 | View Code Duplication | public function fetchAllArray() |
|
| 332 | 2 | ||
| 333 | /** |
||
| 334 | * Fetch all results as "Arrayy"-object. |
||
| 335 | * |
||
| 336 | 2 | * @return Arrayy |
|
| 337 | */ |
||
| 338 | View Code Duplication | public function fetchAllArrayy() |
|
| 358 | 7 | ||
| 359 | 1 | /** |
|
| 360 | * Fetch a single column as an 1-dimension array. |
||
| 361 | * |
||
| 362 | 7 | * @param string $column |
|
| 363 | * @param bool $skipNullValues <p>Skip "NULL"-values. | default: false</p> |
||
| 364 | * |
||
| 365 | * @return array <p>Return an empty array if the "$column" wasn't found</p> |
||
| 366 | */ |
||
| 367 | public function fetchAllColumn($column, $skipNullValues = false) |
||
| 371 | |||
| 372 | 14 | /** |
|
| 373 | * Fetch all results as array with objects. |
||
| 374 | 14 | * |
|
| 375 | 1 | * @param string $class |
|
| 376 | 1 | * @param null|array $params |
|
| 377 | * |
||
| 378 | 14 | * @return array |
|
| 379 | 14 | */ |
|
| 380 | 13 | public function fetchAllObject($class = '', $params = null) |
|
| 408 | 1 | ||
| 409 | 1 | /** |
|
| 410 | * Fetch as array. |
||
| 411 | * |
||
| 412 | * @param bool $reset |
||
| 413 | * |
||
| 414 | * @return array|false <p><strong>false</strong> on error</p> |
||
| 415 | */ |
||
| 416 | View Code Duplication | public function fetchArray($reset = false) |
|
| 433 | |||
| 434 | /** |
||
| 435 | * Fetch data as a key/value pair array. |
||
| 436 | * |
||
| 437 | * <p> |
||
| 438 | 2 | * <br /> |
|
| 439 | * INFO: both "key" and "value" must exists in the fetched data |
||
| 440 | 2 | * the key will be the new key of the result-array |
|
| 441 | * <br /><br /> |
||
| 442 | 2 | * </p> |
|
| 443 | 2 | * |
|
| 444 | 2 | * e.g.: |
|
| 445 | 1 | * <code> |
|
| 446 | 1 | * fetchArrayPair('some_id', 'some_value'); |
|
| 447 | * // array(127 => 'some value', 128 => 'some other value') |
||
| 448 | * </code> |
||
| 449 | * |
||
| 450 | 2 | * @param string $key |
|
| 451 | * @param string $value |
||
| 452 | * |
||
| 453 | * @return array |
||
| 454 | */ |
||
| 455 | public function fetchArrayPair($key, $value) |
||
| 474 | 3 | ||
| 475 | /** |
||
| 476 | 1 | * Fetch as "Arrayy"-object. |
|
| 477 | 1 | * |
|
| 478 | 1 | * @param bool $reset |
|
| 479 | 1 | * |
|
| 480 | * @return Arrayy|false <p><strong>false</strong> on error</p> |
||
| 481 | 3 | */ |
|
| 482 | 3 | View Code Duplication | public function fetchArrayy($reset = false) |
| 499 | 1 | ||
| 500 | /** |
||
| 501 | * Fetch a single column as string (or as 1-dimension array). |
||
| 502 | * |
||
| 503 | * @param string $column |
||
| 504 | * @param bool $skipNullValues <p>Skip "NULL"-values. | default: true</p> |
||
| 505 | * @param bool $asArray <p>Get all values and not only the last one. | default: false</p> |
||
| 506 | * |
||
| 507 | * @return string|array <p>Return a empty string or an empty array if the "$column" wasn't found, depend on |
||
| 508 | * "$asArray"</p> |
||
| 509 | */ |
||
| 510 | public function fetchColumn($column = '', $skipNullValues = true, $asArray = false) |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Fetch as object. |
||
| 561 | * |
||
| 562 | * @param string $class |
||
| 563 | * @param null|array $params |
||
| 564 | * @param bool $reset |
||
| 565 | * |
||
| 566 | 1 | * @return object|false <p><strong>false</strong> on error</p> |
|
| 567 | */ |
||
| 568 | 1 | public function fetchObject($class = '', $params = null, $reset = false) |
|
| 584 | 2 | ||
| 585 | /** |
||
| 586 | 2 | * Fetches a row or a single column within a row. Returns null if there are |
|
| 587 | 2 | * no more rows in the result. |
|
| 588 | * |
||
| 589 | 2 | * @param int $row The row number (optional) |
|
| 590 | 2 | * @param string $column The column name (optional) |
|
| 591 | 1 | * |
|
| 592 | * @return mixed An associative array or a scalar value |
||
| 593 | 2 | */ |
|
| 594 | 1 | public function fetch_callable($row = null, $column = null) |
|
| 612 | 1 | ||
| 613 | /** |
||
| 614 | 1 | * Return rows of field information in a result set. This function is a |
|
| 615 | 1 | * basically a wrapper on the native mysqli_fetch_fields function. |
|
| 616 | 1 | * |
|
| 617 | * @param bool $as_array Return each field info as array; defaults to false |
||
| 618 | 1 | * |
|
| 619 | 1 | * @return array Array of field information each as an associative array |
|
| 620 | 1 | */ |
|
| 621 | public function fetch_fields($as_array = false) |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Returns all rows at once as a grouped array of scalar values or arrays. |
||
| 637 | * |
||
| 638 | * @param string $group The column name to use for grouping |
||
| 639 | * @param string $column The column name to use as values (optional) |
||
| 640 | * |
||
| 641 | * @return array A grouped array of scalar values or arrays |
||
| 642 | */ |
||
| 643 | View Code Duplication | public function fetch_groups($group, $column = null) |
|
| 671 | |||
| 672 | /** |
||
| 673 | * Returns all rows at once as key-value pairs. |
||
| 674 | * |
||
| 675 | * @param string $key The column name to use as keys |
||
| 676 | * @param string $column The column name to use as values (optional) |
||
| 677 | * |
||
| 678 | * @return array An array of key-value pairs |
||
| 679 | */ |
||
| 680 | View Code Duplication | public function fetch_pairs($key, $column = null) |
|
| 708 | |||
| 709 | /** |
||
| 710 | * Returns all rows at once, transposed as an array of arrays. Instead of |
||
| 711 | * returning rows of columns, this method returns columns of rows. |
||
| 712 | * |
||
| 713 | * @param string $column The column name to use as keys (optional) |
||
| 714 | * |
||
| 715 | * @return mixed A transposed array of arrays |
||
| 716 | */ |
||
| 717 | public function fetch_transpose($column = null) |
||
| 742 | |||
| 743 | /** |
||
| 744 | * Returns the first row element from the result. |
||
| 745 | * |
||
| 746 | * @param string $column The column name to use as value (optional) |
||
| 747 | * |
||
| 748 | * @return mixed A row array or a single scalar value |
||
| 749 | */ |
||
| 750 | public function first($column = null) |
||
| 758 | |||
| 759 | /** |
||
| 760 | * free the memory |
||
| 761 | */ |
||
| 762 | public function free() |
||
| 775 | |||
| 776 | /** |
||
| 777 | * alias for "Result->fetch()" |
||
| 778 | * |
||
| 779 | * @see Result::fetch() |
||
| 780 | * |
||
| 781 | * @return array|object|false <p><strong>false</strong> on error</p> |
||
| 782 | */ |
||
| 783 | public function get() |
||
| 787 | |||
| 788 | /** |
||
| 789 | * alias for "Result->fetchAll()" |
||
| 790 | * |
||
| 791 | * @see Result::fetchAll() |
||
| 792 | * |
||
| 793 | * @return array |
||
| 794 | */ |
||
| 795 | public function getAll() |
||
| 799 | |||
| 800 | /** |
||
| 801 | * alias for "Result->fetchAllColumn()" |
||
| 802 | * |
||
| 803 | * @see Result::fetchAllColumn() |
||
| 804 | * |
||
| 805 | * @param string $column |
||
| 806 | * @param bool $skipNullValues |
||
| 807 | * |
||
| 808 | * @return array |
||
| 809 | */ |
||
| 810 | public function getAllColumn($column, $skipNullValues = false) |
||
| 814 | |||
| 815 | /** |
||
| 816 | * alias for "Result->fetchAllArray()" |
||
| 817 | * |
||
| 818 | * @see Result::fetchAllArray() |
||
| 819 | * |
||
| 820 | * @return array |
||
| 821 | */ |
||
| 822 | public function getArray() |
||
| 826 | |||
| 827 | /** |
||
| 828 | * alias for "Result->fetchAllArrayy()" |
||
| 829 | * |
||
| 830 | * @see Result::fetchAllArrayy() |
||
| 831 | * |
||
| 832 | * @return Arrayy |
||
| 833 | */ |
||
| 834 | public function getArrayy() |
||
| 838 | |||
| 839 | /** |
||
| 840 | * alias for "Result->fetchColumn()" |
||
| 841 | * |
||
| 842 | * @see Result::fetchColumn() |
||
| 843 | * |
||
| 844 | * @param $column |
||
| 845 | * @param $asArray |
||
| 846 | * @param $skipNullValues |
||
| 847 | * |
||
| 848 | * @return string|array <p>Return a empty string or an empty array if the "$column" wasn't found, depend on |
||
| 849 | * "$asArray"</p> |
||
| 850 | */ |
||
| 851 | public function getColumn($column, $skipNullValues = true, $asArray = false) |
||
| 855 | |||
| 856 | /** |
||
| 857 | * @return string |
||
| 858 | */ |
||
| 859 | public function getDefaultResultType() |
||
| 863 | |||
| 864 | /** |
||
| 865 | * alias for "Result->fetchAllObject()" |
||
| 866 | * |
||
| 867 | * @see Result::fetchAllObject() |
||
| 868 | * |
||
| 869 | * @return array of mysql-objects |
||
| 870 | */ |
||
| 871 | public function getObject() |
||
| 875 | |||
| 876 | /** |
||
| 877 | * Check if the result is empty. |
||
| 878 | * |
||
| 879 | * @return bool |
||
| 880 | */ |
||
| 881 | public function is_empty() |
||
| 889 | |||
| 890 | /** |
||
| 891 | * Fetch all results as "json"-string. |
||
| 892 | * |
||
| 893 | * @return string |
||
| 894 | */ |
||
| 895 | public function json() |
||
| 901 | |||
| 902 | /** |
||
| 903 | * Returns the last row element from the result. |
||
| 904 | * |
||
| 905 | * @param string $column The column name to use as value (optional) |
||
| 906 | * |
||
| 907 | * @return mixed A row array or a single scalar value |
||
| 908 | */ |
||
| 909 | public function last($column = null) |
||
| 917 | |||
| 918 | /** |
||
| 919 | * Set the mapper... |
||
| 920 | * |
||
| 921 | * @param \Closure $callable |
||
| 922 | * |
||
| 923 | * @return $this |
||
| 924 | */ |
||
| 925 | public function map(\Closure $callable) |
||
| 931 | |||
| 932 | /** |
||
| 933 | * Alias of count(). Deprecated. |
||
| 934 | * |
||
| 935 | * @return int The number of rows in the result |
||
| 936 | */ |
||
| 937 | public function num_rows() |
||
| 941 | |||
| 942 | /** |
||
| 943 | * ArrayAccess interface implementation. |
||
| 944 | * |
||
| 945 | * @param int $offset Offset number |
||
| 946 | * |
||
| 947 | * @return bool Boolean true if offset exists, false otherwise |
||
| 948 | */ |
||
| 949 | public function offsetExists($offset) |
||
| 953 | |||
| 954 | /** |
||
| 955 | * ArrayAccess interface implementation. |
||
| 956 | * |
||
| 957 | * @param int $offset Offset number |
||
| 958 | * |
||
| 959 | * @return mixed |
||
| 960 | */ |
||
| 961 | public function offsetGet($offset) |
||
| 969 | |||
| 970 | /** |
||
| 971 | * ArrayAccess interface implementation. Not implemented by design. |
||
| 972 | * |
||
| 973 | * @param mixed $offset |
||
| 974 | * @param mixed $value |
||
| 975 | */ |
||
| 976 | public function offsetSet($offset, $value) |
||
| 980 | |||
| 981 | /** |
||
| 982 | * ArrayAccess interface implementation. Not implemented by design. |
||
| 983 | * |
||
| 984 | * @param mixed $offset |
||
| 985 | */ |
||
| 986 | public function offsetUnset($offset) |
||
| 990 | |||
| 991 | /** |
||
| 992 | * Reset the offset (data_seek) for the results. |
||
| 993 | * |
||
| 994 | * @return Result |
||
| 995 | */ |
||
| 996 | public function reset() |
||
| 1004 | |||
| 1005 | /** |
||
| 1006 | * You can set the default result-type to 'object', 'array' or 'Arrayy'. |
||
| 1007 | * |
||
| 1008 | * INFO: used for "fetch()" and "fetchAll()" |
||
| 1009 | * |
||
| 1010 | * @param string $default_result_type |
||
| 1011 | */ |
||
| 1012 | public function setDefaultResultType($default_result_type = 'object') |
||
| 1024 | |||
| 1025 | /** |
||
| 1026 | * @param int $offset |
||
| 1027 | * @param null|int $length |
||
| 1028 | * @param bool $preserve_keys |
||
| 1029 | * |
||
| 1030 | * @return array |
||
| 1031 | */ |
||
| 1032 | public function slice($offset = 0, $length = null, $preserve_keys = false) |
||
| 1059 | } |
||
| 1060 |
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.