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 |
||
| 19 | class ExprBuilder implements ExpressionInterface |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var ExpressionInterface $wrappedExpression |
||
| 23 | */ |
||
| 24 | private $wrappedExpression; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * ExprBuilder constructor. |
||
| 28 | */ |
||
| 29 | 26 | public function __construct() |
|
| 30 | { |
||
| 31 | 26 | $this->wrappedExpression = ExprNormalizer::normalizeExpression(func_get_args()); |
|
| 32 | 26 | } |
|
| 33 | |||
| 34 | /** |
||
| 35 | * @return ExpressionInterface |
||
| 36 | */ |
||
| 37 | 1 | public function getWrappedExpression() |
|
| 38 | { |
||
| 39 | 1 | return $this->wrappedExpression; |
|
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @inheritdoc |
||
| 44 | */ |
||
| 45 | 26 | public function compile() |
|
| 46 | { |
||
| 47 | 26 | return $this->wrappedExpression->compile(); |
|
| 48 | } |
||
| 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 | * @param string $functionName |
||
| 119 | * |
||
| 120 | * @return ExprBuilder |
||
| 121 | */ |
||
| 122 | 7 | public function func($functionName) |
|
| 126 | |||
| 127 | ############################### |
||
| 128 | ### COMPARISION EXPRESSIONS ### |
||
| 129 | ############################### |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param mixed $from |
||
| 133 | * @param mixed $to |
||
| 134 | * |
||
| 135 | * @return ExprBuilder |
||
| 136 | */ |
||
| 137 | 1 | public function between($from, $to) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * @param mixed $expression |
||
| 147 | * |
||
| 148 | * @return ExprBuilder |
||
| 149 | */ |
||
| 150 | public function eq($expression) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @param mixed $expression |
||
| 159 | * |
||
| 160 | * @return ExprBuilder |
||
| 161 | */ |
||
| 162 | 1 | public function gt($expression) |
|
| 168 | |||
| 169 | /** |
||
| 170 | * @param mixed $expression |
||
| 171 | * |
||
| 172 | * @return ExprBuilder |
||
| 173 | */ |
||
| 174 | public function gte($expression) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @return ExprBuilder |
||
| 183 | */ |
||
| 184 | public function isNull() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param mixed $expression |
||
| 191 | * |
||
| 192 | * @return ExprBuilder |
||
| 193 | */ |
||
| 194 | public function lt($expression) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @param mixed $expression |
||
| 203 | * |
||
| 204 | * @return ExprBuilder |
||
| 205 | */ |
||
| 206 | public function lte($expression) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param mixed $expression |
||
| 215 | * |
||
| 216 | * @return ExprBuilder |
||
| 217 | */ |
||
| 218 | public function neq($expression) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param mixed $from |
||
| 227 | * @param mixed $to |
||
| 228 | * |
||
| 229 | * @return ExprBuilder |
||
| 230 | */ |
||
| 231 | 1 | public function notBetween($from, $to) |
|
| 238 | |||
| 239 | /** |
||
| 240 | * @param mixed $expression |
||
| 241 | * |
||
| 242 | * @return ExprBuilder |
||
| 243 | */ |
||
| 244 | public function nullEq($expression) |
||
| 250 | |||
| 251 | ############################## |
||
| 252 | ### ARITHMETIC EXPRESSIONS ### |
||
| 253 | ############################## |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param mixed $expression |
||
| 257 | * |
||
| 258 | * @return ExprBuilder |
||
| 259 | */ |
||
| 260 | 1 | public function add($expression) |
|
| 266 | |||
| 267 | /** |
||
| 268 | * @param mixed $expression |
||
| 269 | * |
||
| 270 | * @return ExprBuilder |
||
| 271 | */ |
||
| 272 | public function div($expression) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param mixed $expression |
||
| 281 | * |
||
| 282 | * @return ExprBuilder |
||
| 283 | */ |
||
| 284 | public function divide($expression) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param mixed $expression |
||
| 293 | * |
||
| 294 | * @return ExprBuilder |
||
| 295 | */ |
||
| 296 | public function modulo($expression) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @param mixed $expression |
||
| 305 | * |
||
| 306 | * @return ExprBuilder |
||
| 307 | */ |
||
| 308 | public function multiply($expression) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @param mixed $expression |
||
| 317 | * |
||
| 318 | * @return ExprBuilder |
||
| 319 | */ |
||
| 320 | public function subtract($expression) |
||
| 326 | |||
| 327 | ################# |
||
| 328 | ### FUNCTIONS ### |
||
| 329 | ################# |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @return ExprBuilder |
||
| 333 | */ |
||
| 334 | 1 | public function asci() |
|
| 338 | |||
| 339 | /** |
||
| 340 | * @return ExprBuilder |
||
| 341 | */ |
||
| 342 | public function bin() |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @return ExprBuilder |
||
| 349 | */ |
||
| 350 | public function bitLength() |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @return ExprBuilder |
||
| 357 | */ |
||
| 358 | 1 | public function char() |
|
| 362 | |||
| 363 | /** |
||
| 364 | * @return ExprBuilder |
||
| 365 | */ |
||
| 366 | 1 | public function coalesce() |
|
| 370 | |||
| 371 | /** |
||
| 372 | * @return ExprBuilder |
||
| 373 | */ |
||
| 374 | public function concat() |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @return ExprBuilder |
||
| 381 | */ |
||
| 382 | public function concatWs() |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @return ExprBuilder |
||
| 389 | */ |
||
| 390 | public function elt() |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @return ExprBuilder |
||
| 397 | */ |
||
| 398 | public function exportSet() |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @return ExprBuilder |
||
| 405 | */ |
||
| 406 | public function field() |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @param mixed $expression |
||
| 413 | * |
||
| 414 | * @return ExprBuilder |
||
| 415 | */ |
||
| 416 | 1 | public function ifNull($expression) |
|
| 422 | |||
| 423 | /** |
||
| 424 | * @return ExprBuilder |
||
| 425 | */ |
||
| 426 | 2 | public function max() |
|
| 430 | |||
| 431 | /** |
||
| 432 | * @return ExprBuilder |
||
| 433 | */ |
||
| 434 | 4 | public function sum() |
|
| 438 | |||
| 439 | /** |
||
| 440 | * @return ExprBuilder |
||
| 441 | */ |
||
| 442 | 1 | public function year() |
|
| 446 | } |