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 |
||
| 17 | class Selection implements Iterator, Countable, ArrayAccess |
||
| 18 | { |
||
| 19 | /** @var NetteDatabaseSelection */ |
||
| 20 | private $selection; |
||
| 21 | |||
| 22 | /** @var Structure */ |
||
| 23 | protected $structure; |
||
| 24 | |||
| 25 | /** |
||
| 26 | 36 | * @param NetteDatabaseSelection $selection |
|
| 27 | * @param Structure $structure |
||
| 28 | 36 | */ |
|
| 29 | 36 | public function __construct(NetteDatabaseSelection $selection, Structure $structure) |
|
| 34 | |||
| 35 | 2 | /** |
|
| 36 | * @return NetteDatabaseSelection |
||
| 37 | 2 | */ |
|
| 38 | public function getSelection(): NetteDatabaseSelection |
||
| 42 | |||
| 43 | /********************************************************************\ |
||
| 44 | | Magic methods |
||
| 45 | \********************************************************************/ |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Clone object |
||
| 49 | */ |
||
| 50 | public function __clone() |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param string $name |
||
| 57 | 2 | * @param array $arguments |
|
| 58 | * @return mixed |
||
| 59 | 2 | */ |
|
| 60 | 2 | public function __call($name, array $arguments) |
|
| 73 | |||
| 74 | /**********************************************************************\ |
||
| 75 | * Wrapper function - fetch |
||
| 76 | \**********************************************************************/ |
||
| 77 | |||
| 78 | 2 | /** |
|
| 79 | * Returns row specified by primary key |
||
| 80 | 2 | * @param mixed $key Primary key |
|
| 81 | * @return ActiveRow|null |
||
| 82 | */ |
||
| 83 | public function get($key): ?ActiveRow |
||
| 88 | |||
| 89 | 2 | /** |
|
| 90 | * Returns one record |
||
| 91 | 2 | * @return ActiveRow|null |
|
| 92 | */ |
||
| 93 | 2 | public function fetch(): ?ActiveRow |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Fetches single field |
||
| 101 | * @param string|null $column |
||
| 102 | * @return mixed |
||
| 103 | */ |
||
| 104 | 4 | public function fetchField(string $column = null) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Fetch key => value pairs |
||
| 111 | * @param mixed $key |
||
| 112 | * @param mixed $value |
||
| 113 | * @return array |
||
| 114 | 2 | */ |
|
| 115 | public function fetchPairs($key = null, $value = null): array |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Returns all records |
||
| 128 | * @return array |
||
| 129 | 4 | */ |
|
| 130 | public function fetchAll(): array |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Some examples of usage: https://github.com/nette/utils/blob/master/tests%2FUtils%2FArrays.associate().phpt |
||
| 137 | * @param mixed $path |
||
| 138 | * @return array|\stdClass |
||
| 139 | */ |
||
| 140 | 2 | public function fetchAssoc($path) |
|
| 144 | |||
| 145 | /**********************************************************************\ |
||
| 146 | * Wrapper function - sql selections |
||
| 147 | \**********************************************************************/ |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Adds select clause, more calls appends to the end |
||
| 151 | * @param string $columns for example "column, MD5(column) AS column_md5" |
||
| 152 | 6 | * @param mixed ...$params |
|
| 153 | * @return Selection |
||
| 154 | 6 | */ |
|
| 155 | 6 | public function select($columns, ...$params): Selection |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Adds condition for primary key |
||
| 163 | * @param mixed $key |
||
| 164 | * @return Selection |
||
| 165 | */ |
||
| 166 | public function wherePrimary($key): Selection |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Adds where condition, more calls appends with AND |
||
| 174 | * @param string|string[] $condition |
||
| 175 | * @param mixed ...$params |
||
| 176 | * @return Selection |
||
| 177 | */ |
||
| 178 | 2 | public function where($condition, ...$params): Selection |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Adds ON condition when joining specified table, more calls appends with AND |
||
| 186 | * @param string $tableChain table chain or table alias for which you need additional left join condition |
||
| 187 | * @param string|string[] $condition condition possibly containing ? |
||
| 188 | * @param mixed ...$params |
||
| 189 | * @return Selection |
||
| 190 | 4 | */ |
|
| 191 | public function joinWhere(string $tableChain, $condition, ...$params): Selection |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Adds where condition using the OR operator between parameters |
||
| 199 | * More calls appends with AND. |
||
| 200 | * @param array $parameters ['column1' => 1, 'column2 > ?' => 2, 'full condition'] |
||
| 201 | * @return Selection |
||
| 202 | 2 | * @throws InvalidArgumentException |
|
| 203 | */ |
||
| 204 | 2 | public function whereOr(array $parameters): Selection |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Adds order clause, more calls appends to the end |
||
| 212 | * @param string $columns for example 'column1, column2 DESC' |
||
| 213 | * @param mixed ...$params |
||
| 214 | * @return Selection |
||
| 215 | 2 | */ |
|
| 216 | public function order(string $columns, ...$params): Selection |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Sets limit clause, more calls rewrite old values |
||
| 224 | * @param int $limit |
||
| 225 | * @param int $offset |
||
| 226 | * @return Selection |
||
| 227 | 2 | */ |
|
| 228 | public function limit(int $limit, int $offset = null): Selection |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Sets offset using page number, more calls rewrite old values |
||
| 236 | * @param int $page |
||
| 237 | * @param int $itemsPerPage |
||
| 238 | * @param int|null $numOfPages |
||
| 239 | 2 | * @return Selection |
|
| 240 | */ |
||
| 241 | 2 | public function page(int $page, int $itemsPerPage, int & $numOfPages = null): Selection |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Sets group clause, more calls rewrite old value |
||
| 249 | * @param string $columns |
||
| 250 | * @param mixed ...$params |
||
| 251 | * @return Selection |
||
| 252 | */ |
||
| 253 | public function group(string $columns, ...$params): Selection |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Sets having clause, more calls rewrite old value |
||
| 261 | * @param string $having |
||
| 262 | * @param mixed ...$params |
||
| 263 | * @return Selection |
||
| 264 | */ |
||
| 265 | public function having(string $having, ...$params): Selection |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Aliases table. Example ':book:book_tag.tag', 'tg' |
||
| 273 | * @param string $tableChain |
||
| 274 | * @param string $alias |
||
| 275 | * @return Selection |
||
| 276 | */ |
||
| 277 | 12 | public function alias(string $tableChain, string $alias): Selection |
|
| 282 | |||
| 283 | /**********************************************************************\ |
||
| 284 | * Wrapper function - aggregations |
||
| 285 | \**********************************************************************/ |
||
| 286 | |||
| 287 | 2 | /** |
|
| 288 | * Executes aggregation function |
||
| 289 | 2 | * @param string $function Select call in "FUNCTION(column)" format |
|
| 290 | * @return float |
||
| 291 | */ |
||
| 292 | public function aggregation(string $function): float |
||
| 296 | |||
| 297 | 2 | /** |
|
| 298 | * Counts number of rows |
||
| 299 | 2 | * Countable interface |
|
| 300 | * @param string $column If it is not provided returns count of result rows, otherwise runs new sql counting query |
||
| 301 | * @return int |
||
| 302 | */ |
||
| 303 | public function count(string $column = null): int |
||
| 307 | 2 | ||
| 308 | /** |
||
| 309 | 2 | * Returns minimum value from a column |
|
| 310 | * @param string $column |
||
| 311 | * @return float |
||
| 312 | */ |
||
| 313 | public function min(string $column): float |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Returns maximum value from a column |
||
| 320 | * @param string $column |
||
| 321 | 2 | * @return float |
|
| 322 | */ |
||
| 323 | 2 | public function max(string $column): float |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Returns sum of values in a column |
||
| 330 | * @param string $column |
||
| 331 | * @return float |
||
| 332 | 2 | */ |
|
| 333 | public function sum(string $column): float |
||
| 337 | |||
| 338 | /**********************************************************************\ |
||
| 339 | * Wrapper function - manipulation |
||
| 340 | \**********************************************************************/ |
||
| 341 | 2 | ||
| 342 | /** |
||
| 343 | 2 | * Inserts row in a table |
|
| 344 | * @param array|Traversable|Selection $data |
||
| 345 | * @return IRow|int|bool |
||
| 346 | */ |
||
| 347 | public function insert($data) |
||
| 352 | |||
| 353 | 12 | /** |
|
| 354 | * Updates all rows in result set |
||
| 355 | 12 | * @param array|Traversable $data ($column => $value) |
|
| 356 | 12 | * @return int |
|
| 357 | */ |
||
| 358 | public function update($data): int |
||
| 362 | 12 | ||
| 363 | /** |
||
| 364 | 12 | * Deletes all rows in result set |
|
| 365 | 12 | * @return int |
|
| 366 | */ |
||
| 367 | public function delete(): int |
||
| 371 | |||
| 372 | 2 | /**********************************************************************\ |
|
| 373 | * Iterator interface |
||
| 374 | 2 | \**********************************************************************/ |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Rewind selection |
||
| 378 | */ |
||
| 379 | public function rewind(): void |
||
| 383 | 12 | ||
| 384 | /** |
||
| 385 | * Returns current selection data record |
||
| 386 | * @return ActiveRow|null |
||
| 387 | */ |
||
| 388 | public function current(): ?ActiveRow |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Returns current selection data key |
||
| 396 | * @return string|int Row ID |
||
| 397 | */ |
||
| 398 | public function key() |
||
| 402 | 2 | ||
| 403 | /** |
||
| 404 | 2 | * Move iterator |
|
| 405 | 2 | */ |
|
| 406 | public function next(): void |
||
| 410 | |||
| 411 | /** |
||
| 412 | 2 | * It is selection valid |
|
| 413 | * @return bool |
||
| 414 | 2 | */ |
|
| 415 | 2 | public function valid(): bool |
|
| 419 | |||
| 420 | /**********************************************************************\ |
||
| 421 | * ArrayAccess interface |
||
| 422 | \**********************************************************************/ |
||
| 423 | 2 | ||
| 424 | /** |
||
| 425 | 2 | * @param string $key Row ID |
|
| 426 | * @param IRow $value |
||
| 427 | */ |
||
| 428 | public function offsetSet($key, $value): void |
||
| 432 | 2 | ||
| 433 | /** |
||
| 434 | 2 | * Returns specified row |
|
| 435 | 2 | * @param string $key Row ID |
|
| 436 | * @return ActiveRow|null |
||
| 437 | */ |
||
| 438 | public function offsetGet($key): ?ActiveRow |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Tests if row exists |
||
| 446 | 26 | * @param string $key Row ID |
|
| 447 | * @return bool |
||
| 448 | 26 | */ |
|
| 449 | 26 | public function offsetExists($key): bool |
|
| 453 | |||
| 454 | /** |
||
| 455 | * Removes row from result set |
||
| 456 | * @param string $key Row ID |
||
| 457 | 4 | */ |
|
| 458 | public function offsetUnset($key): void |
||
| 462 | 2 | ||
| 463 | 4 | /**********************************************************************\ |
|
| 464 | * Build methods |
||
| 465 | \**********************************************************************/ |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Prepare one record |
||
| 469 | * @param IRow $row |
||
| 470 | * @return ActiveRow |
||
| 471 | */ |
||
| 472 | protected function prepareRecord(IRow $row): ActiveRow |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Prepare records array |
||
| 480 | * @param array $rows |
||
| 481 | * @return array |
||
| 482 | */ |
||
| 483 | protected function prepareRecords(array $rows): array |
||
| 491 | } |
||
| 492 |