Complex classes like SqlSelect 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 SqlSelect, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class SqlSelect extends SqlActions |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var string $returnType PHP Type used for return result |
||
| 18 | */ |
||
| 19 | protected $returnType = ''; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var object $from Informations about main table. Used for FROM part |
||
| 23 | */ |
||
| 24 | protected $table; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var array $subQueries All sub-queries |
||
| 28 | */ |
||
| 29 | protected $subQueries = []; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var array $join List of all INNER JOIN |
||
| 33 | */ |
||
| 34 | protected $join = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array $joinLeft List of all LEFT JOIN |
||
| 38 | */ |
||
| 39 | protected $joinLeft = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var array $joinRight List of all RIGHT JOIN |
||
| 43 | */ |
||
| 44 | protected $joinRight = []; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string[] $order All columns used for ORDER BY part |
||
| 48 | */ |
||
| 49 | protected $order = []; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var string $limit The LIMIT part |
||
| 53 | */ |
||
| 54 | protected $limit = ''; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string[] $group The GROUP BY part |
||
| 58 | */ |
||
| 59 | protected $group = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Constructor |
||
| 63 | * |
||
| 64 | * @param \BfwSql\SqlConnect $sqlConnect Instance of SGBD connexion |
||
| 65 | * @param string $returnType PHP type used for return result |
||
| 66 | */ |
||
| 67 | public function __construct(SqlConnect $sqlConnect, $returnType) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Define object used to save informations about a table |
||
| 75 | * This object will be used to write query |
||
| 76 | * |
||
| 77 | * @param string|array $table Tables informations |
||
| 78 | * |
||
| 79 | * @return \stdClass |
||
| 80 | */ |
||
| 81 | protected function obtainTableInfos($table) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Add columns for select |
||
| 105 | * |
||
| 106 | * @param array|string $columns Columns to add |
||
| 107 | * @param string $tableName Table name for will be columns added |
||
| 108 | * |
||
| 109 | * @return void |
||
| 110 | */ |
||
| 111 | protected function addColumnsForSelect($columns, $tableName) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Declare information for FROM part and column will be get for main table |
||
| 145 | * |
||
| 146 | * @param string|array $table Table name. |
||
| 147 | * It can be an array if a table shortcut is declared. |
||
| 148 | * In array mode, the format is ['asValue' => 'tableName'] |
||
| 149 | * @param string|array $columns (default: "*") Columns will be get for |
||
| 150 | * the table declared in first argument |
||
| 151 | * |
||
| 152 | * @return \BfwSql\SqlSelect |
||
| 153 | */ |
||
| 154 | public function from($table, $columns='*') |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Add a sub-query in the SELECT part on the request |
||
| 170 | * |
||
| 171 | * @param \BfwSql\SqlActions|string $subRequest The sub-request |
||
| 172 | * @param string $shortcut The shortcut to use for |
||
| 173 | * this query in SELECT part |
||
| 174 | * |
||
| 175 | * @return \BfwSql\SqlSelect |
||
| 176 | */ |
||
| 177 | public function subQuery($subRequest, $shortcut) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Add a (inner|left|right) join to the request |
||
| 203 | * |
||
| 204 | * @param string $joinPropertyName The name of the property in this |
||
| 205 | * class where the join is add |
||
| 206 | * @param string|array $table Name of the table concerned by |
||
| 207 | * the join. Or an array with the table shortcut in key. |
||
| 208 | * @param string $joinOn SQL part "ON" for this join |
||
| 209 | * @param string|array $joinColumns Columns from the table joined to |
||
| 210 | * add in the SELECT part of the request |
||
| 211 | * |
||
| 212 | * @return \BfwSql\SqlSelect |
||
| 213 | */ |
||
| 214 | protected function createJoin( |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Add a INNER JOIN to the request |
||
| 237 | * |
||
| 238 | * @param string|array $table Name of the table concerned by the |
||
| 239 | * join. Or an array with the table shortcut in key. |
||
| 240 | * @param string $joinOn SQL part "ON" for this join |
||
| 241 | * @param string|array $joinColumns Columns from the table joined to add |
||
| 242 | * in the SELECT part of the request |
||
| 243 | * |
||
| 244 | * @return \BfwSql\SqlSelect |
||
| 245 | */ |
||
| 246 | public function join($table, $joinOn, $joinColumns='*') |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Add a LEFT JOIN to the request |
||
| 253 | * |
||
| 254 | * @param string|array $table Name of the table concerned by the |
||
| 255 | * join. Or an array with the table shortcut in key. |
||
| 256 | * @param string $joinOn SQL part "ON" for this join |
||
| 257 | * @param string|array $joinColumns Columns from the table joined to add |
||
| 258 | * in the SELECT part of the request |
||
| 259 | * |
||
| 260 | * @return \BfwSql\SqlSelect |
||
| 261 | */ |
||
| 262 | public function joinLeft($table, $joinOn, $joinColumns='*') |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Add a RIGHT JOIN to the request |
||
| 269 | * |
||
| 270 | * @param string|array $table Name of the table concerned by the |
||
| 271 | * join. Or an array with the table shortcut in key. |
||
| 272 | * @param string $joinOn SQL part "ON" for this join |
||
| 273 | * @param string|array $joinColumns Columns from the table joined to add |
||
| 274 | * in the SELECT part of the request |
||
| 275 | * |
||
| 276 | * @return \BfwSql\SqlSelect |
||
| 277 | */ |
||
| 278 | public function joinRight($table, $joinOn, $joinColumns='*') |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Add a order condition to the request for the ORDER BY part |
||
| 285 | * |
||
| 286 | * @param string $condition The new condition |
||
| 287 | * |
||
| 288 | * @return \BfwSql\SqlSelect |
||
| 289 | */ |
||
| 290 | public function order($condition) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Add information about the LIMIT part in request |
||
| 298 | * |
||
| 299 | * @param array|integer $limit If it's a integer, the number of row to |
||
| 300 | * return. If an array, the format is [numberToStart, numberOfRowToReturn] |
||
| 301 | * |
||
| 302 | * @return \BfwSql\SqlSelect |
||
| 303 | */ |
||
| 304 | public function limit($limit) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Add a GROUP BY part to the request |
||
| 321 | * |
||
| 322 | * @param string $condition The condition to use in GROUP BY |
||
| 323 | * |
||
| 324 | * @return \BfwSql\SqlSelect |
||
| 325 | */ |
||
| 326 | public function group($condition) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Return the PDO constant for the returnType declared |
||
| 334 | * |
||
| 335 | * @return integer |
||
| 336 | */ |
||
| 337 | protected function obtainPdoFetchType() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Fetch one row of the result |
||
| 348 | * |
||
| 349 | * @return mixed |
||
| 350 | */ |
||
| 351 | public function fetchRow() |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Fetch all rows returned by the request |
||
| 359 | * |
||
| 360 | * @return mixed |
||
| 361 | */ |
||
| 362 | public function fetchAll() |
||
| 374 | |||
| 375 | /** |
||
| 376 | * {@inheritdoc} |
||
| 377 | */ |
||
| 378 | public function assembleRequest() |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Write the SELECT part of the request |
||
| 395 | * |
||
| 396 | * @return string |
||
| 397 | */ |
||
| 398 | protected function generateSelect() |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Write the FROM part of the request |
||
| 428 | * |
||
| 429 | * @return string |
||
| 430 | */ |
||
| 431 | protected function generateFrom() |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Write a (inner|left|right) join in the request |
||
| 444 | * |
||
| 445 | * @param string $joinProperty The join property name |
||
| 446 | * |
||
| 447 | * @return string |
||
| 448 | */ |
||
| 449 | protected function generateJoin($joinProperty) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Write the ORDER BY part for the request |
||
| 479 | * |
||
| 480 | * @return string |
||
| 481 | */ |
||
| 482 | protected function generateOrderBy() |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Write the GRUOP BY part for the request |
||
| 502 | * |
||
| 503 | * @return string |
||
| 504 | */ |
||
| 505 | protected function generateGroupBy() |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Write the LIMIT part for the request |
||
| 526 | * |
||
| 527 | * @return string |
||
| 528 | */ |
||
| 529 | protected function generateLimit() |
||
| 538 | } |
||
| 539 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: