Complex classes like WhereTrait 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 WhereTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | trait WhereTrait |
||
32 | { |
||
33 | /** |
||
34 | * {@inheritDoc} |
||
35 | */ |
||
36 | public function where( |
||
43 | |||
44 | /** |
||
45 | * {@inheritDoc} |
||
46 | */ |
||
47 | public function whereTpl(/*# string */ $template, $col) |
||
53 | |||
54 | /** |
||
55 | * {@inheritDoc} |
||
56 | */ |
||
57 | public function orWhereTpl(/*# string */ $template, $col) |
||
63 | |||
64 | /** |
||
65 | * {@inheritDoc} |
||
66 | */ |
||
67 | public function whereRaw(/*# string */ $rawString) |
||
72 | |||
73 | /** |
||
74 | * {@inheritDoc} |
||
75 | */ |
||
76 | public function orWhereRaw(/*# string */ $rawString) |
||
81 | |||
82 | /** |
||
83 | * {@inheritDoc} |
||
84 | */ |
||
85 | public function andWhere( |
||
92 | |||
93 | /** |
||
94 | * {@inheritDoc} |
||
95 | */ |
||
96 | public function orWhere( |
||
103 | |||
104 | /** |
||
105 | * {@inheritDoc} |
||
106 | */ |
||
107 | public function whereNot( |
||
114 | |||
115 | /** |
||
116 | * {@inheritDoc} |
||
117 | */ |
||
118 | public function orWhereNot( |
||
125 | |||
126 | /** |
||
127 | * {@inheritDoc} |
||
128 | */ |
||
129 | public function whereIn(/*# string */ $col, $value) |
||
133 | |||
134 | /** |
||
135 | * {@inheritDoc} |
||
136 | */ |
||
137 | public function orWhereIn(/*# string */ $col, $value) |
||
141 | |||
142 | /** |
||
143 | * {@inheritDoc} |
||
144 | */ |
||
145 | public function whereNotIn(/*# string */ $col, $value) |
||
149 | |||
150 | /** |
||
151 | * {@inheritDoc} |
||
152 | */ |
||
153 | public function orWhereNotIn(/*# string */ $col, $value) |
||
157 | |||
158 | /** |
||
159 | * {@inheritDoc} |
||
160 | */ |
||
161 | public function whereBetween(/*# string */ $col, $value1, $value2) |
||
166 | |||
167 | /** |
||
168 | * {@inheritDoc} |
||
169 | */ |
||
170 | public function orWhereBetween(/*# string */ $col, $value1, $value2) |
||
175 | |||
176 | /** |
||
177 | * {@inheritDoc} |
||
178 | */ |
||
179 | public function whereNotBetween(/*# string */ $col, $value1, $value2) |
||
184 | |||
185 | /** |
||
186 | * {@inheritDoc} |
||
187 | */ |
||
188 | public function orWhereNotBetween(/*# string */ $col, $value1, $value2) |
||
193 | |||
194 | /** |
||
195 | * {@inheritDoc} |
||
196 | */ |
||
197 | public function whereNull(/*# string */ $col) |
||
201 | |||
202 | /** |
||
203 | * {@inheritDoc} |
||
204 | */ |
||
205 | public function orWhereNull(/*# string */ $col) |
||
209 | |||
210 | /** |
||
211 | * {@inheritDoc} |
||
212 | */ |
||
213 | public function whereNotNull(/*# string */ $col) |
||
217 | |||
218 | /** |
||
219 | * {@inheritDoc} |
||
220 | */ |
||
221 | public function orWhereNotNull(/*# string */ $col) |
||
225 | |||
226 | /** |
||
227 | * WHERE EXISTS |
||
228 | * |
||
229 | * ```php |
||
230 | * // WHERE EXISTS (SELECT `user_id` FROM `users`) |
||
231 | * ->whereExists($users->select('user_id')) |
||
232 | * ``` |
||
233 | * |
||
234 | * @param SelectStatementInterface $sel |
||
235 | * @return $this |
||
236 | * @see WhereInterface::where() |
||
237 | * @access public |
||
238 | * @api |
||
239 | */ |
||
240 | public function whereExists(SelectStatementInterface $sel) |
||
244 | |||
245 | /** |
||
246 | * {@inheritDoc} |
||
247 | */ |
||
248 | public function orWhereExists(SelectStatementInterface $sel) |
||
252 | |||
253 | /** |
||
254 | * {@inheritDoc} |
||
255 | */ |
||
256 | public function whereNotExists(SelectStatementInterface $sel) |
||
260 | |||
261 | /** |
||
262 | * {@inheritDoc} |
||
263 | */ |
||
264 | public function orWhereNotExists(SelectStatementInterface $sel) |
||
268 | |||
269 | /** |
||
270 | * @param string|array $col col or cols |
||
271 | * @param mixed $operator |
||
272 | * @param mixed $value |
||
273 | * @param bool $logicAnd 'AND' |
||
274 | * @param bool $whereNot 'WHERE NOT' |
||
275 | * @param bool $rawMode |
||
276 | * @param string $clause 'where' or 'having' |
||
277 | * @return $this |
||
278 | * @access protected |
||
279 | */ |
||
280 | protected function realWhere( |
||
303 | |||
304 | /** |
||
305 | * @param array $cols |
||
306 | * @param bool $logicAnd |
||
307 | * @param bool $whereNot |
||
308 | * @param bool $rawMode |
||
309 | * @access protected |
||
310 | */ |
||
311 | protected function multipleWhere( |
||
327 | |||
328 | /** |
||
329 | * Build WHERE |
||
330 | * |
||
331 | * @param string $clause 'where|having' |
||
332 | * @return array |
||
333 | * @access protected |
||
334 | */ |
||
335 | protected function buildWhere( |
||
372 | |||
373 | abstract protected function processValue($value)/*# : string */; |
||
383 | } |
||
384 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.