Complex classes like Select 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 Select, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class Select extends AbstractBaseQuery |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var Table |
||
| 24 | */ |
||
| 25 | protected $table; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | protected $groupBy = []; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $camelCaseTableName = ''; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var Where |
||
| 39 | */ |
||
| 40 | protected $having; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | protected $havingOperator = 'AND'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var bool |
||
| 49 | */ |
||
| 50 | protected $isDistinct = false; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var Where |
||
| 54 | */ |
||
| 55 | protected $where; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var JoinQuery |
||
| 59 | */ |
||
| 60 | protected $joinQuery; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var ColumnQuery |
||
| 64 | */ |
||
| 65 | protected $columnQuery; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param string $table |
||
| 69 | * @param array $columns |
||
| 70 | */ |
||
| 71 | public function __construct($table = null, array $columns = null) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * This __clone method will create an exact clone but without the object references due to the fact these |
||
| 83 | * are lost in the process of serialization and un-serialization. |
||
| 84 | * |
||
| 85 | * @return Select |
||
| 86 | */ |
||
| 87 | public function __clone() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @return string |
||
| 94 | */ |
||
| 95 | public function partName() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param string $table |
||
| 102 | * @param string $selfColumn |
||
| 103 | * @param string $refColumn |
||
| 104 | * @param string[] $columns |
||
| 105 | * |
||
| 106 | * @return Select |
||
| 107 | */ |
||
| 108 | public function leftJoin($table, $selfColumn = null, $refColumn = null, $columns = []) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param string $table |
||
| 115 | * @param string $selfColumn |
||
| 116 | * @param string $refColumn |
||
| 117 | * @param string[] $columns |
||
| 118 | * @param string $joinType |
||
| 119 | * |
||
| 120 | * @return Select |
||
| 121 | */ |
||
| 122 | public function join( |
||
| 131 | |||
| 132 | /** |
||
| 133 | * WHERE constrains used for the ON clause of a (LEFT/RIGHT/INNER/CROSS) JOIN. |
||
| 134 | * |
||
| 135 | * @return Where |
||
| 136 | */ |
||
| 137 | public function joinCondition() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @param Select $select |
||
| 144 | * @param string $selfColumn |
||
| 145 | * @param string $refColumn |
||
| 146 | * |
||
| 147 | * @return Select |
||
| 148 | */ |
||
| 149 | public function addJoin(Select $select, $selfColumn, $refColumn) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Transforms Select in a joint. |
||
| 156 | * |
||
| 157 | * @param bool $isJoin |
||
| 158 | * |
||
| 159 | * @return JoinQuery |
||
| 160 | */ |
||
| 161 | public function isJoin($isJoin = true) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param string $table |
||
| 168 | * @param string $selfColumn |
||
| 169 | * @param string $refColumn |
||
| 170 | * @param string[] $columns |
||
| 171 | * |
||
| 172 | * @internal param null $selectClass |
||
| 173 | * |
||
| 174 | * @return Select |
||
| 175 | */ |
||
| 176 | public function rightJoin($table, $selfColumn = null, $refColumn = null, $columns = []) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param string $table |
||
| 183 | * @param string $selfColumn |
||
| 184 | * @param string $refColumn |
||
| 185 | * @param string[] $columns |
||
| 186 | * |
||
| 187 | * @return Select |
||
| 188 | */ |
||
| 189 | public function crossJoin($table, $selfColumn = null, $refColumn = null, $columns = []) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param string $table |
||
| 196 | * @param string $selfColumn |
||
| 197 | * @param string $refColumn |
||
| 198 | * @param string[] $columns |
||
| 199 | * |
||
| 200 | * @return Select |
||
| 201 | */ |
||
| 202 | public function innerJoin($table, $selfColumn = null, $refColumn = null, $columns = []) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Alias to joinCondition. |
||
| 209 | * |
||
| 210 | * @return Where |
||
| 211 | */ |
||
| 212 | public function on() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @return bool |
||
| 219 | */ |
||
| 220 | public function isJoinSelect() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @return array |
||
| 227 | */ |
||
| 228 | public function getAllColumns() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @return \NilPortugues\Sql\QueryBuilder\Syntax\Column |
||
| 235 | * |
||
| 236 | * @throws QueryException |
||
| 237 | */ |
||
| 238 | public function getColumns() |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Sets the column names used to write the SELECT statement. |
||
| 245 | * If key is set, key is the column's alias. Value is always the column names. |
||
| 246 | * |
||
| 247 | * @param string[] $columns |
||
| 248 | * |
||
| 249 | * @return ColumnQuery |
||
| 250 | */ |
||
| 251 | public function setColumns(array $columns) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Allows setting a Select query as a column value. |
||
| 258 | * |
||
| 259 | * @param array $column |
||
| 260 | * |
||
| 261 | * @return ColumnQuery |
||
| 262 | */ |
||
| 263 | public function setSelectAsColumn(array $column) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @return array |
||
| 270 | */ |
||
| 271 | public function getColumnSelects() |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Allows setting a value to the select statement. |
||
| 278 | * |
||
| 279 | * @param string $value |
||
| 280 | * @param string $alias |
||
| 281 | * |
||
| 282 | * @return ColumnQuery |
||
| 283 | */ |
||
| 284 | public function setValueAsColumn($value, $alias) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @return array |
||
| 291 | */ |
||
| 292 | public function getColumnValues() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Allows calculation on columns using predefined SQL functions. |
||
| 299 | * |
||
| 300 | * @param string $funcName |
||
| 301 | * @param string[] $arguments |
||
| 302 | * @param string $alias |
||
| 303 | * |
||
| 304 | * @return ColumnQuery |
||
| 305 | */ |
||
| 306 | public function setFunctionAsColumn($funcName, array $arguments, $alias) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @return array |
||
| 313 | */ |
||
| 314 | public function getColumnFuncs() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Returns all the Where conditions to the BuilderInterface class in order to write the SQL WHERE statement. |
||
| 321 | * |
||
| 322 | * @return array |
||
| 323 | */ |
||
| 324 | public function getAllWheres() |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @param null|Where $data |
||
| 331 | * @param string $operation |
||
| 332 | * |
||
| 333 | * @return array |
||
| 334 | */ |
||
| 335 | protected function getAllOperation($data, $operation) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @return array |
||
| 352 | */ |
||
| 353 | public function getAllHavings() |
||
| 357 | |||
| 358 | /** |
||
| 359 | * @param string $columnName |
||
| 360 | * @param string $alias |
||
| 361 | * |
||
| 362 | * @return ColumnQuery |
||
| 363 | */ |
||
| 364 | public function count($columnName = '*', $alias = '') |
||
| 368 | |||
| 369 | /** |
||
| 370 | * @return bool |
||
| 371 | */ |
||
| 372 | public function isCount() |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @param int $start |
||
| 379 | * @param $count |
||
| 380 | * |
||
| 381 | * @return $this |
||
| 382 | */ |
||
| 383 | public function limit($start, $count = 0) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @return array |
||
| 393 | */ |
||
| 394 | public function getAllJoins() |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @return array |
||
| 401 | */ |
||
| 402 | public function getGroupBy() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @param string[] $columns |
||
| 409 | * |
||
| 410 | * @return $this |
||
| 411 | */ |
||
| 412 | public function groupBy(array $columns) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @return Where |
||
| 421 | */ |
||
| 422 | public function getJoinCondition() |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @return string |
||
| 429 | */ |
||
| 430 | public function getJoinType() |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @param string|null $joinType |
||
| 437 | * |
||
| 438 | * @return $this |
||
| 439 | */ |
||
| 440 | public function setJoinType($joinType) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @param $havingOperator |
||
| 449 | * |
||
| 450 | * @throws QueryException |
||
| 451 | * |
||
| 452 | * @return Where |
||
| 453 | */ |
||
| 454 | public function having($havingOperator = 'AND') |
||
| 470 | |||
| 471 | /** |
||
| 472 | * @return string |
||
| 473 | */ |
||
| 474 | public function getHavingOperator() |
||
| 478 | |||
| 479 | /** |
||
| 480 | * @return $this |
||
| 481 | */ |
||
| 482 | public function distinct() |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @return bool |
||
| 491 | */ |
||
| 492 | public function isDistinct() |
||
| 496 | |||
| 497 | /** |
||
| 498 | * @return array |
||
| 499 | */ |
||
| 500 | public function getAllOrderBy() |
||
| 510 | } |
||
| 511 |