Complex classes like WhereStatement 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 WhereStatement, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class WhereStatement extends Statement |
||
16 | { |
||
17 | /** |
||
18 | * @var bool |
||
19 | */ |
||
20 | protected $having; |
||
21 | |||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $conditions = []; |
||
26 | |||
27 | /** |
||
28 | * @param bool $having when true, statement becomes a having statement |
||
29 | */ |
||
30 | public function __construct($having = false) |
||
34 | |||
35 | /** |
||
36 | * Tells whether this statement is a HAVING statement. |
||
37 | * |
||
38 | * @return bool true: is HAVING, false: is WHERE |
||
39 | */ |
||
40 | public function isHaving() |
||
44 | |||
45 | /** |
||
46 | * Adds a condition to the statement. |
||
47 | * |
||
48 | * Accepts the following forms: |
||
49 | * 1. Equality comparison: |
||
50 | * addCondition('username', 'john') |
||
51 | * 2. Comparison with custom operator: |
||
52 | * addCondition('balance', 100, '>') |
||
53 | * 3. IN statement: |
||
54 | * addCondition('group', ['admin', 'owner']) |
||
55 | * 4. SQL fragment: |
||
56 | * addCondition('name LIKE "%john%"') |
||
57 | * 5. List of conditions to add: |
||
58 | * addCondition([['balance', 100, '>'], |
||
59 | * ['user_id', 5]]) |
||
60 | * 6. Map of equality comparisons: |
||
61 | * addCondition(['username' => 'john', |
||
62 | * 'user_id' => 5]) |
||
63 | * 7. List of SQL fragments: |
||
64 | * addCondition(['first_name LIKE "%john%"', |
||
65 | * 'last_name LIKE "%doe%"']) |
||
66 | * |
||
67 | * @param array|string $field |
||
68 | * @param string|bool $value condition value (optional) |
||
69 | * @param string $operator operator (optional) |
||
70 | * |
||
71 | * @return self |
||
72 | */ |
||
73 | public function addCondition($field, $value = false, $operator = '=') |
||
112 | |||
113 | public function addConditionOr($field, $value = false, $operator = '=') |
||
119 | |||
120 | /** |
||
121 | * Adds a between condition to the query. |
||
122 | * |
||
123 | * @param string $field |
||
124 | * @param mixed $a first between value |
||
125 | * @param mixed $b second between value |
||
126 | * |
||
127 | * @return self |
||
128 | */ |
||
129 | public function addBetweenCondition($field, $a, $b) |
||
135 | |||
136 | /** |
||
137 | * Adds a not between condition to the query. |
||
138 | * |
||
139 | * @param string $field |
||
140 | * @param mixed $a first between value |
||
141 | * @param mixed $b second between value |
||
142 | * |
||
143 | * @return self |
||
144 | */ |
||
145 | public function addNotBetweenCondition($field, $a, $b) |
||
151 | |||
152 | /** |
||
153 | * Adds an exists condition to the query. |
||
154 | * |
||
155 | * @param callable $f |
||
156 | * |
||
157 | * @return self |
||
158 | */ |
||
159 | public function addExistsCondition(callable $f) |
||
165 | |||
166 | /** |
||
167 | * Adds a not exists condition to the query. |
||
168 | * |
||
169 | * @param callable $f |
||
170 | * |
||
171 | * @return self |
||
172 | */ |
||
173 | public function addNotExistsCondition(callable $f) |
||
179 | |||
180 | /** |
||
181 | * Gets the conditions for this statement. |
||
182 | * |
||
183 | * @return array |
||
184 | */ |
||
185 | public function getConditions() |
||
189 | |||
190 | public function build() |
||
212 | |||
213 | /** |
||
214 | * Builds a parameterized and escaped SQL fragment |
||
215 | * for a condition that uses our own internal |
||
216 | * representation. |
||
217 | * |
||
218 | * A condition is represented by an array, and can be |
||
219 | * have one of the following forms: |
||
220 | * i) ['SQL fragment'] |
||
221 | * ii) ['identifier', '=', 'value'] |
||
222 | * iii) ['identifier', 'BETWEEN', 'value', 'value'] |
||
223 | * iv) ['EXISTS', function(SelectQuery $query) {}] |
||
224 | * |
||
225 | * @param array $cond |
||
226 | * |
||
227 | * @return string generated SQL fragment |
||
228 | */ |
||
229 | protected function buildClause(array $cond) |
||
281 | |||
282 | protected function implodeClauses(array $clauses) |
||
307 | } |
||
308 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.