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 |
||
5 | class QueryBuilder |
||
6 | { |
||
7 | const TYPE_INSERT = 1; |
||
8 | const TYPE_SELECT = 2; |
||
9 | const TYPE_UPDATE = 3; |
||
10 | const TYPE_DELETE = 4; |
||
11 | const TYPE_REPLACE = 5; |
||
12 | |||
13 | /** |
||
14 | * @var Connection |
||
15 | */ |
||
16 | private $conn; |
||
17 | |||
18 | private $type; |
||
19 | |||
20 | private $sqlParts = array( |
||
21 | 'select' => array(), |
||
22 | 'from' => array(), |
||
23 | 'where' => array(), |
||
24 | 'groupBy' => array(), |
||
25 | 'groupByLimit' => null, |
||
26 | 'withinGroupOrderBy' => array(), |
||
27 | 'orderBy' => array(), |
||
28 | 'facet' => array(), |
||
29 | 'resultSetNames' => array(0), |
||
30 | 'set' => array(), |
||
31 | 'values' => array(), |
||
32 | 'options' => array(), |
||
33 | 'firstResult' => 0, |
||
34 | 'maxResults' => null, |
||
35 | ); |
||
36 | |||
37 | private static $multipleParts = array( |
||
38 | 'select' => true, |
||
39 | 'from' => true, |
||
40 | 'where' => true, |
||
41 | 'groupBy' => true, |
||
42 | 'groupByLimit' => false, |
||
43 | 'withinGroupOrderBy' => true, |
||
44 | 'orderBy' => true, |
||
45 | 'facet' => true, |
||
46 | 'resultSetNames' => true, |
||
47 | 'set' => true, |
||
48 | 'values' => true, |
||
49 | 'options' => true, |
||
50 | 'firstResult' => false, |
||
51 | 'maxResults' => false, |
||
52 | ); |
||
53 | |||
54 | private $isDirty = true; |
||
55 | |||
56 | private $sql; |
||
57 | |||
58 | private $parameters = array(); |
||
59 | |||
60 | private $parametersCounter = 0; |
||
61 | |||
62 | 81 | public function __construct(Connection $conn) |
|
66 | |||
67 | public function getEscaper() |
||
71 | |||
72 | 60 | public function select($select = null) |
|
81 | |||
82 | 9 | public function addSelect($select) |
|
88 | |||
89 | 6 | public function update($index) |
|
95 | |||
96 | 6 | public function insert($index) |
|
102 | |||
103 | 3 | public function replace($index) |
|
109 | |||
110 | 3 | public function delete($index) |
|
116 | |||
117 | 6 | public function set($key, $value) |
|
121 | |||
122 | 9 | public function values(array $values) |
|
126 | |||
127 | 3 | public function addValues(array $values) |
|
131 | |||
132 | 60 | public function from($index) |
|
136 | |||
137 | 3 | public function addFrom($index) |
|
141 | |||
142 | 39 | public function where($where) |
|
146 | |||
147 | 9 | public function andWhere($where) |
|
151 | |||
152 | 15 | public function groupBy($groupBy, $limit = null) |
|
158 | |||
159 | 3 | public function addGroupBy($groupBy) |
|
163 | |||
164 | 6 | public function withinGroupOrderBy($order, $direction = null) |
|
168 | |||
169 | 3 | public function addWithinGroupOrderBy($order, $direction = null) |
|
173 | |||
174 | /** |
||
175 | * @param string|array $facet 'column1', or array('column1', 'column1') or array('column1' => 'column_alias', 'column2') |
||
176 | * @param string $by |
||
177 | * @param string $order |
||
178 | * @param string $direction |
||
179 | * @param int $limit |
||
180 | * @param int $skip |
||
181 | * |
||
182 | * @return $this |
||
183 | */ |
||
184 | 3 | public function facet($facet, $by = null, $order = null, $direction = null, $limit = null, $skip = 0) |
|
190 | |||
191 | public function nameResultSet($name) |
||
195 | |||
196 | 9 | public function orderBy($order, $direction = null) |
|
200 | |||
201 | 6 | public function addOrderBy($order, $direction = null) |
|
205 | |||
206 | 6 | public function setOption($name, $value) |
|
210 | |||
211 | 6 | public function setMaxResults($limit) |
|
215 | |||
216 | 3 | public function setFirstResult($skip) |
|
220 | |||
221 | public function merge(self $qb) |
||
226 | |||
227 | 9 | public function setParameter($parameter, $value) |
|
233 | |||
234 | /** |
||
235 | * Creates a new named parameter and bind the value $value to it. |
||
236 | * |
||
237 | * @param string $value |
||
238 | * @param string $prefix the name to bind with |
||
239 | * |
||
240 | * @return string the placeholder name used |
||
241 | */ |
||
242 | 6 | public function createParameter($value, $prefix = 'gen_') |
|
250 | |||
251 | 9 | public function getParameters() |
|
255 | |||
256 | public function execute() |
||
260 | |||
261 | public function getResult() |
||
265 | |||
266 | public function getMultiResult() |
||
270 | |||
271 | 78 | public function getSql() |
|
300 | |||
301 | /** |
||
302 | * Either appends to or replaces a single, generic query part. |
||
303 | * |
||
304 | * @param string $sqlPartName |
||
305 | * @param string|array $sqlPart |
||
306 | * @param bool $append |
||
307 | * |
||
308 | * @return $this this QueryBuilder instance |
||
309 | */ |
||
310 | 78 | protected function add($sqlPartName, $sqlPart, $append = false) |
|
326 | |||
327 | 60 | protected function buildSqlForSelect() |
|
360 | |||
361 | 9 | protected function buildSqlForInsert() |
|
378 | |||
379 | 6 | protected function buildSqlForUpdate() |
|
391 | |||
392 | 3 | protected function buildSqlForDelete() |
|
399 | |||
400 | 69 | protected function buildWherePart() |
|
408 | |||
409 | 60 | protected function buildGroupByPart() |
|
429 | |||
430 | 60 | protected function buildOrderByPart() |
|
443 | |||
444 | 60 | protected function buildOptionsPart() |
|
457 | |||
458 | /** |
||
459 | * Build FACET {expr_list} [BY {expr_list}] [ORDER BY {expr | FACET()} {ASC | DESC}] [LIMIT [offset,] count]. |
||
460 | * |
||
461 | * @return string |
||
462 | */ |
||
463 | 60 | protected function buildFacetPart() |
|
495 | |||
496 | 18 | protected function getDirection($order, $direction) |
|
508 | } |
||
509 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.