Complex classes like ExprBuilder 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 ExprBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class ExprBuilder implements ExpressionInterface |
||
19 | { |
||
20 | /** |
||
21 | * @var ExpressionInterface $wrappedExpression |
||
22 | */ |
||
23 | private $wrappedExpression; |
||
24 | |||
25 | /** |
||
26 | * ExprBuilder constructor. |
||
27 | */ |
||
28 | 25 | public function __construct() |
|
32 | |||
33 | /** |
||
34 | * @return ExpressionInterface |
||
35 | */ |
||
36 | 1 | public function getWrappedExpression() |
|
40 | |||
41 | /** |
||
42 | * @inheritdoc |
||
43 | */ |
||
44 | 25 | public function compile() |
|
49 | |||
50 | /** |
||
51 | * @param mixed $alias |
||
52 | * |
||
53 | * @return ExprBuilder |
||
54 | */ |
||
55 | 3 | public function alias($alias) |
|
61 | |||
62 | /** |
||
63 | * @return ExprBuilder |
||
64 | */ |
||
65 | public function sumNullable() |
||
69 | |||
70 | /** |
||
71 | * @return ExprBuilder |
||
72 | */ |
||
73 | 1 | public function in() |
|
79 | |||
80 | /** |
||
81 | * @return ExprBuilder |
||
82 | */ |
||
83 | 1 | public function using() |
|
87 | |||
88 | /** |
||
89 | * @return ExprBuilder |
||
90 | */ |
||
91 | 1 | public function desc() |
|
95 | |||
96 | /** |
||
97 | * @return ExprBuilder |
||
98 | */ |
||
99 | 1 | public function asc() |
|
103 | |||
104 | /** |
||
105 | * @param string $connector |
||
106 | * @param mixed $expression |
||
107 | * |
||
108 | * @return ExprBuilder |
||
109 | */ |
||
110 | 2 | public function conjunction($connector, $expression) |
|
116 | |||
117 | ############################### |
||
118 | ### COMPARISION EXPRESSIONS ### |
||
119 | ############################### |
||
120 | |||
121 | /** |
||
122 | * @param mixed $from |
||
123 | * @param mixed $to |
||
124 | * |
||
125 | * @return ExprBuilder |
||
126 | */ |
||
127 | 1 | public function between($from, $to) |
|
134 | |||
135 | /** |
||
136 | * @param mixed $expression |
||
137 | * |
||
138 | * @return ExprBuilder |
||
139 | */ |
||
140 | public function eq($expression) |
||
146 | |||
147 | /** |
||
148 | * @param mixed $expression |
||
149 | * |
||
150 | * @return ExprBuilder |
||
151 | */ |
||
152 | 1 | public function gt($expression) |
|
158 | |||
159 | /** |
||
160 | * @param mixed $expression |
||
161 | * |
||
162 | * @return ExprBuilder |
||
163 | */ |
||
164 | public function gte($expression) |
||
170 | |||
171 | /** |
||
172 | * @return ExprBuilder |
||
173 | */ |
||
174 | public function isNull() |
||
178 | |||
179 | /** |
||
180 | * @param mixed $expression |
||
181 | * |
||
182 | * @return ExprBuilder |
||
183 | */ |
||
184 | public function lt($expression) |
||
190 | |||
191 | /** |
||
192 | * @param mixed $expression |
||
193 | * |
||
194 | * @return ExprBuilder |
||
195 | */ |
||
196 | public function lte($expression) |
||
202 | |||
203 | /** |
||
204 | * @param mixed $expression |
||
205 | * |
||
206 | * @return ExprBuilder |
||
207 | */ |
||
208 | public function neq($expression) |
||
214 | |||
215 | /** |
||
216 | * @param mixed $from |
||
217 | * @param mixed $to |
||
218 | * |
||
219 | * @return ExprBuilder |
||
220 | */ |
||
221 | 1 | public function notBetween($from, $to) |
|
228 | |||
229 | /** |
||
230 | * @param mixed $expression |
||
231 | * |
||
232 | * @return ExprBuilder |
||
233 | */ |
||
234 | public function nullEq($expression) |
||
240 | |||
241 | ############################## |
||
242 | ### ARITHMETIC EXPRESSIONS ### |
||
243 | ############################## |
||
244 | |||
245 | /** |
||
246 | * @param mixed $expression |
||
247 | * |
||
248 | * @return ExprBuilder |
||
249 | */ |
||
250 | 1 | public function add($expression) |
|
256 | |||
257 | /** |
||
258 | * @param mixed $expression |
||
259 | * |
||
260 | * @return ExprBuilder |
||
261 | */ |
||
262 | public function div($expression) |
||
268 | |||
269 | /** |
||
270 | * @param mixed $expression |
||
271 | * |
||
272 | * @return ExprBuilder |
||
273 | */ |
||
274 | public function divide($expression) |
||
280 | |||
281 | /** |
||
282 | * @param mixed $expression |
||
283 | * |
||
284 | * @return ExprBuilder |
||
285 | */ |
||
286 | public function modulo($expression) |
||
292 | |||
293 | /** |
||
294 | * @param mixed $expression |
||
295 | * |
||
296 | * @return ExprBuilder |
||
297 | */ |
||
298 | public function multiply($expression) |
||
304 | |||
305 | /** |
||
306 | * @param mixed $expression |
||
307 | * |
||
308 | * @return ExprBuilder |
||
309 | */ |
||
310 | public function subtract($expression) |
||
316 | |||
317 | ################# |
||
318 | ### FUNCTIONS ### |
||
319 | ################# |
||
320 | |||
321 | /** |
||
322 | * @return ExprBuilder |
||
323 | */ |
||
324 | public function asci() |
||
328 | |||
329 | /** |
||
330 | * @return ExprBuilder |
||
331 | */ |
||
332 | public function bin() |
||
336 | |||
337 | /** |
||
338 | * @return ExprBuilder |
||
339 | */ |
||
340 | public function bitLength() |
||
344 | |||
345 | /** |
||
346 | * @return ExprBuilder |
||
347 | */ |
||
348 | 1 | public function char() |
|
352 | |||
353 | /** |
||
354 | * @return ExprBuilder |
||
355 | */ |
||
356 | 1 | public function coalesce() |
|
360 | |||
361 | /** |
||
362 | * @return ExprBuilder |
||
363 | */ |
||
364 | public function concat() |
||
368 | |||
369 | /** |
||
370 | * @return ExprBuilder |
||
371 | */ |
||
372 | public function concatWs() |
||
376 | |||
377 | /** |
||
378 | * @return ExprBuilder |
||
379 | */ |
||
380 | public function elt() |
||
384 | |||
385 | /** |
||
386 | * @return ExprBuilder |
||
387 | */ |
||
388 | public function exportSet() |
||
392 | |||
393 | /** |
||
394 | * @return ExprBuilder |
||
395 | */ |
||
396 | public function field() |
||
400 | |||
401 | /** |
||
402 | * @param mixed $expression |
||
403 | * |
||
404 | * @return ExprBuilder |
||
405 | */ |
||
406 | 1 | public function ifNull($expression) |
|
412 | |||
413 | /** |
||
414 | * @return ExprBuilder |
||
415 | */ |
||
416 | 2 | public function max() |
|
420 | |||
421 | /** |
||
422 | * @return ExprBuilder |
||
423 | */ |
||
424 | 4 | public function sum() |
|
428 | |||
429 | /** |
||
430 | * @return ExprBuilder |
||
431 | */ |
||
432 | 1 | public function year() |
|
436 | } |