Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like QueryBuilder 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 QueryBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class QueryBuilder implements QueryInterface |
||
| 18 | { |
||
| 19 | /* @var string The modifier. SELECT, INSERT INTO, UPDATE or DELETE */ |
||
| 20 | private $modifier; |
||
| 21 | |||
| 22 | /* @var string The table name. */ |
||
| 23 | private $table; |
||
| 24 | |||
| 25 | /* @var array The columns. */ |
||
| 26 | private $columns; |
||
| 27 | |||
| 28 | /* @var array The values. */ |
||
| 29 | private $values; |
||
| 30 | |||
| 31 | /* @var array The join conditions. */ |
||
| 32 | private $join; |
||
| 33 | |||
| 34 | /* @var QueryExpression The where clause. */ |
||
| 35 | private $where; |
||
| 36 | |||
| 37 | /* @var array The group by conditions. */ |
||
| 38 | private $groupBy; |
||
| 39 | |||
| 40 | /* @var array The order by conditions. */ |
||
| 41 | private $orderBy; |
||
| 42 | |||
| 43 | /* @var string The limit. */ |
||
| 44 | private $limit; |
||
| 45 | |||
| 46 | /* @var string The offset. */ |
||
| 47 | private $offset; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Construct a query builder object with the given table. |
||
| 51 | * |
||
| 52 | * @param string $table |
||
| 53 | */ |
||
| 54 | 45 | public function __construct($table) |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Returns a string representation of the query object. |
||
| 65 | * |
||
| 66 | * @return string a string representation of the query object. |
||
| 67 | */ |
||
| 68 | 45 | public function __toString() |
|
| 87 | |||
| 88 | /** |
||
| 89 | * {@inheritdoc} |
||
| 90 | */ |
||
| 91 | 33 | public function select($columns = ['*']) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Returns the select clause. |
||
| 101 | * |
||
| 102 | * @return string the select clause. |
||
| 103 | */ |
||
| 104 | 33 | private function getSelectClause() |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Returns the select query. |
||
| 111 | * |
||
| 112 | * @return string the select query. |
||
| 113 | */ |
||
| 114 | 33 | private function getSelectQuery() |
|
| 144 | |||
| 145 | /** |
||
| 146 | * {@inheritdoc} |
||
| 147 | */ |
||
| 148 | 3 | public function insert(array $values) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Returns the insert clause. |
||
| 158 | * |
||
| 159 | * @return string the insert clause. |
||
| 160 | */ |
||
| 161 | 3 | private function getInsertClause() |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Returns the insert query. |
||
| 176 | * |
||
| 177 | * @return string the insert query. |
||
| 178 | */ |
||
| 179 | 3 | private function getInsertQuery() |
|
| 183 | |||
| 184 | /** |
||
| 185 | * {@inheritdoc} |
||
| 186 | */ |
||
| 187 | 4 | public function update(array $values) |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Returns the update clause. |
||
| 197 | * |
||
| 198 | * @return string the update clause. |
||
| 199 | */ |
||
| 200 | 4 | private function getUpdateClause() |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Returns the update query. |
||
| 213 | * |
||
| 214 | * @return string the update query. |
||
| 215 | */ |
||
| 216 | 4 | View Code Duplication | private function getUpdateQuery() |
| 230 | |||
| 231 | /** |
||
| 232 | * {@inheritdoc} |
||
| 233 | */ |
||
| 234 | 3 | public function delete() |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Returns the delete clause. |
||
| 243 | * |
||
| 244 | * @return string the delete clause. |
||
| 245 | */ |
||
| 246 | 3 | private function getDeleteClause() |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Returns the delete query. |
||
| 253 | * |
||
| 254 | * @return string the delete query. |
||
| 255 | */ |
||
| 256 | 3 | View Code Duplication | private function getDeleteQuery() |
| 271 | |||
| 272 | /** |
||
| 273 | * {@inheritdoc} |
||
| 274 | */ |
||
| 275 | 2 | public function join($table, $primary, $operator, $secondary) |
|
| 281 | |||
| 282 | /** |
||
| 283 | * {@inheritdoc} |
||
| 284 | */ |
||
| 285 | 2 | public function leftJoin($table, $primary, $operator, $secondary) |
|
| 291 | |||
| 292 | /** |
||
| 293 | * {@inheritdoc} |
||
| 294 | */ |
||
| 295 | 2 | public function rightJoin($table, $primary, $operator, $secondary) |
|
| 301 | |||
| 302 | /** |
||
| 303 | * {@inheritdoc} |
||
| 304 | */ |
||
| 305 | 2 | public function crossJoin($table, $primary, $operator, $secondary) |
|
| 311 | |||
| 312 | 33 | private function getJoinClause() |
|
| 322 | |||
| 323 | /** |
||
| 324 | * {@inheritdoc} |
||
| 325 | */ |
||
| 326 | 15 | public function where(QueryExpression $whereClause) |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Returns the where clause. |
||
| 335 | * |
||
| 336 | * @return string the where clause. |
||
| 337 | */ |
||
| 338 | 40 | private function getWhereClause() |
|
| 346 | |||
| 347 | /** |
||
| 348 | * {@inheritdoc} |
||
| 349 | */ |
||
| 350 | 2 | public function groupBy($column) |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Returns the group by clause. |
||
| 359 | * |
||
| 360 | * @return string the group by clause. |
||
| 361 | */ |
||
| 362 | 33 | private function getGroupByClause() |
|
| 370 | |||
| 371 | /** |
||
| 372 | * {@inheritdoc} |
||
| 373 | */ |
||
| 374 | 4 | public function orderBy($column, $order = null) |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Returns the order by clause. |
||
| 389 | * |
||
| 390 | * @return string the order by clause. |
||
| 391 | */ |
||
| 392 | 33 | private function getOrderByClause() |
|
| 400 | |||
| 401 | /** |
||
| 402 | * {@inheritdoc} |
||
| 403 | */ |
||
| 404 | 6 | public function limit($limit) |
|
| 410 | |||
| 411 | /** |
||
| 412 | * Returns the limit clause. |
||
| 413 | * |
||
| 414 | * @return string the limit clause. |
||
| 415 | */ |
||
| 416 | 40 | private function getLimitClause() |
|
| 424 | |||
| 425 | /** |
||
| 426 | * {@inheritdoc} |
||
| 427 | */ |
||
| 428 | 2 | public function offset($offset) |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Returns the offset clause. |
||
| 437 | * |
||
| 438 | * @return string the offset clause. |
||
| 439 | */ |
||
| 440 | 33 | private function getOffsetClause() |
|
| 448 | } |
||
| 449 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..