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 |
||
| 14 | class Selection implements Iterator, Countable, ArrayAccess |
||
| 15 | { |
||
| 16 | /** @var NetteDatabaseSelection */ |
||
| 17 | private $selection; |
||
| 18 | |||
| 19 | /** @var Structure */ |
||
| 20 | protected $structure; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @param NetteDatabaseSelection $selection |
||
| 24 | * @param Structure|null $structure |
||
| 25 | */ |
||
| 26 | 36 | public function __construct(NetteDatabaseSelection $selection, Structure $structure = null) |
|
| 31 | |||
| 32 | /** |
||
| 33 | * @return NetteDatabaseSelection |
||
| 34 | */ |
||
| 35 | 2 | public function getSelection() |
|
| 39 | |||
| 40 | /** |
||
| 41 | * Clone object |
||
| 42 | */ |
||
| 43 | public function __clone() |
||
| 47 | |||
| 48 | /**********************************************************************\ |
||
| 49 | * Wrapper function - fetch |
||
| 50 | \**********************************************************************/ |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Returns row specified by primary key |
||
| 54 | * @param mixed $key Primary key |
||
| 55 | * @return mixed |
||
| 56 | */ |
||
| 57 | 2 | public function get($key) |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Returns one record |
||
| 65 | * @return bool|mixed |
||
| 66 | */ |
||
| 67 | 6 | public function fetch() |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Fetches single field |
||
| 75 | * @param string|null $column |
||
| 76 | * @return mixed |
||
| 77 | */ |
||
| 78 | 2 | public function fetchField($column = null) |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Fetch key => value pairs |
||
| 85 | * @param string|null $key |
||
| 86 | * @param string|null $value |
||
| 87 | * @return array |
||
| 88 | */ |
||
| 89 | 2 | public function fetchPairs($key = null, $value = null) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Returns all records |
||
| 102 | * @return array |
||
| 103 | */ |
||
| 104 | 4 | public function fetchAll() |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Some examples of usage: https://github.com/nette/utils/blob/master/tests%2FUtils%2FArrays.associate().phpt |
||
| 111 | * @param string $path |
||
| 112 | * @return array|\stdClass |
||
| 113 | */ |
||
| 114 | 2 | public function fetchAssoc($path) |
|
| 118 | |||
| 119 | /**********************************************************************\ |
||
| 120 | * Wrapper function - sql selections |
||
| 121 | \**********************************************************************/ |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Adds select clause, more calls appends to the end |
||
| 125 | * @param string $columns for example "column, MD5(column) AS column_md5" |
||
| 126 | * @param mixed ...$params |
||
| 127 | * @return Selection |
||
| 128 | */ |
||
| 129 | 4 | public function select($columns, ...$params) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Adds condition for primary key |
||
| 137 | * @param mixed $key |
||
| 138 | * @return Selection |
||
| 139 | */ |
||
| 140 | 2 | public function wherePrimary($key) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Adds where condition, more calls appends with AND |
||
| 148 | * @param string $condition |
||
| 149 | * @param mixed ...$params |
||
| 150 | * @return Selection |
||
| 151 | */ |
||
| 152 | 6 | public function where($condition, ...$params) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Adds ON condition when joining specified table, more calls appends with AND |
||
| 160 | * @param string $tableChain table chain or table alias for which you need additional left join condition |
||
| 161 | * @param string $condition condition possibly containing ? |
||
| 162 | * @param mixed ...$params |
||
| 163 | * @return Selection |
||
| 164 | */ |
||
| 165 | public function joinWhere($tableChain, $condition, ...$params) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Adds where condition using the OR operator between parameters |
||
| 173 | * More calls appends with AND. |
||
| 174 | * @param array $parameters ['column1' => 1, 'column2 > ?' => 2, 'full condition'] |
||
| 175 | * @return Selection |
||
| 176 | * @throws InvalidArgumentException |
||
| 177 | */ |
||
| 178 | 2 | public function whereOr(array $parameters) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Adds order clause, more calls appends to the end |
||
| 186 | * @param string $columns for example 'column1, column2 DESC' |
||
| 187 | * @param mixed ...$params |
||
| 188 | * @return Selection |
||
| 189 | */ |
||
| 190 | 4 | public function order($columns, ...$params) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Sets limit clause, more calls rewrite old values |
||
| 198 | * @param int $limit |
||
| 199 | * @param int $offset |
||
| 200 | * @return Selection |
||
| 201 | */ |
||
| 202 | 2 | public function limit($limit, $offset = null) |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Sets offset using page number, more calls rewrite old values |
||
| 210 | * @param int $page |
||
| 211 | * @param int $itemsPerPage |
||
| 212 | * @param int|null $numOfPages |
||
| 213 | * @return Selection |
||
| 214 | */ |
||
| 215 | 2 | public function page($page, $itemsPerPage, & $numOfPages = null) |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Sets group clause, more calls rewrite old value |
||
| 223 | * @param string $columns |
||
| 224 | * @param mixed ...$params |
||
| 225 | * @return Selection |
||
| 226 | */ |
||
| 227 | 2 | public function group($columns, ...$params) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Sets having clause, more calls rewrite old value |
||
| 235 | * @param string $having |
||
| 236 | * @param mixed ...$params |
||
| 237 | * @return Selection |
||
| 238 | */ |
||
| 239 | 2 | public function having($having, ...$params) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Aliases table. Example ':book:book_tag.tag', 'tg' |
||
| 247 | * @param string $tableChain |
||
| 248 | * @param string $alias |
||
| 249 | * @return Selection |
||
| 250 | */ |
||
| 251 | public function alias($tableChain, $alias) |
||
| 256 | |||
| 257 | /**********************************************************************\ |
||
| 258 | * Wrapper function - aggregations |
||
| 259 | \**********************************************************************/ |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Executes aggregation function |
||
| 263 | * @param string $function select call in "FUNCTION(column)" format |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | 2 | public function aggregation($function) |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Counts number of rows |
||
| 273 | * Countable interface |
||
| 274 | * @param string $column If it is not provided returns count of result rows, otherwise runs new sql counting query |
||
| 275 | * @return int |
||
| 276 | */ |
||
| 277 | 12 | public function count($column = null) |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Returns minimum value from a column |
||
| 284 | * @param string $column |
||
| 285 | * @return int |
||
| 286 | */ |
||
| 287 | 2 | public function min($column) |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Returns maximum value from a column |
||
| 294 | * @param string $column |
||
| 295 | * @return int |
||
| 296 | */ |
||
| 297 | 2 | public function max($column) |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Returns sum of values in a column |
||
| 304 | * @param string $column |
||
| 305 | * @return int |
||
| 306 | */ |
||
| 307 | 2 | public function sum($column) |
|
| 311 | |||
| 312 | /**********************************************************************\ |
||
| 313 | * Wrapper function - manipulation |
||
| 314 | \**********************************************************************/ |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Inserts row in a table |
||
| 318 | * @param array|Traversable|Selection $data |
||
| 319 | * @return IRow|int|bool |
||
| 320 | */ |
||
| 321 | 2 | public function insert($data) |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Updates all rows in result set |
||
| 329 | * @param array|Traversable $data ($column => $value) |
||
| 330 | * @return int |
||
| 331 | */ |
||
| 332 | 2 | public function update($data) |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Deletes all rows in result set |
||
| 339 | * @return int |
||
| 340 | */ |
||
| 341 | 2 | public function delete() |
|
| 345 | |||
| 346 | /**********************************************************************\ |
||
| 347 | * Iterator interface |
||
| 348 | \**********************************************************************/ |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Rewind selection |
||
| 352 | */ |
||
| 353 | 12 | public function rewind() |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Returns current selection data record |
||
| 360 | * @return bool|mixed |
||
| 361 | */ |
||
| 362 | 12 | public function current() |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Returns current selection data key |
||
| 370 | * @return string |
||
| 371 | */ |
||
| 372 | 2 | public function key() |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Move iterator |
||
| 379 | */ |
||
| 380 | 12 | public function next() |
|
| 384 | |||
| 385 | /** |
||
| 386 | * It is selection valid |
||
| 387 | * @return bool |
||
| 388 | */ |
||
| 389 | 12 | public function valid() |
|
| 393 | |||
| 394 | /**********************************************************************\ |
||
| 395 | * ArrayAccess interface |
||
| 396 | \**********************************************************************/ |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @param string $key Row ID |
||
| 400 | * @param IRow $value |
||
| 401 | */ |
||
| 402 | 2 | public function offsetSet($key, $value) |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Returns specified row |
||
| 409 | * @param string $key Row ID |
||
| 410 | * @return IRow|null |
||
| 411 | */ |
||
| 412 | 2 | public function offsetGet($key) |
|
| 417 | |||
| 418 | /** |
||
| 419 | * Tests if row exists |
||
| 420 | * @param string $key Row ID |
||
| 421 | * @return bool |
||
| 422 | */ |
||
| 423 | 2 | public function offsetExists($key) |
|
| 427 | |||
| 428 | /** |
||
| 429 | * Removes row from result set |
||
| 430 | * @param string $key Row ID |
||
| 431 | */ |
||
| 432 | 2 | public function offsetUnset($key) |
|
| 436 | |||
| 437 | /**********************************************************************\ |
||
| 438 | * Build methods |
||
| 439 | \**********************************************************************/ |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Prepare one record |
||
| 443 | * @param IRow $row |
||
| 444 | * @return mixed |
||
| 445 | */ |
||
| 446 | 26 | protected function prepareRecord(IRow $row) |
|
| 451 | |||
| 452 | /** |
||
| 453 | * Prepare records array |
||
| 454 | * @param array $rows |
||
| 455 | * @return array |
||
| 456 | */ |
||
| 457 | 4 | protected function prepareRecords(array $rows) |
|
| 465 | } |
||
| 466 |