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 |
||
| 16 | final class Result implements \Countable, \SeekableIterator, \ArrayAccess |
||
| 17 | { |
||
| 18 | |||
| 19 | const RESULT_TYPE_ARRAY = 'array'; |
||
| 20 | const RESULT_TYPE_ARRAYY = 'Arrayy'; |
||
| 21 | const RESULT_TYPE_OBJECT = 'object'; |
||
| 22 | const RESULT_TYPE_YIELD = 'yield'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var int |
||
| 26 | */ |
||
| 27 | public $num_rows; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | public $sql; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var \mysqli_result |
||
| 36 | */ |
||
| 37 | private $_result; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var int |
||
| 41 | */ |
||
| 42 | private $current_row; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var \Closure|null |
||
| 46 | */ |
||
| 47 | private $_mapper; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | private $_default_result_type = self::RESULT_TYPE_OBJECT; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Result constructor. |
||
| 56 | * |
||
| 57 | * @param string $sql |
||
| 58 | * @param \mysqli_result $result |
||
| 59 | * @param \Closure $mapper Optional callback mapper for the "fetchCallable()" method |
||
| 60 | */ |
||
| 61 | 64 | public function __construct(string $sql = '', \mysqli_result $result, \Closure $mapper = null) |
|
| 72 | |||
| 73 | /** |
||
| 74 | * __destruct |
||
| 75 | */ |
||
| 76 | 63 | public function __destruct() |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Runs a user-provided callback with the MySQLi_Result object given as |
||
| 83 | * argument and returns the result, or returns the MySQLi_Result object if |
||
| 84 | * called without an argument. |
||
| 85 | * |
||
| 86 | * @param callable $callback User-provided callback (optional) |
||
| 87 | * |
||
| 88 | * @return mixed|\mysqli_result |
||
| 89 | */ |
||
| 90 | 2 | public function __invoke(callable $callback = null) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Get the current "num_rows" as string. |
||
| 101 | * |
||
| 102 | * @return string |
||
| 103 | */ |
||
| 104 | public function __toString() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Cast data into int, float or string. |
||
| 111 | * |
||
| 112 | * <p> |
||
| 113 | * <br /> |
||
| 114 | * INFO: install / use "mysqlnd"-driver for better performance |
||
| 115 | * </p> |
||
| 116 | * |
||
| 117 | * @param array|object $data |
||
| 118 | * |
||
| 119 | * @return array|object|false <p><strong>false</strong> on error</p> |
||
| 120 | */ |
||
| 121 | 46 | private function cast(&$data) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Countable interface implementation. |
||
| 176 | * |
||
| 177 | * @return int The number of rows in the result |
||
| 178 | */ |
||
| 179 | 2 | public function count(): int |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Iterator interface implementation. |
||
| 186 | * |
||
| 187 | * @return mixed The current element |
||
| 188 | */ |
||
| 189 | 6 | public function current() |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Iterator interface implementation. |
||
| 196 | * |
||
| 197 | * @return int The current element key (row index; zero-based) |
||
| 198 | */ |
||
| 199 | 1 | public function key(): int |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Iterator interface implementation. |
||
| 206 | * |
||
| 207 | * @return void |
||
| 208 | */ |
||
| 209 | 6 | public function next() |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Iterator interface implementation. |
||
| 216 | * |
||
| 217 | * @param int $row Row position to rewind to; defaults to 0 |
||
| 218 | * |
||
| 219 | * @return void |
||
| 220 | */ |
||
| 221 | 10 | public function rewind($row = 0) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Moves the internal pointer to the specified row position. |
||
| 230 | * |
||
| 231 | * @param int $row <p>Row position; zero-based and set to 0 by default</p> |
||
| 232 | * |
||
| 233 | * @return bool <p>true on success, false otherwise</p> |
||
| 234 | */ |
||
| 235 | 18 | public function seek($row = 0): bool |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Iterator interface implementation. |
||
| 246 | * |
||
| 247 | * @return bool <p>true if the current index is valid, false otherwise</p> |
||
| 248 | */ |
||
| 249 | 6 | public function valid(): bool |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Fetch. |
||
| 256 | * |
||
| 257 | * <p> |
||
| 258 | * <br /> |
||
| 259 | * INFO: this will return an object by default, not an array<br /> |
||
| 260 | * and you can change the behaviour via "Result->setDefaultResultType()" |
||
| 261 | * </p> |
||
| 262 | * |
||
| 263 | * @param bool $reset optional <p>Reset the \mysqli_result counter.</p> |
||
| 264 | * |
||
| 265 | * @return array|object|false <p><strong>false</strong> on error</p> |
||
| 266 | */ |
||
| 267 | 2 | public function fetch(bool $reset = false) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Fetch all results. |
||
| 286 | * |
||
| 287 | * <p> |
||
| 288 | * <br /> |
||
| 289 | * INFO: this will return an object by default, not an array<br /> |
||
| 290 | * and you can change the behaviour via "Result->setDefaultResultType()" |
||
| 291 | * </p> |
||
| 292 | * |
||
| 293 | * @return array |
||
| 294 | */ |
||
| 295 | 2 | public function fetchAll(): array |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Fetch all results as array. |
||
| 314 | * |
||
| 315 | * @return array |
||
| 316 | */ |
||
| 317 | 14 | public function fetchAllArray(): array |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Fetch all results as "Arrayy"-object. |
||
| 336 | * |
||
| 337 | * @return Arrayy |
||
| 338 | */ |
||
| 339 | 4 | public function fetchAllArrayy(): Arrayy |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Fetch a single column as an 1-dimension array. |
||
| 358 | * |
||
| 359 | * @param string $column |
||
| 360 | * @param bool $skipNullValues <p>Skip "NULL"-values. | default: false</p> |
||
| 361 | * |
||
| 362 | * @return array <p>Return an empty array if the "$column" wasn't found</p> |
||
| 363 | */ |
||
| 364 | 3 | public function fetchAllColumn(string $column, bool $skipNullValues = false): array |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Fetch all results as array with objects. |
||
| 371 | * |
||
| 372 | * @param object|string $class <p> |
||
| 373 | * <strong>string</strong>: create a new object (with optional constructor |
||
| 374 | * parameter)<br> |
||
| 375 | * <strong>object</strong>: use a object and fill the the data into |
||
| 376 | * </p> |
||
| 377 | * @param null|array $params optional |
||
| 378 | * <p> |
||
| 379 | * An array of parameters to pass to the constructor, used if $class is a |
||
| 380 | * string. |
||
| 381 | * </p> |
||
| 382 | * |
||
| 383 | * @return array |
||
| 384 | */ |
||
| 385 | 11 | public function fetchAllObject($class = '', array $params = null): array |
|
| 435 | |||
| 436 | /** |
||
| 437 | * Fetch all results as "\Generator" via yield. |
||
| 438 | * |
||
| 439 | * @param object|string $class <p> |
||
| 440 | * <strong>string</strong>: create a new object (with optional constructor |
||
| 441 | * parameter)<br> |
||
| 442 | * <strong>object</strong>: use a object and fill the the data into |
||
| 443 | * </p> |
||
| 444 | * @param null|array $params optional |
||
| 445 | * <p> |
||
| 446 | * An array of parameters to pass to the constructor, used if $class is a |
||
| 447 | * string. |
||
| 448 | * </p> |
||
| 449 | * |
||
| 450 | * @return \Generator |
||
| 451 | */ |
||
| 452 | 1 | public function fetchAllYield($class = '', array $params = null): \Generator |
|
| 499 | |||
| 500 | /** |
||
| 501 | * Fetch as array. |
||
| 502 | * |
||
| 503 | * @param bool $reset |
||
| 504 | * |
||
| 505 | * @return array|false <p><strong>false</strong> on error</p> |
||
| 506 | */ |
||
| 507 | 16 | View Code Duplication | public function fetchArray(bool $reset = false) |
| 524 | |||
| 525 | /** |
||
| 526 | * Fetch data as a key/value pair array. |
||
| 527 | * |
||
| 528 | * <p> |
||
| 529 | * <br /> |
||
| 530 | * INFO: both "key" and "value" must exists in the fetched data |
||
| 531 | * the key will be the new key of the result-array |
||
| 532 | * <br /><br /> |
||
| 533 | * </p> |
||
| 534 | * |
||
| 535 | * e.g.: |
||
| 536 | * <code> |
||
| 537 | * fetchArrayPair('some_id', 'some_value'); |
||
| 538 | * // array(127 => 'some value', 128 => 'some other value') |
||
| 539 | * </code> |
||
| 540 | * |
||
| 541 | * @param string $key |
||
| 542 | * @param string $value |
||
| 543 | * |
||
| 544 | * @return array |
||
| 545 | */ |
||
| 546 | 1 | public function fetchArrayPair(string $key, string $value): array |
|
| 565 | |||
| 566 | /** |
||
| 567 | * Fetch as "Arrayy"-object. |
||
| 568 | * |
||
| 569 | * @param bool $reset optional <p>Reset the \mysqli_result counter.</p> |
||
| 570 | * |
||
| 571 | * @return Arrayy|false <p><strong>false</strong> on error</p> |
||
| 572 | */ |
||
| 573 | 2 | View Code Duplication | public function fetchArrayy(bool $reset = false) |
| 590 | |||
| 591 | /** |
||
| 592 | * Fetches a row or a single column within a row. Returns null if there are |
||
| 593 | * no more rows in the result. |
||
| 594 | * |
||
| 595 | * @param int $row The row number (optional) |
||
| 596 | * @param string $column The column name (optional) |
||
| 597 | * |
||
| 598 | * @return mixed An associative array or a scalar value |
||
| 599 | */ |
||
| 600 | 15 | public function fetchCallable(int $row = null, string $column = null) |
|
| 618 | |||
| 619 | /** |
||
| 620 | * Fetch a single column as string (or as 1-dimension array). |
||
| 621 | * |
||
| 622 | * @param string $column |
||
| 623 | * @param bool $skipNullValues <p>Skip "NULL"-values. | default: true</p> |
||
| 624 | * @param bool $asArray <p>Get all values and not only the last one. | default: false</p> |
||
| 625 | * |
||
| 626 | * @return string|array <p>Return a empty string or an empty array if the "$column" wasn't found, depend on |
||
| 627 | * "$asArray"</p> |
||
| 628 | */ |
||
| 629 | 5 | public function fetchColumn(string $column = '', bool $skipNullValues = true, bool $asArray = false) |
|
| 677 | |||
| 678 | /** |
||
| 679 | * Return rows of field information in a result set. This function is a |
||
| 680 | * basically a wrapper on the native mysqli_fetch_fields function. |
||
| 681 | * |
||
| 682 | * @param bool $as_array Return each field info as array; defaults to false |
||
| 683 | * |
||
| 684 | * @return array Array of field information each as an associative array |
||
| 685 | */ |
||
| 686 | 1 | public function fetchFields(bool $as_array = false): array |
|
| 699 | |||
| 700 | /** |
||
| 701 | * Returns all rows at once as a grouped array of scalar values or arrays. |
||
| 702 | * |
||
| 703 | * @param string $group The column name to use for grouping |
||
| 704 | * @param string $column The column name to use as values (optional) |
||
| 705 | * |
||
| 706 | * @return array A grouped array of scalar values or arrays |
||
| 707 | */ |
||
| 708 | 1 | View Code Duplication | public function fetchGroups(string $group, string $column = null): array |
| 736 | |||
| 737 | /** |
||
| 738 | * Fetch as object. |
||
| 739 | * |
||
| 740 | * @param object|string $class <p> |
||
| 741 | * <strong>string</strong>: create a new object (with optional constructor |
||
| 742 | * parameter)<br> |
||
| 743 | * <strong>object</strong>: use a object and fill the the data into |
||
| 744 | * </p> |
||
| 745 | * @param null|array $params optional |
||
| 746 | * <p> |
||
| 747 | * An array of parameters to pass to the constructor, used if $class is a |
||
| 748 | * string. |
||
| 749 | * </p> |
||
| 750 | * @param bool $reset optional <p>Reset the \mysqli_result counter.</p> |
||
| 751 | * |
||
| 752 | * @return object|false <p><strong>false</strong> on error</p> |
||
| 753 | */ |
||
| 754 | 18 | public function fetchObject($class = '', array $params = null, bool $reset = false) |
|
| 792 | |||
| 793 | /** |
||
| 794 | * Returns all rows at once as key-value pairs. |
||
| 795 | * |
||
| 796 | * @param string $key The column name to use as keys |
||
| 797 | * @param string $column The column name to use as values (optional) |
||
| 798 | * |
||
| 799 | * @return array An array of key-value pairs |
||
| 800 | */ |
||
| 801 | 1 | View Code Duplication | public function fetchPairs(string $key, string $column = null): array |
| 829 | |||
| 830 | /** |
||
| 831 | * Returns all rows at once, transposed as an array of arrays. Instead of |
||
| 832 | * returning rows of columns, this method returns columns of rows. |
||
| 833 | * |
||
| 834 | * @param string $column The column name to use as keys (optional) |
||
| 835 | * |
||
| 836 | * @return mixed A transposed array of arrays |
||
| 837 | */ |
||
| 838 | 1 | public function fetchTranspose(string $column = null) |
|
| 863 | |||
| 864 | /** |
||
| 865 | * Fetch as "\Generator" via yield. |
||
| 866 | * |
||
| 867 | * @param object|string $class <p> |
||
| 868 | * <strong>string</strong>: create a new object (with optional constructor |
||
| 869 | * parameter)<br> |
||
| 870 | * <strong>object</strong>: use a object and fill the the data into |
||
| 871 | * </p> |
||
| 872 | * @param null|array $params optional |
||
| 873 | * <p> |
||
| 874 | * An array of parameters to pass to the constructor, used if $class is a |
||
| 875 | * string. |
||
| 876 | * </p> |
||
| 877 | * @param bool $reset optional <p>Reset the \mysqli_result counter.</p> |
||
| 878 | * |
||
| 879 | * @return \Generator |
||
| 880 | */ |
||
| 881 | 1 | public function fetchYield($class = '', array $params = null, bool $reset = false): \Generator |
|
| 925 | |||
| 926 | /** |
||
| 927 | * Returns the first row element from the result. |
||
| 928 | * |
||
| 929 | * @param string $column The column name to use as value (optional) |
||
| 930 | * |
||
| 931 | * @return mixed A row array or a single scalar value |
||
| 932 | */ |
||
| 933 | 3 | public function first(string $column = null) |
|
| 941 | |||
| 942 | /** |
||
| 943 | * free the memory |
||
| 944 | */ |
||
| 945 | 63 | public function free() |
|
| 957 | |||
| 958 | /** |
||
| 959 | * alias for "Result->fetch()" |
||
| 960 | * |
||
| 961 | * @see Result::fetch() |
||
| 962 | * |
||
| 963 | * @return array|object|false <p><strong>false</strong> on error</p> |
||
| 964 | */ |
||
| 965 | 1 | public function get() |
|
| 969 | |||
| 970 | /** |
||
| 971 | * alias for "Result->fetchAll()" |
||
| 972 | * |
||
| 973 | * @see Result::fetchAll() |
||
| 974 | * |
||
| 975 | * @return array |
||
| 976 | */ |
||
| 977 | 1 | public function getAll(): array |
|
| 981 | |||
| 982 | /** |
||
| 983 | * alias for "Result->fetchAllColumn()" |
||
| 984 | * |
||
| 985 | * @see Result::fetchAllColumn() |
||
| 986 | * |
||
| 987 | * @param string $column |
||
| 988 | * @param bool $skipNullValues |
||
| 989 | * |
||
| 990 | * @return array |
||
| 991 | */ |
||
| 992 | public function getAllColumn(string $column, bool $skipNullValues = false): array |
||
| 996 | |||
| 997 | /** |
||
| 998 | * alias for "Result->fetchAllArray()" |
||
| 999 | * |
||
| 1000 | * @see Result::fetchAllArray() |
||
| 1001 | * |
||
| 1002 | * @return array |
||
| 1003 | */ |
||
| 1004 | 1 | public function getArray(): array |
|
| 1008 | |||
| 1009 | /** |
||
| 1010 | * alias for "Result->fetchAllArrayy()" |
||
| 1011 | * |
||
| 1012 | * @see Result::fetchAllArrayy() |
||
| 1013 | * |
||
| 1014 | * @return Arrayy |
||
| 1015 | */ |
||
| 1016 | public function getArrayy(): Arrayy |
||
| 1020 | |||
| 1021 | /** |
||
| 1022 | * alias for "Result->fetchAllYield()" |
||
| 1023 | * |
||
| 1024 | * @see Result::fetchAllYield() |
||
| 1025 | * |
||
| 1026 | * @param bool $asArray |
||
| 1027 | * |
||
| 1028 | * @return \Generator |
||
| 1029 | */ |
||
| 1030 | public function getYield($asArray = false): \Generator |
||
| 1034 | |||
| 1035 | /** |
||
| 1036 | * alias for "Result->fetchColumn()" |
||
| 1037 | * |
||
| 1038 | * @see Result::fetchColumn() |
||
| 1039 | * |
||
| 1040 | * @param string $column |
||
| 1041 | * @param bool $asArray |
||
| 1042 | * @param bool $skipNullValues |
||
| 1043 | * |
||
| 1044 | * @return string|array <p>Return a empty string or an empty array if the "$column" wasn't found, depend on |
||
| 1045 | * "$asArray"</p> |
||
| 1046 | */ |
||
| 1047 | 1 | public function getColumn(string $column, bool $skipNullValues = true, bool $asArray = false) |
|
| 1051 | |||
| 1052 | /** |
||
| 1053 | * @return string |
||
| 1054 | */ |
||
| 1055 | 1 | public function getDefaultResultType(): string |
|
| 1059 | |||
| 1060 | /** |
||
| 1061 | * alias for "Result->fetchAllObject()" |
||
| 1062 | * |
||
| 1063 | * @see Result::fetchAllObject() |
||
| 1064 | * |
||
| 1065 | * @return array of mysql-objects |
||
| 1066 | */ |
||
| 1067 | 1 | public function getObject(): array |
|
| 1071 | |||
| 1072 | /** |
||
| 1073 | * Check if the result is empty. |
||
| 1074 | * |
||
| 1075 | * @return bool |
||
| 1076 | */ |
||
| 1077 | 34 | public function is_empty(): bool |
|
| 1081 | |||
| 1082 | /** |
||
| 1083 | * Fetch all results as "json"-string. |
||
| 1084 | * |
||
| 1085 | * @return string |
||
| 1086 | */ |
||
| 1087 | 1 | public function json(): string |
|
| 1093 | |||
| 1094 | /** |
||
| 1095 | * Returns the last row element from the result. |
||
| 1096 | * |
||
| 1097 | * @param string $column The column name to use as value (optional) |
||
| 1098 | * |
||
| 1099 | * @return mixed A row array or a single scalar value |
||
| 1100 | */ |
||
| 1101 | 3 | public function last(string $column = null) |
|
| 1109 | |||
| 1110 | /** |
||
| 1111 | * Set the mapper... |
||
| 1112 | * |
||
| 1113 | * @param \Closure $callable |
||
| 1114 | * |
||
| 1115 | * @return $this |
||
| 1116 | */ |
||
| 1117 | 1 | public function map(\Closure $callable) |
|
| 1123 | |||
| 1124 | /** |
||
| 1125 | * Alias of count(). Deprecated. |
||
| 1126 | * |
||
| 1127 | * @return int The number of rows in the result |
||
| 1128 | */ |
||
| 1129 | 1 | public function num_rows(): int |
|
| 1133 | |||
| 1134 | /** |
||
| 1135 | * ArrayAccess interface implementation. |
||
| 1136 | * |
||
| 1137 | * @param int $offset <p>Offset number</p> |
||
| 1138 | * |
||
| 1139 | * @return bool <p>true if offset exists, false otherwise</p> |
||
| 1140 | */ |
||
| 1141 | 1 | public function offsetExists($offset): bool |
|
| 1145 | |||
| 1146 | /** |
||
| 1147 | * ArrayAccess interface implementation. |
||
| 1148 | * |
||
| 1149 | * @param int $offset Offset number |
||
| 1150 | * |
||
| 1151 | * @return mixed |
||
| 1152 | */ |
||
| 1153 | 1 | public function offsetGet($offset) |
|
| 1161 | |||
| 1162 | /** |
||
| 1163 | * ArrayAccess interface implementation. Not implemented by design. |
||
| 1164 | * |
||
| 1165 | * @param mixed $offset |
||
| 1166 | * @param mixed $value |
||
| 1167 | */ |
||
| 1168 | public function offsetSet($offset, $value) |
||
| 1173 | |||
| 1174 | /** |
||
| 1175 | * ArrayAccess interface implementation. Not implemented by design. |
||
| 1176 | * |
||
| 1177 | * @param mixed $offset |
||
| 1178 | */ |
||
| 1179 | public function offsetUnset($offset) |
||
| 1184 | |||
| 1185 | /** |
||
| 1186 | * Reset the offset (data_seek) for the results. |
||
| 1187 | * |
||
| 1188 | * @return Result |
||
| 1189 | */ |
||
| 1190 | 32 | public function reset(): self |
|
| 1198 | |||
| 1199 | /** |
||
| 1200 | * You can set the default result-type to Result::RESULT_TYPE_*. |
||
| 1201 | * |
||
| 1202 | * INFO: used for "fetch()" and "fetchAll()" |
||
| 1203 | * |
||
| 1204 | * @param string $default_result_type |
||
| 1205 | */ |
||
| 1206 | 2 | public function setDefaultResultType(string $default_result_type = self::RESULT_TYPE_OBJECT) |
|
| 1220 | |||
| 1221 | /** |
||
| 1222 | * @param int $offset |
||
| 1223 | * @param null|int $length |
||
| 1224 | * @param bool $preserve_keys |
||
| 1225 | * |
||
| 1226 | * @return array |
||
| 1227 | */ |
||
| 1228 | 1 | public function slice(int $offset = 0, int $length = null, bool $preserve_keys = false): array |
|
| 1254 | } |
||
| 1255 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: