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