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 Builder 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 Builder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Builder extends QueryBuilder |
||
12 | { |
||
13 | /** |
||
14 | * The query instance. |
||
15 | * |
||
16 | * @var Query |
||
17 | */ |
||
18 | protected $query; |
||
19 | |||
20 | /** |
||
21 | * The r\Table instance. |
||
22 | * |
||
23 | * @var r\Table |
||
24 | */ |
||
25 | protected $table; |
||
26 | |||
27 | /** |
||
28 | * All of the available clause operators. |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | public $operators = [ |
||
33 | '=', '<', '>', '<=', '>=', '<>', '!=', |
||
34 | 'like', 'not like', 'between', 'ilike', |
||
35 | '&', '|', '^', '<<', '>>', |
||
36 | 'rlike', 'regexp', 'not regexp', |
||
37 | '~', '~*', '!~', '!~*', |
||
38 | 'contains', 'exists', 'type', 'mod', 'size', |
||
39 | ]; |
||
40 | |||
41 | /** |
||
42 | * Create a new query builder instance. |
||
43 | * |
||
44 | * @param Connection $connection |
||
45 | */ |
||
46 | public function __construct(Connection $connection) |
||
53 | |||
54 | /** |
||
55 | * Set the collection which the query is targeting. |
||
56 | * |
||
57 | * @param string $table |
||
58 | * |
||
59 | * @return Builder |
||
60 | */ |
||
61 | public function from($table) |
||
70 | |||
71 | /** |
||
72 | * Execute the query as a fresh "select" statement. |
||
73 | * |
||
74 | * @param array $columns |
||
75 | * |
||
76 | * @return array|static[] |
||
77 | */ |
||
78 | public function getFresh($columns = []) |
||
109 | |||
110 | /** |
||
111 | * Run the query as a "select" statement against the connection. |
||
112 | * |
||
113 | * @return array |
||
114 | */ |
||
115 | protected function runSelect() |
||
119 | |||
120 | /** |
||
121 | * Compile orders into query. |
||
122 | */ |
||
123 | public function compileOrders() |
||
144 | |||
145 | /** |
||
146 | * Insert a new record into the database. |
||
147 | * |
||
148 | * @param array $values |
||
149 | * |
||
150 | * @return bool |
||
151 | */ |
||
152 | public function insert(array $values) |
||
159 | |||
160 | /** |
||
161 | * Insert a new record and get the value of the primary key. |
||
162 | * |
||
163 | * @param array $values |
||
164 | * @param string $sequence |
||
165 | * |
||
166 | * @return int |
||
167 | */ |
||
168 | public function insertGetId(array $values, $sequence = null) |
||
182 | |||
183 | /** |
||
184 | * Update a record in the database. |
||
185 | * |
||
186 | * @param array $values |
||
187 | * @param array $options |
||
188 | * |
||
189 | * @return int |
||
190 | */ |
||
191 | public function update(array $values, array $options = []) |
||
195 | |||
196 | /** |
||
197 | * Perform an update query. |
||
198 | * |
||
199 | * @param array $query |
||
200 | * @param array $options |
||
201 | * |
||
202 | * @return int |
||
203 | */ |
||
204 | protected function performUpdate($query, array $options = []) |
||
215 | |||
216 | /** |
||
217 | * Delete a record from the database. |
||
218 | * |
||
219 | * @param mixed $id |
||
220 | * |
||
221 | * @return int |
||
222 | */ |
||
223 | public function delete($id = null) |
||
235 | |||
236 | /** |
||
237 | * Compile the where array to filter chain. |
||
238 | * |
||
239 | * @return array |
||
240 | */ |
||
241 | protected function compileWheres() |
||
257 | |||
258 | public function buildFilter($document) |
||
264 | |||
265 | /** |
||
266 | * Run a truncate statement on the table. |
||
267 | * |
||
268 | * @return void |
||
269 | */ |
||
270 | public function truncate() |
||
276 | |||
277 | /** |
||
278 | * Append one or more values to an array. |
||
279 | * |
||
280 | * @param mixed $column |
||
281 | * @param mixed $value |
||
282 | * @param bool $unique |
||
283 | * |
||
284 | * @return bool |
||
285 | */ |
||
286 | public function push($column, $value = null, $unique = false) |
||
297 | |||
298 | /** |
||
299 | * Remove one or more values from an array. |
||
300 | * |
||
301 | * @param mixed $column |
||
302 | * @param mixed $value |
||
303 | * |
||
304 | * @return bool |
||
305 | */ |
||
306 | public function pull($column, $value = null) |
||
315 | |||
316 | /** |
||
317 | * Force the query to only return distinct results. |
||
318 | * |
||
319 | * @var string|null |
||
320 | * |
||
321 | * @return Builder |
||
322 | */ |
||
323 | public function distinct($column = null) |
||
333 | |||
334 | /** |
||
335 | * Retrieve the "count" result of the query. |
||
336 | * |
||
337 | * @param string $columns |
||
338 | * |
||
339 | * @return int |
||
340 | */ |
||
341 | public function count($columns = null) |
||
348 | |||
349 | /** |
||
350 | * Retrieve the sum of the values of a given column. |
||
351 | * |
||
352 | * @param string $column |
||
353 | * |
||
354 | * @return mixed |
||
355 | */ |
||
356 | public function sum($column) |
||
363 | |||
364 | /** |
||
365 | * Retrieve the minimum value of a given column. |
||
366 | * |
||
367 | * @param string $column |
||
368 | * |
||
369 | * @return mixed |
||
370 | */ |
||
371 | View Code Duplication | public function min($column) |
|
380 | |||
381 | /** |
||
382 | * Retrieve the maximum value of a given column. |
||
383 | * |
||
384 | * @param string $column |
||
385 | * |
||
386 | * @return mixed |
||
387 | */ |
||
388 | View Code Duplication | public function max($column) |
|
397 | |||
398 | /** |
||
399 | * Retrieve the average of the values of a given column. |
||
400 | * |
||
401 | * @param string $column |
||
402 | * |
||
403 | * @return mixed |
||
404 | */ |
||
405 | public function avg($column) |
||
413 | |||
414 | /** |
||
415 | * Remove one or more fields. |
||
416 | * |
||
417 | * @param mixed $columns |
||
418 | * |
||
419 | * @return int |
||
420 | */ |
||
421 | public function drop($columns) |
||
434 | |||
435 | /** |
||
436 | * Add a "group by" clause to the query. |
||
437 | * |
||
438 | * @param array|string $column,... |
||
439 | * |
||
440 | * @return $this |
||
441 | */ |
||
442 | public function groupBy(...$groups) |
||
452 | |||
453 | /** |
||
454 | * Add an "order by" clause to the query. |
||
455 | * |
||
456 | * @param string $column |
||
457 | * @param string $direction |
||
458 | * @param bool $index |
||
459 | * |
||
460 | * @return $this |
||
461 | */ |
||
462 | public function orderBy($column, $direction = 'asc', $index = false) |
||
470 | |||
471 | /** |
||
472 | * Add a where between statement to the query. |
||
473 | * |
||
474 | * @param string $column |
||
475 | * @param array $values |
||
476 | * @param string $boolean |
||
477 | * @param bool $not |
||
478 | * |
||
479 | * @return Builder |
||
480 | */ |
||
481 | public function whereBetween($column, array $values, $boolean = 'and', $not = false) |
||
488 | |||
489 | /** |
||
490 | * Handle dynamic method calls into the method. |
||
491 | * |
||
492 | * @param string $method |
||
493 | * @param array $parameters |
||
494 | * |
||
495 | * @return mixed |
||
496 | */ |
||
497 | public function __call($method, $parameters) |
||
505 | } |
||
506 |
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..