Complex classes like DBQueryBuilder 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 DBQueryBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class DBQueryBuilder { |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Determines the line separator to use. |
||
| 13 | * |
||
| 14 | * @return string Non-empty whitespace character |
||
| 15 | */ |
||
| 16 | public function getSeparator() { |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Builds a sql query with the specified connection |
||
| 22 | * |
||
| 23 | * @param SQLExpression $query The expression object to build from |
||
| 24 | * @param array $parameters Out parameter for the resulting query parameters |
||
| 25 | * @return string The resulting SQL as a string |
||
| 26 | */ |
||
| 27 | public function buildSQL(SQLExpression $query, &$parameters) { |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Builds a query from a SQLSelect expression |
||
| 50 | * |
||
| 51 | * @param SQLSelect $query The expression object to build from |
||
| 52 | * @param array $parameters Out parameter for the resulting query parameters |
||
| 53 | * @return string Completed SQL string |
||
| 54 | */ |
||
| 55 | protected function buildSelectQuery(SQLSelect $query, array &$parameters) { |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Builds a query from a SQLDelete expression |
||
| 68 | * |
||
| 69 | * @param SQLDelete $query The expression object to build from |
||
| 70 | * @param array $parameters Out parameter for the resulting query parameters |
||
| 71 | * @return string Completed SQL string |
||
| 72 | */ |
||
| 73 | protected function buildDeleteQuery(SQLDelete $query, array &$parameters) { |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Builds a query from a SQLInsert expression |
||
| 82 | * |
||
| 83 | * @param SQLInsert $query The expression object to build from |
||
| 84 | * @param array $parameters Out parameter for the resulting query parameters |
||
| 85 | * @return string Completed SQL string |
||
| 86 | */ |
||
| 87 | protected function buildInsertQuery(SQLInsert $query, array &$parameters) { |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Builds a query from a SQLUpdate expression |
||
| 129 | * |
||
| 130 | * @param SQLUpdate $query The expression object to build from |
||
| 131 | * @param array $parameters Out parameter for the resulting query parameters |
||
| 132 | * @return string Completed SQL string |
||
| 133 | */ |
||
| 134 | protected function buildUpdateQuery(SQLUpdate $query, array &$parameters) { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Returns the SELECT clauses ready for inserting into a query. |
||
| 142 | * |
||
| 143 | * @param SQLSelect $query The expression object to build from |
||
| 144 | * @param array $parameters Out parameter for the resulting query parameters |
||
| 145 | * @return string Completed select part of statement |
||
| 146 | */ |
||
| 147 | protected function buildSelectFragment(SQLSelect $query, array &$parameters) { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Return the DELETE clause ready for inserting into a query. |
||
| 169 | * |
||
| 170 | * @param SQLExpression $query The expression object to build from |
||
| 171 | * @param array $parameters Out parameter for the resulting query parameters |
||
| 172 | * @return string Completed delete part of statement |
||
| 173 | */ |
||
| 174 | public function buildDeleteFragment(SQLDelete $query, array &$parameters) { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Return the UPDATE clause ready for inserting into a query. |
||
| 188 | * |
||
| 189 | * @param SQLExpression $query The expression object to build from |
||
| 190 | * @param array $parameters Out parameter for the resulting query parameters |
||
| 191 | * @return string Completed from part of statement |
||
| 192 | */ |
||
| 193 | public function buildUpdateFragment(SQLUpdate $query, array &$parameters) { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Return the FROM clause ready for inserting into a query. |
||
| 214 | * |
||
| 215 | * @param SQLExpression $query The expression object to build from |
||
| 216 | * @param array $parameters Out parameter for the resulting query parameters |
||
| 217 | * @return string Completed from part of statement |
||
| 218 | */ |
||
| 219 | public function buildFromFragment(SQLConditionalExpression $query, array &$parameters) { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Returns the WHERE clauses ready for inserting into a query. |
||
| 228 | * |
||
| 229 | * @param SQLExpression $query The expression object to build from |
||
| 230 | * @param array $parameters Out parameter for the resulting query parameters |
||
| 231 | * @return string Completed where condition |
||
| 232 | */ |
||
| 233 | public function buildWhereFragment(SQLConditionalExpression $query, array &$parameters) { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Returns the ORDER BY clauses ready for inserting into a query. |
||
| 247 | * |
||
| 248 | * @param SQLSelect $query The expression object to build from |
||
| 249 | * @param array $parameters Out parameter for the resulting query parameters |
||
| 250 | * @return string Completed order by part of statement |
||
| 251 | */ |
||
| 252 | public function buildOrderByFragment(SQLSelect $query, array &$parameters) { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Returns the GROUP BY clauses ready for inserting into a query. |
||
| 268 | * |
||
| 269 | * @param SQLSelect $query The expression object to build from |
||
| 270 | * @param array $parameters Out parameter for the resulting query parameters |
||
| 271 | * @return string Completed group part of statement |
||
| 272 | */ |
||
| 273 | public function buildGroupByFragment(SQLSelect $query, array &$parameters) { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Returns the HAVING clauses ready for inserting into a query. |
||
| 283 | * |
||
| 284 | * @param SQLSelect $query The expression object to build from |
||
| 285 | * @param array $parameters Out parameter for the resulting query parameters |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | public function buildHavingFragment(SQLSelect $query, array &$parameters) { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Return the LIMIT clause ready for inserting into a query. |
||
| 301 | * |
||
| 302 | * @param SQLSelect $query The expression object to build from |
||
| 303 | * @param array $parameters Out parameter for the resulting query parameters |
||
| 304 | * @return string The finalised limit SQL fragment |
||
| 305 | */ |
||
| 306 | public function buildLimitFragment(SQLSelect $query, array &$parameters) { |
||
| 332 | } |
||
| 333 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.