Complex classes like Selection 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 Selection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Selection implements Iterator, Countable, ArrayAccess |
||
| 16 | { |
||
| 17 | /** @var NetteDatabaseSelection */ |
||
| 18 | private $selection; |
||
| 19 | |||
| 20 | /** @var Structure */ |
||
| 21 | protected $structure; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @param NetteDatabaseSelection $selection |
||
| 25 | * @param Structure $structure |
||
| 26 | */ |
||
| 27 | 56 | public function __construct(NetteDatabaseSelection $selection, Structure $structure) |
|
| 32 | |||
| 33 | /** |
||
| 34 | * @return NetteDatabaseSelection |
||
| 35 | */ |
||
| 36 | 2 | public function getSelection() |
|
| 40 | |||
| 41 | /** |
||
| 42 | * Clone object |
||
| 43 | */ |
||
| 44 | public function __clone() |
||
| 48 | |||
| 49 | /********************************************************************\ |
||
| 50 | | Magic methods |
||
| 51 | \********************************************************************/ |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @param string $name |
||
| 55 | * @param array $arguments |
||
| 56 | * @return mixed |
||
| 57 | */ |
||
| 58 | 2 | public function __call($name, array $arguments) |
|
| 71 | |||
| 72 | /**********************************************************************\ |
||
| 73 | * Wrapper function - fetch |
||
| 74 | \**********************************************************************/ |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Returns row specified by primary key |
||
| 78 | * @param mixed $key Primary key |
||
| 79 | * @return mixed |
||
| 80 | */ |
||
| 81 | 2 | public function get($key) |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Returns one record |
||
| 89 | * @return bool|mixed |
||
| 90 | */ |
||
| 91 | 28 | public function fetch() |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Fetches single field |
||
| 99 | * @param string|null $column |
||
| 100 | * @return mixed |
||
| 101 | */ |
||
| 102 | 2 | public function fetchField($column = null) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Fetch key => value pairs |
||
| 109 | * @param string|null $key |
||
| 110 | * @param string|null $value |
||
| 111 | * @return array |
||
| 112 | */ |
||
| 113 | 2 | public function fetchPairs($key = null, $value = null) |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Returns all records |
||
| 126 | * @return array |
||
| 127 | */ |
||
| 128 | 4 | public function fetchAll() |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Some examples of usage: https://github.com/nette/utils/blob/master/tests%2FUtils%2FArrays.associate().phpt |
||
| 135 | * @param string $path |
||
| 136 | * @return array|\stdClass |
||
| 137 | */ |
||
| 138 | 2 | public function fetchAssoc($path) |
|
| 142 | |||
| 143 | /**********************************************************************\ |
||
| 144 | * Wrapper function - sql selections |
||
| 145 | \**********************************************************************/ |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Adds select clause, more calls appends to the end |
||
| 149 | * @param string $columns for example "column, MD5(column) AS column_md5" |
||
| 150 | * @param mixed ...$params |
||
| 151 | * @return Selection |
||
| 152 | */ |
||
| 153 | 4 | public function select($columns, ...$params) |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Adds condition for primary key |
||
| 161 | * @param mixed $key |
||
| 162 | * @return Selection |
||
| 163 | */ |
||
| 164 | 8 | public function wherePrimary($key) |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Adds where condition, more calls appends with AND |
||
| 172 | * @param string|string[] $condition |
||
| 173 | * @param mixed ...$params |
||
| 174 | * @return Selection |
||
| 175 | */ |
||
| 176 | 14 | public function where($condition, ...$params) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Adds ON condition when joining specified table, more calls appends with AND |
||
| 184 | * @param string $tableChain table chain or table alias for which you need additional left join condition |
||
| 185 | * @param string $condition condition possibly containing ? |
||
| 186 | * @param mixed ...$params |
||
| 187 | * @return Selection |
||
| 188 | */ |
||
| 189 | public function joinWhere($tableChain, $condition, ...$params) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Adds where condition using the OR operator between parameters |
||
| 197 | * More calls appends with AND. |
||
| 198 | * @param array $parameters ['column1' => 1, 'column2 > ?' => 2, 'full condition'] |
||
| 199 | * @return Selection |
||
| 200 | * @throws InvalidArgumentException |
||
| 201 | */ |
||
| 202 | 2 | public function whereOr(array $parameters) |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Adds order clause, more calls appends to the end |
||
| 210 | * @param string $columns for example 'column1, column2 DESC' |
||
| 211 | * @param mixed ...$params |
||
| 212 | * @return Selection |
||
| 213 | */ |
||
| 214 | 4 | public function order($columns, ...$params) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Sets limit clause, more calls rewrite old values |
||
| 222 | * @param int $limit |
||
| 223 | * @param int $offset |
||
| 224 | * @return Selection |
||
| 225 | */ |
||
| 226 | 2 | public function limit($limit, $offset = null) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Sets offset using page number, more calls rewrite old values |
||
| 234 | * @param int $page |
||
| 235 | * @param int $itemsPerPage |
||
| 236 | * @param int|null $numOfPages |
||
| 237 | * @return Selection |
||
| 238 | */ |
||
| 239 | 2 | public function page($page, $itemsPerPage, & $numOfPages = null) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Sets group clause, more calls rewrite old value |
||
| 247 | * @param string $columns |
||
| 248 | * @param mixed ...$params |
||
| 249 | * @return Selection |
||
| 250 | */ |
||
| 251 | 2 | public function group($columns, ...$params) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Sets having clause, more calls rewrite old value |
||
| 259 | * @param string $having |
||
| 260 | * @param mixed ...$params |
||
| 261 | * @return Selection |
||
| 262 | */ |
||
| 263 | 2 | public function having($having, ...$params) |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Aliases table. Example ':book:book_tag.tag', 'tg' |
||
| 271 | * @param string $tableChain |
||
| 272 | * @param string $alias |
||
| 273 | * @return Selection |
||
| 274 | */ |
||
| 275 | public function alias($tableChain, $alias) |
||
| 280 | |||
| 281 | /**********************************************************************\ |
||
| 282 | * Wrapper function - aggregations |
||
| 283 | \**********************************************************************/ |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Executes aggregation function |
||
| 287 | * @param string $function select call in "FUNCTION(column)" format |
||
| 288 | * @return string |
||
| 289 | */ |
||
| 290 | 2 | public function aggregation($function) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Counts number of rows |
||
| 297 | * Countable interface |
||
| 298 | * @param string $column If it is not provided returns count of result rows, otherwise runs new sql counting query |
||
| 299 | * @return int |
||
| 300 | */ |
||
| 301 | 14 | public function count($column = null) |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Returns minimum value from a column |
||
| 308 | * @param string $column |
||
| 309 | * @return int |
||
| 310 | */ |
||
| 311 | 2 | public function min($column) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Returns maximum value from a column |
||
| 318 | * @param string $column |
||
| 319 | * @return int |
||
| 320 | */ |
||
| 321 | 2 | public function max($column) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Returns sum of values in a column |
||
| 328 | * @param string $column |
||
| 329 | * @return int |
||
| 330 | */ |
||
| 331 | 2 | public function sum($column) |
|
| 335 | |||
| 336 | /**********************************************************************\ |
||
| 337 | * Wrapper function - manipulation |
||
| 338 | \**********************************************************************/ |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Inserts row in a table |
||
| 342 | * @param array|Traversable|Selection $data |
||
| 343 | * @return IRow|int|bool |
||
| 344 | */ |
||
| 345 | 2 | public function insert($data) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Updates all rows in result set |
||
| 353 | * @param array|Traversable $data ($column => $value) |
||
| 354 | * @return int |
||
| 355 | */ |
||
| 356 | 2 | public function update($data) |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Deletes all rows in result set |
||
| 363 | * @return int |
||
| 364 | */ |
||
| 365 | 2 | public function delete() |
|
| 369 | |||
| 370 | /**********************************************************************\ |
||
| 371 | * Iterator interface |
||
| 372 | \**********************************************************************/ |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Rewind selection |
||
| 376 | */ |
||
| 377 | 12 | public function rewind() |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Returns current selection data record |
||
| 384 | * @return bool|mixed |
||
| 385 | */ |
||
| 386 | 12 | public function current() |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Returns current selection data key |
||
| 394 | * @return string |
||
| 395 | */ |
||
| 396 | 2 | public function key() |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Move iterator |
||
| 403 | */ |
||
| 404 | 12 | public function next() |
|
| 408 | |||
| 409 | /** |
||
| 410 | * It is selection valid |
||
| 411 | * @return bool |
||
| 412 | */ |
||
| 413 | 12 | public function valid() |
|
| 417 | |||
| 418 | /**********************************************************************\ |
||
| 419 | * ArrayAccess interface |
||
| 420 | \**********************************************************************/ |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @param string $key Row ID |
||
| 424 | * @param IRow $value |
||
| 425 | */ |
||
| 426 | 2 | public function offsetSet($key, $value) |
|
| 430 | |||
| 431 | /** |
||
| 432 | * Returns specified row |
||
| 433 | * @param string $key Row ID |
||
| 434 | * @return IRow|null |
||
| 435 | */ |
||
| 436 | 2 | public function offsetGet($key) |
|
| 441 | |||
| 442 | /** |
||
| 443 | * Tests if row exists |
||
| 444 | * @param string $key Row ID |
||
| 445 | * @return bool |
||
| 446 | */ |
||
| 447 | 2 | public function offsetExists($key) |
|
| 451 | |||
| 452 | /** |
||
| 453 | * Removes row from result set |
||
| 454 | * @param string $key Row ID |
||
| 455 | */ |
||
| 456 | 2 | public function offsetUnset($key) |
|
| 460 | |||
| 461 | /**********************************************************************\ |
||
| 462 | * Build methods |
||
| 463 | \**********************************************************************/ |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Prepare one record |
||
| 467 | * @param IRow $row |
||
| 468 | * @return mixed |
||
| 469 | */ |
||
| 470 | 44 | protected function prepareRecord(IRow $row) |
|
| 475 | |||
| 476 | /** |
||
| 477 | * Prepare records array |
||
| 478 | * @param array $rows |
||
| 479 | * @return array |
||
| 480 | */ |
||
| 481 | 4 | protected function prepareRecords(array $rows) |
|
| 489 | } |
||
| 490 |