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 | * Wrapper function - fetch |
||
42 | \**********************************************************************/ |
||
43 | |||
44 | /** |
||
45 | * Returns row specified by primary key |
||
46 | * @param mixed $key Primary key |
||
47 | * @return mixed |
||
48 | */ |
||
49 | 2 | public function get($key) |
|
54 | |||
55 | /** |
||
56 | * Returns one record |
||
57 | * @return bool|mixed |
||
58 | */ |
||
59 | 6 | public function fetch() |
|
64 | |||
65 | /** |
||
66 | * Fetches single field |
||
67 | * @param string|null $column |
||
68 | * @return mixed |
||
69 | */ |
||
70 | 2 | public function fetchField($column = null) |
|
74 | |||
75 | /** |
||
76 | * Fetch key => value pairs |
||
77 | * @param string|null $key |
||
78 | * @param string|null $value |
||
79 | * @return array |
||
80 | */ |
||
81 | 2 | public function fetchPairs($key = null, $value = null) |
|
91 | |||
92 | /** |
||
93 | * Returns all records |
||
94 | * @return array |
||
95 | */ |
||
96 | 4 | public function fetchAll() |
|
100 | |||
101 | /** |
||
102 | * Some examples of usage: https://github.com/nette/utils/blob/master/tests%2FUtils%2FArrays.associate().phpt |
||
103 | * @param string $path |
||
104 | * @return array|\stdClass |
||
105 | */ |
||
106 | 2 | public function fetchAssoc($path) |
|
110 | |||
111 | /**********************************************************************\ |
||
112 | * Wrapper function - sql selections |
||
113 | \**********************************************************************/ |
||
114 | |||
115 | /** |
||
116 | * Adds select clause, more calls appends to the end |
||
117 | * @param string $columns for example "column, MD5(column) AS column_md5" |
||
118 | * @param mixed ...$params |
||
119 | * @return Selection |
||
120 | */ |
||
121 | 4 | public function select($columns, ...$params) |
|
126 | |||
127 | /** |
||
128 | * Adds condition for primary key |
||
129 | * @param mixed $key |
||
130 | * @return Selection |
||
131 | */ |
||
132 | 2 | public function wherePrimary($key) |
|
137 | |||
138 | /** |
||
139 | * Adds where condition, more calls appends with AND |
||
140 | * @param string $condition |
||
141 | * @param mixed ...$params |
||
142 | * @return Selection |
||
143 | */ |
||
144 | 6 | public function where($condition, ...$params) |
|
149 | |||
150 | /** |
||
151 | * Adds ON condition when joining specified table, more calls appends with AND |
||
152 | * @param string $tableChain table chain or table alias for which you need additional left join condition |
||
153 | * @param string $condition condition possibly containing ? |
||
154 | * @param mixed ...$params |
||
155 | * @return Selection |
||
156 | */ |
||
157 | public function joinWhere($tableChain, $condition, ...$params) |
||
162 | |||
163 | /** |
||
164 | * Adds where condition using the OR operator between parameters |
||
165 | * More calls appends with AND. |
||
166 | * @param array $parameters ['column1' => 1, 'column2 > ?' => 2, 'full condition'] |
||
167 | * @return Selection |
||
168 | * @throws InvalidArgumentException |
||
169 | */ |
||
170 | 2 | public function whereOr(array $parameters) |
|
175 | |||
176 | /** |
||
177 | * Adds order clause, more calls appends to the end |
||
178 | * @param string $columns for example 'column1, column2 DESC' |
||
179 | * @param mixed ...$params |
||
180 | * @return Selection |
||
181 | */ |
||
182 | 4 | public function order($columns, ...$params) |
|
187 | |||
188 | /** |
||
189 | * Sets limit clause, more calls rewrite old values |
||
190 | * @param int $limit |
||
191 | * @param int $offset |
||
192 | * @return Selection |
||
193 | */ |
||
194 | 2 | public function limit($limit, $offset = null) |
|
199 | |||
200 | /** |
||
201 | * Sets offset using page number, more calls rewrite old values |
||
202 | * @param int $page |
||
203 | * @param int $itemsPerPage |
||
204 | * @param int|null $numOfPages |
||
205 | * @return Selection |
||
206 | */ |
||
207 | 2 | public function page($page, $itemsPerPage, & $numOfPages = null) |
|
212 | |||
213 | /** |
||
214 | * Sets group clause, more calls rewrite old value |
||
215 | * @param string $columns |
||
216 | * @param mixed ...$params |
||
217 | * @return Selection |
||
218 | */ |
||
219 | 2 | public function group($columns, ...$params) |
|
224 | |||
225 | /** |
||
226 | * Sets having clause, more calls rewrite old value |
||
227 | * @param string $having |
||
228 | * @param mixed ...$params |
||
229 | * @return Selection |
||
230 | */ |
||
231 | 2 | public function having($having, ...$params) |
|
236 | |||
237 | /** |
||
238 | * Aliases table. Example ':book:book_tag.tag', 'tg' |
||
239 | * @param string $tableChain |
||
240 | * @param string $alias |
||
241 | * @return Selection |
||
242 | */ |
||
243 | public function alias($tableChain, $alias) |
||
248 | |||
249 | /**********************************************************************\ |
||
250 | * Wrapper function - aggregations |
||
251 | \**********************************************************************/ |
||
252 | |||
253 | /** |
||
254 | * Executes aggregation function |
||
255 | * @param string $function select call in "FUNCTION(column)" format |
||
256 | * @return string |
||
257 | */ |
||
258 | 2 | public function aggregation($function) |
|
262 | |||
263 | /** |
||
264 | * Counts number of rows |
||
265 | * Countable interface |
||
266 | * @param string $column If it is not provided returns count of result rows, otherwise runs new sql counting query |
||
267 | * @return int |
||
268 | */ |
||
269 | 12 | public function count($column = null) |
|
273 | |||
274 | /** |
||
275 | * Returns minimum value from a column |
||
276 | * @param string $column |
||
277 | * @return int |
||
278 | */ |
||
279 | 2 | public function min($column) |
|
283 | |||
284 | /** |
||
285 | * Returns maximum value from a column |
||
286 | * @param string $column |
||
287 | * @return int |
||
288 | */ |
||
289 | 2 | public function max($column) |
|
293 | |||
294 | /** |
||
295 | * Returns sum of values in a column |
||
296 | * @param string $column |
||
297 | * @return int |
||
298 | */ |
||
299 | 2 | public function sum($column) |
|
303 | |||
304 | /**********************************************************************\ |
||
305 | * Wrapper function - manipulation |
||
306 | \**********************************************************************/ |
||
307 | |||
308 | /** |
||
309 | * Inserts row in a table |
||
310 | * @param array|Traversable|Selection $data |
||
311 | * @return IRow|int|bool |
||
312 | */ |
||
313 | 2 | public function insert($data) |
|
318 | |||
319 | /** |
||
320 | * Updates all rows in result set |
||
321 | * @param array|Traversable $data ($column => $value) |
||
322 | * @return int |
||
323 | */ |
||
324 | 2 | public function update($data) |
|
328 | |||
329 | /** |
||
330 | * Deletes all rows in result set |
||
331 | * @return int |
||
332 | */ |
||
333 | 2 | public function delete() |
|
337 | |||
338 | /**********************************************************************\ |
||
339 | * Iterator interface |
||
340 | \**********************************************************************/ |
||
341 | |||
342 | /** |
||
343 | * Rewind selection |
||
344 | */ |
||
345 | 12 | public function rewind() |
|
349 | |||
350 | /** |
||
351 | * Returns current selection data record |
||
352 | * @return bool|mixed |
||
353 | */ |
||
354 | 12 | public function current() |
|
359 | |||
360 | /** |
||
361 | * Returns current selection data key |
||
362 | * @return string |
||
363 | */ |
||
364 | 2 | public function key() |
|
365 | { |
||
366 | 2 | return $this->selection->key(); |
|
367 | } |
||
368 | |||
369 | /** |
||
370 | * Move iterator |
||
371 | */ |
||
372 | 12 | public function next() |
|
376 | |||
377 | /** |
||
378 | * It is selection valid |
||
379 | * @return bool |
||
380 | */ |
||
381 | 12 | public function valid() |
|
385 | |||
386 | /**********************************************************************\ |
||
387 | * ArrayAccess interface |
||
388 | \**********************************************************************/ |
||
389 | |||
390 | /** |
||
391 | * @param string $key Row ID |
||
392 | * @param IRow $value |
||
393 | */ |
||
394 | 2 | public function offsetSet($key, $value) |
|
398 | |||
399 | /** |
||
400 | * Returns specified row |
||
401 | * @param string $key Row ID |
||
402 | * @return IRow|null |
||
403 | */ |
||
404 | 2 | public function offsetGet($key) |
|
409 | |||
410 | /** |
||
411 | * Tests if row exists |
||
412 | * @param string $key Row ID |
||
413 | * @return bool |
||
414 | */ |
||
415 | 2 | public function offsetExists($key) |
|
419 | |||
420 | /** |
||
421 | * Removes row from result set |
||
422 | * @param string $key Row ID |
||
423 | */ |
||
424 | 2 | public function offsetUnset($key) |
|
428 | |||
429 | /**********************************************************************\ |
||
430 | * Build methods |
||
431 | \**********************************************************************/ |
||
432 | |||
433 | /** |
||
434 | * Prepare one record |
||
435 | * @param IRow $row |
||
436 | * @return mixed |
||
437 | */ |
||
438 | 26 | protected function prepareRecord(IRow $row) |
|
443 | |||
444 | /** |
||
445 | * Prepare records array |
||
446 | * @param array $rows |
||
447 | * @return array |
||
448 | */ |
||
449 | 4 | protected function prepareRecords(array $rows) |
|
457 | } |
||
458 |