Complex classes like Where 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 Where, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Where |
||
| 22 | { |
||
| 23 | const OPERATOR_GREATER_THAN_OR_EQUAL = '>='; |
||
| 24 | const OPERATOR_GREATER_THAN = '>'; |
||
| 25 | const OPERATOR_LESS_THAN_OR_EQUAL = '<='; |
||
| 26 | const OPERATOR_LESS_THAN = '<'; |
||
| 27 | const OPERATOR_LIKE = 'LIKE'; |
||
| 28 | const OPERATOR_NOT_LIKE = 'NOT LIKE'; |
||
| 29 | const OPERATOR_EQUAL = '='; |
||
| 30 | const OPERATOR_NOT_EQUAL = '<>'; |
||
| 31 | const CONJUNCTION_AND = 'AND'; |
||
| 32 | const CONJUNCTION_OR = 'OR'; |
||
| 33 | const CONJUNCTION_EXISTS = 'EXISTS'; |
||
| 34 | const CONJUNCTION_NOT_EXISTS = 'NOT EXISTS'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array |
||
| 38 | */ |
||
| 39 | private $comparisons = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | private $betweens = []; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | private $isNull = []; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | private $isNotNull = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | private $booleans = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | private $match = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | private $ins = []; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var array |
||
| 73 | */ |
||
| 74 | private $notIns = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | private $subWheres = []; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | private $conjunction = self::CONJUNCTION_AND; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var QueryInterface |
||
| 88 | */ |
||
| 89 | private $query; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var Table |
||
| 93 | */ |
||
| 94 | private $table; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var array |
||
| 98 | */ |
||
| 99 | private $exists = []; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var array |
||
| 103 | */ |
||
| 104 | private $notExists = []; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param QueryInterface $query |
||
| 108 | */ |
||
| 109 | public function __construct(QueryInterface $query) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Deep copy for nested references. |
||
| 116 | * |
||
| 117 | * @return mixed |
||
| 118 | */ |
||
| 119 | public function __clone() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @return bool |
||
| 126 | */ |
||
| 127 | public function isEmpty() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @return string |
||
| 146 | */ |
||
| 147 | public function getConjunction() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @return array |
||
| 154 | */ |
||
| 155 | public function getSubWheres() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param $operator |
||
| 162 | * |
||
| 163 | * @return Where |
||
| 164 | */ |
||
| 165 | public function subWhere($operator = 'OR') |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @param string $operator |
||
| 179 | * |
||
| 180 | * @return $this |
||
| 181 | * |
||
| 182 | * @throws QueryException |
||
| 183 | */ |
||
| 184 | public function conjunction($operator) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @return Table |
||
| 198 | */ |
||
| 199 | public function getTable() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Used for subWhere query building. |
||
| 206 | * |
||
| 207 | * @param Table $table string |
||
| 208 | * |
||
| 209 | * @return $this |
||
| 210 | */ |
||
| 211 | public function setTable($table) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * equals alias. |
||
| 220 | * |
||
| 221 | * @param $column |
||
| 222 | * @param int $value |
||
| 223 | * |
||
| 224 | * @return static |
||
| 225 | */ |
||
| 226 | public function eq($column, $value) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @param $column |
||
| 233 | * @param $value |
||
| 234 | * |
||
| 235 | * @return static |
||
| 236 | */ |
||
| 237 | public function equals($column, $value) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @param $column |
||
| 244 | * @param $value |
||
| 245 | * @param string $operator |
||
| 246 | * |
||
| 247 | * @return $this |
||
| 248 | */ |
||
| 249 | public function compare($column, $value, $operator) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param $column |
||
| 264 | * |
||
| 265 | * @return Column|Select |
||
| 266 | */ |
||
| 267 | private function prepareColumn($column) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param string $column |
||
| 281 | * @param int $value |
||
| 282 | * |
||
| 283 | * @return static |
||
| 284 | */ |
||
| 285 | public function notEquals($column, $value) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param string $column |
||
| 292 | * @param int $value |
||
| 293 | * |
||
| 294 | * @return static |
||
| 295 | */ |
||
| 296 | public function greaterThan($column, $value) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @param string $column |
||
| 303 | * @param int $value |
||
| 304 | * |
||
| 305 | * @return static |
||
| 306 | */ |
||
| 307 | public function greaterThanOrEqual($column, $value) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @param string $column |
||
| 314 | * @param int $value |
||
| 315 | * |
||
| 316 | * @return static |
||
| 317 | */ |
||
| 318 | public function lessThan($column, $value) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @param string $column |
||
| 325 | * @param int $value |
||
| 326 | * |
||
| 327 | * @return static |
||
| 328 | */ |
||
| 329 | public function lessThanOrEqual($column, $value) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param string $column |
||
| 336 | * @param $value |
||
| 337 | * |
||
| 338 | * @return static |
||
| 339 | */ |
||
| 340 | public function like($column, $value) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @param string $column |
||
| 347 | * @param int $value |
||
| 348 | * |
||
| 349 | * @return static |
||
| 350 | */ |
||
| 351 | public function notLike($column, $value) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @param string[] $columns |
||
| 358 | * @param mixed[] $values |
||
| 359 | * |
||
| 360 | * @return static |
||
| 361 | */ |
||
| 362 | public function match(array $columns, array $values) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @param string[] $columns |
||
| 369 | * @param mixed[] $values |
||
| 370 | * @param string $mode |
||
| 371 | * |
||
| 372 | * @return $this |
||
| 373 | */ |
||
| 374 | protected function genericMatch(array &$columns, array &$values, $mode) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @param string $literal |
||
| 387 | * |
||
| 388 | * @return $this |
||
| 389 | */ |
||
| 390 | public function asLiteral($literal) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @param string[] $columns |
||
| 399 | * @param mixed[] $values |
||
| 400 | * |
||
| 401 | * @return $this |
||
| 402 | */ |
||
| 403 | public function matchBoolean(array $columns, array $values) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @param string[] $columns |
||
| 410 | * @param mixed[] $values |
||
| 411 | * |
||
| 412 | * @return $this |
||
| 413 | */ |
||
| 414 | public function matchWithQueryExpansion(array $columns, array $values) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @param string $column |
||
| 421 | * @param integer[] $values |
||
| 422 | * |
||
| 423 | * @return $this |
||
| 424 | */ |
||
| 425 | public function in($column, array $values) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @param string $column |
||
| 434 | * @param integer[] $values |
||
| 435 | * |
||
| 436 | * @return $this |
||
| 437 | */ |
||
| 438 | public function notIn($column, array $values) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @param string $column |
||
| 447 | * @param int $a |
||
| 448 | * @param int $b |
||
| 449 | * |
||
| 450 | * @return $this |
||
| 451 | */ |
||
| 452 | public function between($column, $a, $b) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * @param string $column |
||
| 462 | * |
||
| 463 | * @return static |
||
| 464 | */ |
||
| 465 | public function isNull($column) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * @param string $column |
||
| 475 | * |
||
| 476 | * @return $this |
||
| 477 | */ |
||
| 478 | public function isNotNull($column) |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @param string $column |
||
| 488 | * @param int $value |
||
| 489 | * |
||
| 490 | * @return $this |
||
| 491 | */ |
||
| 492 | public function addBitClause($column, $value) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * @param Select $select |
||
| 502 | * |
||
| 503 | * @return $this |
||
| 504 | */ |
||
| 505 | public function exists(Select $select) |
||
| 511 | |||
| 512 | /** |
||
| 513 | * @param Select $select |
||
| 514 | * |
||
| 515 | * @return $this |
||
| 516 | */ |
||
| 517 | public function notExists(Select $select) |
||
| 523 | |||
| 524 | /** |
||
| 525 | * @return array |
||
| 526 | */ |
||
| 527 | public function getMatches() |
||
| 531 | |||
| 532 | /** |
||
| 533 | * @return array |
||
| 534 | */ |
||
| 535 | public function getIns() |
||
| 539 | |||
| 540 | /** |
||
| 541 | * @return array |
||
| 542 | */ |
||
| 543 | public function getNotIns() |
||
| 547 | |||
| 548 | /** |
||
| 549 | * @return array |
||
| 550 | */ |
||
| 551 | public function getBetweens() |
||
| 555 | |||
| 556 | /** |
||
| 557 | * @return array |
||
| 558 | */ |
||
| 559 | public function getBooleans() |
||
| 563 | |||
| 564 | /** |
||
| 565 | * @return array |
||
| 566 | */ |
||
| 567 | public function getComparisons() |
||
| 571 | |||
| 572 | /** |
||
| 573 | * @return array |
||
| 574 | */ |
||
| 575 | public function getNotNull() |
||
| 579 | |||
| 580 | /** |
||
| 581 | * @return array |
||
| 582 | */ |
||
| 583 | public function getNull() |
||
| 587 | |||
| 588 | /** |
||
| 589 | * @return array |
||
| 590 | */ |
||
| 591 | public function getExists() |
||
| 595 | |||
| 596 | /** |
||
| 597 | * @return array |
||
| 598 | */ |
||
| 599 | public function getNotExists() |
||
| 603 | } |
||
| 604 |