Complex classes like ResultSetBase 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 ResultSetBase, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | abstract class ResultSetBase implements ResultSetInterface |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @var int |
||
| 11 | */ |
||
| 12 | protected $num_rows = 0; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var int |
||
| 16 | */ |
||
| 17 | protected $cursor = 0; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var int |
||
| 21 | */ |
||
| 22 | protected $next_cursor = 0; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var int |
||
| 26 | */ |
||
| 27 | protected $affected_rows = 0; // leave to 0 so SELECT etc. will be coherent |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected $fields; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var null|array |
||
| 36 | */ |
||
| 37 | protected $stored = null; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var null|array |
||
| 41 | */ |
||
| 42 | protected $fetched = null; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var null|\Foolz\SphinxQL\Drivers\ResultSetAdapterInterface |
||
| 46 | */ |
||
| 47 | protected $adapter = null; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Checks that a row actually exists |
||
| 51 | * |
||
| 52 | * @param int $num The number of the row to check on |
||
| 53 | * @return bool True if the row exists |
||
| 54 | */ |
||
| 55 | public function hasRow($num) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Checks that a next row exists |
||
| 62 | * |
||
| 63 | * @return bool True if there's another row with a higher index |
||
| 64 | */ |
||
| 65 | public function hasNextRow() |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Returns the number of rows affected by the query |
||
| 72 | * This will be 0 for SELECT and any query not editing rows |
||
| 73 | * |
||
| 74 | * @return int |
||
| 75 | */ |
||
| 76 | public function getAffectedRows() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Returns the number of rows in the result set |
||
| 83 | * |
||
| 84 | * @return int The number of rows in the result set |
||
| 85 | */ |
||
| 86 | public function getCount() |
||
| 90 | |||
| 91 | /** |
||
| 92 | * (PHP 5 >= 5.0.0)<br/> |
||
| 93 | * Whether a offset exists |
||
| 94 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
| 95 | * @param mixed $offset <p> |
||
| 96 | * An offset to check for. |
||
| 97 | * </p> |
||
| 98 | * @return boolean true on success or false on failure. |
||
| 99 | * </p> |
||
| 100 | * <p> |
||
| 101 | * The return value will be casted to boolean if non-boolean was returned. |
||
| 102 | */ |
||
| 103 | public function offsetExists($offset) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * (PHP 5 >= 5.0.0)<br/> |
||
| 110 | * Offset to retrieve |
||
| 111 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
| 112 | * @param mixed $offset <p> |
||
| 113 | * The offset to retrieve. |
||
| 114 | * </p> |
||
| 115 | * @return mixed Can return all value types. |
||
| 116 | */ |
||
| 117 | public function offsetGet($offset) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * (PHP 5 >= 5.0.0)<br/> |
||
| 124 | * Offset to set |
||
| 125 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
| 126 | * @param mixed $offset <p> |
||
| 127 | * The offset to assign the value to. |
||
| 128 | * </p> |
||
| 129 | * @param mixed $value <p> |
||
| 130 | * The value to set. |
||
| 131 | * </p> |
||
| 132 | * @return void |
||
| 133 | * |
||
| 134 | * @codeCoverageIgnore |
||
| 135 | */ |
||
| 136 | public function offsetSet($offset, $value) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * (PHP 5 >= 5.0.0)<br/> |
||
| 143 | * Offset to unset |
||
| 144 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
| 145 | * @param mixed $offset <p> |
||
| 146 | * The offset to unset. |
||
| 147 | * </p> |
||
| 148 | * @return void |
||
| 149 | * |
||
| 150 | * @codeCoverageIgnore |
||
| 151 | */ |
||
| 152 | public function offsetUnset($offset) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * (PHP 5 >= 5.0.0)<br/> |
||
| 159 | * Return the current element |
||
| 160 | * @link http://php.net/manual/en/iterator.current.php |
||
| 161 | * @return mixed Can return any type. |
||
| 162 | */ |
||
| 163 | public function current() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * (PHP 5 >= 5.0.0)<br/> |
||
| 172 | * Move forward to next element |
||
| 173 | * @link http://php.net/manual/en/iterator.next.php |
||
| 174 | * @return void Any returned value is ignored. |
||
| 175 | */ |
||
| 176 | public function next() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * (PHP 5 >= 5.0.0)<br/> |
||
| 183 | * Return the key of the current element |
||
| 184 | * @link http://php.net/manual/en/iterator.key.php |
||
| 185 | * @return mixed scalar on success, or null on failure. |
||
| 186 | */ |
||
| 187 | public function key() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * (PHP 5 >= 5.0.0)<br/> |
||
| 194 | * Checks if current position is valid |
||
| 195 | * @link http://php.net/manual/en/iterator.valid.php |
||
| 196 | * @return boolean The return value will be casted to boolean and then evaluated. |
||
| 197 | * Returns true on success or false on failure. |
||
| 198 | */ |
||
| 199 | public function valid() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * (PHP 5 >= 5.0.0)<br/> |
||
| 210 | * Rewind the Iterator to the first element |
||
| 211 | * @link http://php.net/manual/en/iterator.rewind.php |
||
| 212 | * @return void Any returned value is ignored. |
||
| 213 | */ |
||
| 214 | public function rewind() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * (PHP 5 >= 5.1.0)<br/> |
||
| 227 | * Count elements of an object |
||
| 228 | * @link http://php.net/manual/en/countable.count.php |
||
| 229 | * @return int The custom count as an integer. |
||
| 230 | * </p> |
||
| 231 | * <p> |
||
| 232 | * The return value is cast to an integer. |
||
| 233 | */ |
||
| 234 | public function count() |
||
| 238 | |||
| 239 | protected function init() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param array $numeric_array |
||
| 251 | * @return array |
||
| 252 | */ |
||
| 253 | protected function makeAssoc($numeric_array) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @param ResultSetAdapter::FETCH_ASSOC|ResultSetAdapter::FETCH_NUM $fetch_type |
||
|
|
|||
| 265 | * @return array|bool|null |
||
| 266 | */ |
||
| 267 | protected function fetchFromStore($fetch_type) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @param ResultSetAdapter::FETCH_ASSOC|ResultSetAdapter::FETCH_NUM $fetch_type |
||
| 284 | * @return array|bool |
||
| 285 | */ |
||
| 286 | protected function fetchAllFromStore($fetch_type) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @param ResultSetAdapter::FETCH_ASSOC|ResultSetAdapter::FETCH_NUM $fetch_type |
||
| 305 | * @return array |
||
| 306 | */ |
||
| 307 | protected function fetchAll($fetch_type) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Store all the data in this object and free the driver object |
||
| 323 | * |
||
| 324 | * @return static $this |
||
| 325 | */ |
||
| 326 | public function store() |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Returns the array as in version 0.9.x |
||
| 343 | * |
||
| 344 | * @return array|int|mixed |
||
| 345 | * @deprecated Commodity method for simple transition to version 1.0.0 |
||
| 346 | */ |
||
| 347 | public function getStored() |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Moves the cursor to the selected row |
||
| 359 | * |
||
| 360 | * @param int $num The number of the row to move the cursor to |
||
| 361 | * @return static |
||
| 362 | * @throws ResultSetException If the row does not exist |
||
| 363 | */ |
||
| 364 | public function toRow($num) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Moves the cursor to the next row |
||
| 382 | * |
||
| 383 | * @return static $this |
||
| 384 | * @throws ResultSetException If the next row does not exist |
||
| 385 | */ |
||
| 386 | public function toNextRow() |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Fetches all the rows as an array of associative arrays |
||
| 394 | * |
||
| 395 | * @return array |
||
| 396 | */ |
||
| 397 | public function fetchAllAssoc() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Fetches all the rows as an array of indexed arrays |
||
| 404 | * |
||
| 405 | * @return array |
||
| 406 | */ |
||
| 407 | public function fetchAllNum() |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Fetches a row as an associative array |
||
| 414 | * |
||
| 415 | * @return array |
||
| 416 | */ |
||
| 417 | public function fetchAssoc() |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Fetches a row as an indexed array |
||
| 424 | * |
||
| 425 | * @return array|null |
||
| 426 | */ |
||
| 427 | public function fetchNum() |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @param ResultSetAdapter::FETCH_ASSOC|ResultSetAdapter::FETCH_NUM $fetch_type |
||
| 434 | * @return array|null |
||
| 435 | */ |
||
| 436 | protected function fetch($fetch_type) |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Frees the memory from the result |
||
| 453 | * Call it after you're done with a result set |
||
| 454 | * |
||
| 455 | * @return static |
||
| 456 | */ |
||
| 457 | public function freeResult() |
||
| 462 | } |
||
| 463 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.