Complex classes like Queryable 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 Queryable, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Queryable extends QueryBuilder implements IQueryable, Interfaces\IOrderedQueryable |
||
16 | { |
||
17 | /** |
||
18 | * @var Queries\ISourceInfo |
||
19 | */ |
||
20 | protected $sourceInfo; |
||
21 | |||
22 | /** |
||
23 | * @var IIteratorScheme |
||
24 | */ |
||
25 | protected $scheme; |
||
26 | |||
27 | public function __construct( |
||
28 | Providers\IQueryProvider $provider, |
||
29 | Queries\ISourceInfo $sourceInfo, |
||
30 | O\TraversalExpression $queryExpression = null, |
||
31 | IIteratorScheme $scheme = null |
||
32 | ) { |
||
33 | parent::__construct($provider); |
||
34 | $this->sourceInfo = $sourceInfo; |
||
35 | $this->expression = $queryExpression ?: O\Expression::value($this); |
||
36 | $this->scheme = $scheme ?: Iterators\SchemeProvider::getDefault(); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Returns the requested query from the query provider. |
||
41 | * |
||
42 | * @param O\Expression $expression |
||
43 | * |
||
44 | * @return mixed The result of the request query |
||
45 | */ |
||
46 | final protected function loadQuery(O\Expression $expression) |
||
50 | |||
51 | public function getExpression() |
||
55 | |||
56 | public function getSourceInfo() |
||
60 | |||
61 | public function isSource() |
||
65 | |||
66 | public function getSource() |
||
85 | |||
86 | final public function asArray() |
||
90 | |||
91 | final public function getIterator() |
||
95 | |||
96 | public function getTrueIterator() |
||
100 | |||
101 | public function asTraversable() |
||
105 | |||
106 | public function asCollection() |
||
110 | |||
111 | public function getIteratorScheme() |
||
115 | |||
116 | public function iterate(callable $function) |
||
120 | |||
121 | final public function getProvider() |
||
125 | |||
126 | // <editor-fold defaultstate="collapsed" desc="Query segments"> |
||
127 | |||
128 | public function select(callable $function) |
||
132 | |||
133 | public function selectMany(callable $function) |
||
137 | |||
138 | public function indexBy(callable $function) |
||
142 | |||
143 | public function keys() |
||
147 | |||
148 | public function reindex() |
||
152 | |||
153 | public function where(callable $predicate) |
||
157 | |||
158 | public function groupBy(callable $function) |
||
162 | |||
163 | public function join($values) |
||
167 | |||
168 | public function groupJoin($values) |
||
172 | |||
173 | public function union($values) |
||
177 | |||
178 | public function intersect($values) |
||
182 | |||
183 | public function difference($values) |
||
187 | |||
188 | public function append($values) |
||
192 | |||
193 | public function whereIn($values) |
||
197 | |||
198 | public function except($values) |
||
202 | |||
203 | public function skip($amount) |
||
207 | |||
208 | public function take($amount) |
||
212 | |||
213 | public function slice($start, $amount) |
||
217 | |||
218 | public function orderBy(callable $function, $direction) |
||
222 | |||
223 | public function orderByAscending(callable $function) |
||
227 | |||
228 | public function orderByDescending(callable $function) |
||
232 | |||
233 | public function thenBy(callable $function, $direction) |
||
237 | |||
238 | public function thenByAscending(callable $function) |
||
242 | |||
243 | public function thenByDescending(callable $function) |
||
247 | |||
248 | public function unique() |
||
252 | |||
253 | // </editor-fold> |
||
254 | |||
255 | // <editor-fold defaultstate="collapsed" desc="Query Requests"> |
||
256 | |||
257 | public function offsetExists($index) |
||
261 | |||
262 | public function offsetGet($index) |
||
266 | |||
267 | public function offsetSet($index, $value) |
||
271 | |||
272 | public function offsetUnset($index) |
||
276 | |||
277 | public function first() |
||
281 | |||
282 | public function last() |
||
286 | |||
287 | public function count() |
||
291 | |||
292 | public function isEmpty() |
||
296 | |||
297 | public function contains($value) |
||
301 | |||
302 | public function aggregate(callable $function) |
||
306 | |||
307 | public function all(callable $function = null) |
||
311 | |||
312 | public function any(callable $function = null) |
||
316 | |||
317 | public function maximum(callable $function = null) |
||
321 | |||
322 | public function minimum(callable $function = null) |
||
326 | |||
327 | public function sum(callable $function = null) |
||
331 | |||
332 | public function average(callable $function = null) |
||
336 | |||
337 | public function implode($delimiter, callable $function = null) |
||
341 | |||
342 | // </editor-fold> |
||
343 | } |
||
344 |