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 |
||
| 15 | class Select extends AbstractActions |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @const ERR_TABLE_INFOS_BAD_FORMAT Exception code if table structure is |
||
| 19 | * not correct. |
||
| 20 | */ |
||
| 21 | const ERR_TABLE_INFOS_BAD_FORMAT = 2304001; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @const ERR_SUB_QUERY_FORMAT Exception code if the sub-query has not the |
||
| 25 | * correct format (string or AbstractAction object). |
||
| 26 | */ |
||
| 27 | const ERR_SUB_QUERY_FORMAT = 2304002; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @const ERR_GENERATE_FROM_MISSING_TABLE_NAME Exception code if the user |
||
| 31 | * try to generate a request without table name. |
||
| 32 | */ |
||
| 33 | const ERR_GENERATE_FROM_MISSING_TABLE_NAME = 2304003; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var string $returnType PHP Type used for return result |
||
| 37 | */ |
||
| 38 | protected $returnType = ''; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var object $mainTable Informations about main table. Used for FROM part |
||
| 42 | */ |
||
| 43 | protected $mainTable; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var array $subQueries All sub-queries |
||
| 47 | */ |
||
| 48 | protected $subQueries = []; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var array $join List of all INNER JOIN |
||
| 52 | */ |
||
| 53 | protected $join = []; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var array $joinLeft List of all LEFT JOIN |
||
| 57 | */ |
||
| 58 | protected $joinLeft = []; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var array $joinRight List of all RIGHT JOIN |
||
| 62 | */ |
||
| 63 | protected $joinRight = []; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var string[] $order All columns used for ORDER BY part |
||
| 67 | */ |
||
| 68 | protected $order = []; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var string $limit The LIMIT part |
||
| 72 | */ |
||
| 73 | protected $limit = ''; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var string[] $group The GROUP BY part |
||
| 77 | */ |
||
| 78 | protected $group = []; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Constructor |
||
| 82 | * |
||
| 83 | * @param \BfwSql\SqlConnect $sqlConnect Instance of SGBD connexion |
||
| 84 | * @param string $returnType PHP type used for return result |
||
| 85 | */ |
||
| 86 | public function __construct(\BfwSql\SqlConnect $sqlConnect, $returnType) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Getter accessor to returnType property |
||
| 94 | * |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | public function getReturnType() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Getter accessor to mainTable property |
||
| 104 | * |
||
| 105 | * @return \stdClass |
||
| 106 | */ |
||
| 107 | public function getMainTable() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Getter accessor to subQueries property |
||
| 114 | * |
||
| 115 | * @return array |
||
| 116 | */ |
||
| 117 | public function getSubQueries() |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Getter accessor to join property |
||
| 124 | * |
||
| 125 | * @return array |
||
| 126 | */ |
||
| 127 | public function getJoin() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Getter accessor to joinLeft property |
||
| 134 | * |
||
| 135 | * @return array |
||
| 136 | */ |
||
| 137 | public function getJoinLeft() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Getter accessor to joinRight property |
||
| 144 | * |
||
| 145 | * @return array |
||
| 146 | */ |
||
| 147 | public function getJoinRight() |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Getter accessor to order property |
||
| 154 | * |
||
| 155 | * @return string[] |
||
| 156 | */ |
||
| 157 | public function getOrder() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Getter accessor to limit property |
||
| 164 | * |
||
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | public function getLimit() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Getter accessor to group property |
||
| 174 | * |
||
| 175 | * @return string[] |
||
| 176 | */ |
||
| 177 | public function getGroup() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Define object used to save informations about a table |
||
| 184 | * This object will be used to write query |
||
| 185 | * |
||
| 186 | * @param string|array $table Tables informations |
||
| 187 | * |
||
| 188 | * @return \stdClass |
||
| 189 | */ |
||
| 190 | protected function obtainTableInfos($table) |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Add columns for select |
||
| 217 | * |
||
| 218 | * @param array|string $columns Columns to add |
||
| 219 | * @param string $tableName Table name for will be columns added |
||
| 220 | * |
||
| 221 | * @return void |
||
| 222 | */ |
||
| 223 | protected function addColumnsForSelect($columns, $tableName) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Declare information for FROM part and column will be get for main table |
||
| 260 | * |
||
| 261 | * @param string|array $table Table name. |
||
| 262 | * It can be an array if a table shortcut is declared. |
||
| 263 | * In array mode, the format is ['asValue' => 'tableName'] |
||
| 264 | * @param string|array $columns (default: "*") Columns will be get for |
||
| 265 | * the table declared in first argument |
||
| 266 | * |
||
| 267 | * @return \BfwSql\SqlSelect |
||
| 268 | */ |
||
| 269 | public function from($table, $columns = '*') |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Add a sub-query in the SELECT part on the request |
||
| 285 | * |
||
| 286 | * @param \BfwSql\Actions\AbstractActions|string $subRequest The sub-request |
||
| 287 | * @param string $shortcut The shortcut to use for |
||
| 288 | * this query in SELECT part |
||
| 289 | * |
||
| 290 | * @return \BfwSql\SqlSelect |
||
| 291 | */ |
||
| 292 | public function subQuery($subRequest, $shortcut) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Add a (inner|left|right) join to the request |
||
| 319 | * |
||
| 320 | * @param string $joinPropertyName The name of the property in this |
||
| 321 | * class where the join is add |
||
| 322 | * @param string|array $table Name of the table concerned by |
||
| 323 | * the join. Or an array with the table shortcut in key. |
||
| 324 | * @param string $joinOn SQL part "ON" for this join |
||
| 325 | * @param string|array $joinColumns Columns from the table joined to |
||
| 326 | * add in the SELECT part of the request |
||
| 327 | * |
||
| 328 | * @return \BfwSql\SqlSelect |
||
| 329 | */ |
||
| 330 | protected function createJoin( |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Add a INNER JOIN to the request |
||
| 352 | * |
||
| 353 | * @param string|array $table Name of the table concerned by the |
||
| 354 | * join. Or an array with the table shortcut in key. |
||
| 355 | * @param string $joinOn SQL part "ON" for this join |
||
| 356 | * @param string|array $joinColumns Columns from the table joined to add |
||
| 357 | * in the SELECT part of the request |
||
| 358 | * |
||
| 359 | * @return \BfwSql\SqlSelect |
||
| 360 | */ |
||
| 361 | public function join($table, $joinOn, $joinColumns = '*') |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Add a LEFT JOIN to the request |
||
| 368 | * |
||
| 369 | * @param string|array $table Name of the table concerned by the |
||
| 370 | * join. Or an array with the table shortcut in key. |
||
| 371 | * @param string $joinOn SQL part "ON" for this join |
||
| 372 | * @param string|array $joinColumns Columns from the table joined to add |
||
| 373 | * in the SELECT part of the request |
||
| 374 | * |
||
| 375 | * @return \BfwSql\SqlSelect |
||
| 376 | */ |
||
| 377 | public function joinLeft($table, $joinOn, $joinColumns = '*') |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Add a RIGHT JOIN to the request |
||
| 384 | * |
||
| 385 | * @param string|array $table Name of the table concerned by the |
||
| 386 | * join. Or an array with the table shortcut in key. |
||
| 387 | * @param string $joinOn SQL part "ON" for this join |
||
| 388 | * @param string|array $joinColumns Columns from the table joined to add |
||
| 389 | * in the SELECT part of the request |
||
| 390 | * |
||
| 391 | * @return \BfwSql\SqlSelect |
||
| 392 | */ |
||
| 393 | public function joinRight($table, $joinOn, $joinColumns = '*') |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Add a order condition to the request for the ORDER BY part |
||
| 400 | * |
||
| 401 | * @param string $condition The new condition |
||
| 402 | * |
||
| 403 | * @return \BfwSql\SqlSelect |
||
| 404 | */ |
||
| 405 | public function order($condition) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Add information about the LIMIT part in request |
||
| 413 | * |
||
| 414 | * @param array|integer $limit If it's a integer, the number of row to |
||
| 415 | * return. If an array, the format is [numberToStart, numberOfRowToReturn] |
||
| 416 | * |
||
| 417 | * @return \BfwSql\SqlSelect |
||
| 418 | */ |
||
| 419 | public function limit($limit) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Add a GROUP BY part to the request |
||
| 436 | * |
||
| 437 | * @param string $condition The condition to use in GROUP BY |
||
| 438 | * |
||
| 439 | * @return \BfwSql\SqlSelect |
||
| 440 | */ |
||
| 441 | public function group($condition) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Return the PDO constant for the returnType declared |
||
| 449 | * |
||
| 450 | * @return integer |
||
| 451 | */ |
||
| 452 | protected function obtainPdoFetchType() |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Fetch one row of the result |
||
| 463 | * |
||
| 464 | * @return mixed |
||
| 465 | */ |
||
| 466 | public function fetchRow() |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Fetch all rows returned by the request |
||
| 474 | * |
||
| 475 | * @return generator |
||
| 476 | */ |
||
| 477 | public function fetchAll() |
||
| 485 | |||
| 486 | /** |
||
| 487 | * {@inheritdoc} |
||
| 488 | */ |
||
| 489 | protected function assembleRequest() |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Write the SELECT part of the request |
||
| 504 | * |
||
| 505 | * @return string |
||
| 506 | */ |
||
| 507 | protected function generateSelect() |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Write the FROM part of the request |
||
| 535 | * |
||
| 536 | * @return string |
||
| 537 | */ |
||
| 538 | protected function generateFrom() |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Write a (inner|left|right) join in the request |
||
| 559 | * |
||
| 560 | * @param string $joinProperty The join property name |
||
| 561 | * |
||
| 562 | * @return string |
||
| 563 | */ |
||
| 564 | protected function generateJoin($joinProperty) |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Write the ORDER BY part for the request |
||
| 593 | * |
||
| 594 | * @return string |
||
| 595 | */ |
||
| 596 | protected function generateOrderBy() |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Write the GRUOP BY part for the request |
||
| 616 | * |
||
| 617 | * @return string |
||
| 618 | */ |
||
| 619 | protected function generateGroupBy() |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Write the LIMIT part for the request |
||
| 639 | * |
||
| 640 | * @return string |
||
| 641 | */ |
||
| 642 | protected function generateLimit() |
||
| 651 | } |
||
| 652 |