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 qb 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 qb, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class qb |
||
9 | { |
||
10 | private $_conditions = array(); |
||
11 | private $_table_names = array(); |
||
12 | private $_read_fields = array(); |
||
13 | private $_write_fields = array(); |
||
14 | private $_write_values = array(); |
||
15 | private $_write_field_types = array(); |
||
16 | private $_order_fields = array(); |
||
17 | private $_group_fields = array(); |
||
18 | public $limit = 100; |
||
19 | public $limit_offset = -1; |
||
20 | public static $default_limit = 100; |
||
21 | |||
22 | public function __construct($table_name = null) |
||
29 | |||
30 | public function resetTables() |
||
36 | |||
37 | public function setTable($table_name) |
||
44 | |||
45 | public function table($table_name) |
||
51 | |||
52 | public function resetWrite() |
||
60 | |||
61 | /** |
||
62 | * $type : |
||
63 | * - 0 = string |
||
64 | * - 1 = integer |
||
65 | * - 2 = raw. |
||
66 | */ |
||
67 | public function set($field, $value, $type = 0) |
||
75 | |||
76 | public function resetConditions() |
||
82 | |||
83 | public function and($field, $value = null, $operator = '=') |
||
89 | |||
90 | /** |
||
91 | * Adds "<strong><em>or</em></strong>" condition to current and condition |
||
92 | * array. |
||
93 | */ |
||
94 | public function or() |
||
100 | |||
101 | public static function c($field, $value = null, $operator = '=') |
||
109 | |||
110 | public function resetReadFields() |
||
116 | |||
117 | public function select($field) |
||
123 | |||
124 | public function groupBy($field) |
||
130 | |||
131 | public function orderBy($field, $asc = true) |
||
137 | |||
138 | public function getSelect() |
||
179 | |||
180 | public function getUpdate() |
||
204 | |||
205 | public function getInsert() |
||
223 | |||
224 | public function getDelete() |
||
236 | } |
||
237 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.