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() |
|
| 48 | |||
| 49 | /** |
||
| 50 | * @param mixed $alias |
||
| 51 | * |
||
| 52 | * @return ExprBuilder |
||
| 53 | */ |
||
| 54 | 3 | public function alias($alias) |
|
| 60 | |||
| 61 | /** |
||
| 62 | * @return ExprBuilder |
||
| 63 | */ |
||
| 64 | public function sumNullable() |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @return ExprBuilder |
||
| 71 | */ |
||
| 72 | 1 | public function in() |
|
| 78 | |||
| 79 | /** |
||
| 80 | * @return ExprBuilder |
||
| 81 | */ |
||
| 82 | 1 | public function using() |
|
| 86 | |||
| 87 | /** |
||
| 88 | * @return ExprBuilder |
||
| 89 | */ |
||
| 90 | 1 | public function desc() |
|
| 94 | |||
| 95 | /** |
||
| 96 | * @return ExprBuilder |
||
| 97 | */ |
||
| 98 | 1 | public function asc() |
|
| 102 | |||
| 103 | /** |
||
| 104 | * @param string $connector |
||
| 105 | * @param mixed $expression |
||
| 106 | * |
||
| 107 | * @return ExprBuilder |
||
| 108 | */ |
||
| 109 | 2 | public function conjunction($connector, $expression) |
|
| 115 | |||
| 116 | ############################### |
||
| 117 | ### COMPARISION EXPRESSIONS ### |
||
| 118 | ############################### |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param mixed $from |
||
| 122 | * @param mixed $to |
||
| 123 | * |
||
| 124 | * @return ExprBuilder |
||
| 125 | */ |
||
| 126 | 1 | public function between($from, $to) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * @param mixed $expression |
||
| 136 | * |
||
| 137 | * @return ExprBuilder |
||
| 138 | */ |
||
| 139 | public function eq($expression) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param mixed $expression |
||
| 148 | * |
||
| 149 | * @return ExprBuilder |
||
| 150 | */ |
||
| 151 | 1 | public function gt($expression) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * @param mixed $expression |
||
| 160 | * |
||
| 161 | * @return ExprBuilder |
||
| 162 | */ |
||
| 163 | public function gte($expression) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @return ExprBuilder |
||
| 172 | */ |
||
| 173 | public function isNull() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param mixed $expression |
||
| 180 | * |
||
| 181 | * @return ExprBuilder |
||
| 182 | */ |
||
| 183 | public function lt($expression) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param mixed $expression |
||
| 192 | * |
||
| 193 | * @return ExprBuilder |
||
| 194 | */ |
||
| 195 | public function lte($expression) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param mixed $expression |
||
| 204 | * |
||
| 205 | * @return ExprBuilder |
||
| 206 | */ |
||
| 207 | public function neq($expression) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param mixed $from |
||
| 216 | * @param mixed $to |
||
| 217 | * |
||
| 218 | * @return ExprBuilder |
||
| 219 | */ |
||
| 220 | 1 | public function notBetween($from, $to) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * @param mixed $expression |
||
| 230 | * |
||
| 231 | * @return ExprBuilder |
||
| 232 | */ |
||
| 233 | public function nullEq($expression) |
||
| 239 | |||
| 240 | ############################## |
||
| 241 | ### ARITHMETIC EXPRESSIONS ### |
||
| 242 | ############################## |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param mixed $expression |
||
| 246 | * |
||
| 247 | * @return ExprBuilder |
||
| 248 | */ |
||
| 249 | 1 | public function add($expression) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * @param mixed $expression |
||
| 258 | * |
||
| 259 | * @return ExprBuilder |
||
| 260 | */ |
||
| 261 | public function div($expression) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param mixed $expression |
||
| 270 | * |
||
| 271 | * @return ExprBuilder |
||
| 272 | */ |
||
| 273 | public function divide($expression) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @param mixed $expression |
||
| 282 | * |
||
| 283 | * @return ExprBuilder |
||
| 284 | */ |
||
| 285 | public function modulo($expression) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param mixed $expression |
||
| 294 | * |
||
| 295 | * @return ExprBuilder |
||
| 296 | */ |
||
| 297 | public function multiply($expression) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param mixed $expression |
||
| 306 | * |
||
| 307 | * @return ExprBuilder |
||
| 308 | */ |
||
| 309 | public function subtract($expression) |
||
| 315 | |||
| 316 | ################# |
||
| 317 | ### FUNCTIONS ### |
||
| 318 | ################# |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @return ExprBuilder |
||
| 322 | */ |
||
| 323 | public function asci() |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @return ExprBuilder |
||
| 330 | */ |
||
| 331 | public function bin() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @return ExprBuilder |
||
| 338 | */ |
||
| 339 | public function bitLength() |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @return ExprBuilder |
||
| 346 | */ |
||
| 347 | 1 | public function char() |
|
| 351 | |||
| 352 | /** |
||
| 353 | * @return ExprBuilder |
||
| 354 | */ |
||
| 355 | 1 | public function coalesce() |
|
| 359 | |||
| 360 | /** |
||
| 361 | * @return ExprBuilder |
||
| 362 | */ |
||
| 363 | public function concat() |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @return ExprBuilder |
||
| 370 | */ |
||
| 371 | public function concatWs() |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @return ExprBuilder |
||
| 378 | */ |
||
| 379 | public function elt() |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @return ExprBuilder |
||
| 386 | */ |
||
| 387 | public function exportSet() |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @return ExprBuilder |
||
| 394 | */ |
||
| 395 | public function field() |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @param mixed $expression |
||
| 402 | * |
||
| 403 | * @return ExprBuilder |
||
| 404 | */ |
||
| 405 | 1 | public function ifNull($expression) |
|
| 411 | |||
| 412 | /** |
||
| 413 | * @return ExprBuilder |
||
| 414 | */ |
||
| 415 | 2 | public function max() |
|
| 419 | |||
| 420 | /** |
||
| 421 | * @return ExprBuilder |
||
| 422 | */ |
||
| 423 | 4 | public function sum() |
|
| 427 | |||
| 428 | /** |
||
| 429 | * @return ExprBuilder |
||
| 430 | */ |
||
| 431 | 1 | public function year() |
|
| 435 | } |