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 |
||
| 24 | class QueryBuilder extends Object |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var Connection |
||
| 28 | */ |
||
| 29 | public $db; |
||
| 30 | |||
| 31 | public function __construct($connection, $config = []) |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param array $params |
||
| 39 | * @return Request |
||
| 40 | */ |
||
| 41 | public function createRequest($params = []) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Builds config array to create Command. |
||
| 48 | * @param Query $query |
||
| 49 | * @throws NotSupportedException |
||
| 50 | * @return Request |
||
| 51 | */ |
||
| 52 | public function build(Query $query, $params = [], array $options = []) |
||
| 72 | |||
| 73 | public function buildAuth(Request $request, $params = []) |
||
| 77 | |||
| 78 | public function buildMethod(Request $request, $action, $params = []) |
||
| 95 | |||
| 96 | public function buildFrom(Request $request, $from, $params = []) |
||
| 100 | |||
| 101 | public function buildWhere(Request $request, $condition, $params = []) |
||
| 111 | |||
| 112 | public function buildCount(Request $request, $count, $params = []) |
||
| 123 | |||
| 124 | public function buildOrderBy(Request $request, $orderBy, $params = []) |
||
| 128 | |||
| 129 | public function buildLimit(Request $request, $limit, $offset = 0, $params = []) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Creates insert request. |
||
| 136 | * @param string $url |
||
| 137 | * @param array $columns |
||
| 138 | * @param array $options |
||
| 139 | * @return AbstractTransport |
||
| 140 | */ |
||
| 141 | public function insert($url, $columns, &$params = [], array $options = []) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Creates update request. |
||
| 156 | * @param string $url |
||
| 157 | * @param array $columns |
||
| 158 | * @param array $condition |
||
| 159 | * @param array $options |
||
| 160 | * @return AbstractTransport |
||
| 161 | */ |
||
| 162 | View Code Duplication | public function update($url, $columns, $condition = [], &$params = [], array $options = []) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Creates delete request. |
||
| 178 | * @param string $table |
||
| 179 | * @param array $condition |
||
| 180 | * @param array $options |
||
| 181 | * @return AbstractTransport |
||
| 182 | */ |
||
| 183 | View Code Duplication | public function delete($table, $condition = [], &$params = [], array $options = []) |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Creates request for given action. |
||
| 197 | * @param string $action |
||
| 198 | * @param string $table |
||
| 199 | * @param mixed $body |
||
| 200 | * @param array $options |
||
| 201 | * @return AbstractTransport |
||
| 202 | */ |
||
| 203 | public function perform($action, $table, $body, $options = []) |
||
| 209 | |||
| 210 | public function buildCondition($condition) |
||
| 247 | |||
| 248 | protected function buildHashCondition($condition) |
||
| 262 | |||
| 263 | protected function buildLikeCondition($operator, $operands) |
||
| 267 | |||
| 268 | protected function buildIlikeCondition($operator, $operands) |
||
| 272 | |||
| 273 | protected function buildCompareCondition($operator, $operands) |
||
| 281 | |||
| 282 | protected function buildAndCondition($operator, $operands) |
||
| 296 | |||
| 297 | protected function buildBetweenCondition($operator, $operands) |
||
| 301 | |||
| 302 | protected function buildInCondition($operator, $operands, $not = false) |
||
| 332 | |||
| 333 | protected function buildNotInCondition($operator, $operands) |
||
| 337 | |||
| 338 | protected function buildEqCondition($operator, $operands) |
||
| 344 | |||
| 345 | protected function buildNotEqCondition($operator, $operands) |
||
| 351 | |||
| 352 | protected function buildCompositeInCondition($operator, $columns, $values) |
||
| 356 | } |
||
| 357 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.