Total Complexity | 44 |
Total Lines | 367 |
Duplicated Lines | 0 % |
Changes | 10 | ||
Bugs | 1 | Features | 6 |
Complex classes like WhereQuery 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.
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 WhereQuery, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
53 | class WhereQuery extends Query { |
||
54 | |||
55 | /** |
||
56 | * @var array $available_criteria |
||
57 | */ |
||
58 | protected $available_criteria = [ |
||
59 | 'OR', 'AND', |
||
60 | 'ALL', 'ANSWERED', 'BCC', 'BEFORE', 'BODY', 'CC', 'DELETED', 'FLAGGED', 'FROM', 'KEYWORD', |
||
61 | 'NEW', 'NOT', 'OLD', 'ON', 'RECENT', 'SEEN', 'SINCE', 'SUBJECT', 'TEXT', 'TO', |
||
62 | 'UNANSWERED', 'UNDELETED', 'UNFLAGGED', 'UNKEYWORD', 'UNSEEN' |
||
63 | ]; |
||
64 | |||
65 | /** |
||
66 | * Magic method in order to allow alias usage of all "where" methods in an optional connection with "NOT" |
||
67 | * @param string $name |
||
68 | * @param array|null $arguments |
||
69 | * |
||
70 | * @return mixed |
||
71 | * @throws InvalidWhereQueryCriteriaException |
||
72 | * @throws MethodNotFoundException |
||
73 | */ |
||
74 | public function __call($name, $arguments) { |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Validate a given criteria |
||
94 | * @param $criteria |
||
95 | * |
||
96 | * @return string |
||
97 | * @throws InvalidWhereQueryCriteriaException |
||
98 | */ |
||
99 | protected function validate_criteria($criteria) { |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * @param mixed $criteria |
||
114 | * @param null $value |
||
115 | * |
||
116 | * @return $this |
||
117 | * @throws InvalidWhereQueryCriteriaException |
||
118 | */ |
||
119 | public function where($criteria, $value = null) { |
||
120 | if(is_array($criteria)){ |
||
121 | foreach($criteria as $key => $value){ |
||
122 | if(is_numeric($key)){ |
||
123 | return $this->where($value); |
||
124 | } |
||
125 | return $this->where($key, $value); |
||
126 | } |
||
127 | }else{ |
||
128 | $criteria = $this->validate_criteria($criteria); |
||
129 | $value = $this->parse_value($value); |
||
130 | |||
131 | if($value === null || $value === ''){ |
||
132 | $this->query->push([$criteria]); |
||
133 | }else{ |
||
134 | $this->query->push([$criteria, $value]); |
||
135 | } |
||
136 | } |
||
137 | |||
138 | return $this; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * @param \Closure $closure |
||
143 | * |
||
144 | * @return $this |
||
145 | */ |
||
146 | public function orWhere(\Closure $closure = null) { |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @param \Closure $closure |
||
155 | * |
||
156 | * @return $this |
||
157 | */ |
||
158 | public function andWhere(\Closure $closure = null) { |
||
159 | $this->query->push(['AND']); |
||
160 | if($closure !== null) $closure($this); |
||
161 | |||
162 | return $this; |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * @return WhereQuery |
||
167 | * @throws InvalidWhereQueryCriteriaException |
||
168 | */ |
||
169 | public function whereAll() { |
||
170 | return $this->where('ALL'); |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * @return WhereQuery |
||
175 | * @throws InvalidWhereQueryCriteriaException |
||
176 | */ |
||
177 | public function whereAnswered() { |
||
178 | return $this->where('ANSWERED'); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * @param string $value |
||
183 | * |
||
184 | * @return WhereQuery |
||
185 | * @throws InvalidWhereQueryCriteriaException |
||
186 | */ |
||
187 | public function whereBcc($value) { |
||
188 | return $this->where('BCC', $value); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @param mixed $value |
||
193 | * @return WhereQuery |
||
194 | * @throws InvalidWhereQueryCriteriaException |
||
195 | * @throws MessageSearchValidationException |
||
196 | */ |
||
197 | public function whereBefore($value) { |
||
198 | $date = $this->parse_date($value); |
||
199 | return $this->where('BEFORE', $date); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * @param string $value |
||
204 | * |
||
205 | * @return WhereQuery |
||
206 | * @throws InvalidWhereQueryCriteriaException |
||
207 | */ |
||
208 | public function whereBody($value) { |
||
209 | return $this->where('BODY', $value); |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * @param string $value |
||
214 | * |
||
215 | * @return WhereQuery |
||
216 | * @throws InvalidWhereQueryCriteriaException |
||
217 | */ |
||
218 | public function whereCc($value) { |
||
219 | return $this->where('CC', $value); |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * @return WhereQuery |
||
224 | * @throws InvalidWhereQueryCriteriaException |
||
225 | */ |
||
226 | public function whereDeleted() { |
||
227 | return $this->where('DELETED'); |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * @param string $value |
||
232 | * |
||
233 | * @return WhereQuery |
||
234 | * @throws InvalidWhereQueryCriteriaException |
||
235 | */ |
||
236 | public function whereFlagged($value) { |
||
237 | return $this->where('FLAGGED', $value); |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * @param string $value |
||
242 | * |
||
243 | * @return WhereQuery |
||
244 | * @throws InvalidWhereQueryCriteriaException |
||
245 | */ |
||
246 | public function whereFrom($value) { |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * @param string $value |
||
252 | * |
||
253 | * @return WhereQuery |
||
254 | * @throws InvalidWhereQueryCriteriaException |
||
255 | */ |
||
256 | public function whereKeyword($value) { |
||
257 | return $this->where('KEYWORD', $value); |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * @return WhereQuery |
||
262 | * @throws InvalidWhereQueryCriteriaException |
||
263 | */ |
||
264 | public function whereNew() { |
||
265 | return $this->where('NEW'); |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * @return WhereQuery |
||
270 | * @throws InvalidWhereQueryCriteriaException |
||
271 | */ |
||
272 | public function whereNot() { |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * @return WhereQuery |
||
278 | * @throws InvalidWhereQueryCriteriaException |
||
279 | */ |
||
280 | public function whereOld() { |
||
281 | return $this->where('OLD'); |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * @param mixed $value |
||
286 | * |
||
287 | * @return WhereQuery |
||
288 | * @throws MessageSearchValidationException |
||
289 | * @throws InvalidWhereQueryCriteriaException |
||
290 | */ |
||
291 | public function whereOn($value) { |
||
292 | $date = $this->parse_date($value); |
||
293 | return $this->where('ON', $date); |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * @return WhereQuery |
||
298 | * @throws InvalidWhereQueryCriteriaException |
||
299 | */ |
||
300 | public function whereRecent() { |
||
301 | return $this->where('RECENT'); |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * @return WhereQuery |
||
306 | * @throws InvalidWhereQueryCriteriaException |
||
307 | */ |
||
308 | public function whereSeen() { |
||
309 | return $this->where('SEEN'); |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * @param mixed $value |
||
314 | * |
||
315 | * @return WhereQuery |
||
316 | * @throws MessageSearchValidationException |
||
317 | * @throws InvalidWhereQueryCriteriaException |
||
318 | */ |
||
319 | public function whereSince($value) { |
||
320 | $date = $this->parse_date($value); |
||
321 | return $this->where('SINCE', $date); |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * @param string $value |
||
326 | * |
||
327 | * @return WhereQuery |
||
328 | * @throws InvalidWhereQueryCriteriaException |
||
329 | */ |
||
330 | public function whereSubject($value) { |
||
331 | return $this->where('SUBJECT', $value); |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * @param string $value |
||
336 | * |
||
337 | * @return WhereQuery |
||
338 | * @throws InvalidWhereQueryCriteriaException |
||
339 | */ |
||
340 | public function whereText($value) { |
||
341 | return $this->where('TEXT', $value); |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * @param string $value |
||
346 | * |
||
347 | * @return WhereQuery |
||
348 | * @throws InvalidWhereQueryCriteriaException |
||
349 | */ |
||
350 | public function whereTo($value) { |
||
351 | return $this->where('TO', $value); |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * @param string $value |
||
356 | * |
||
357 | * @return WhereQuery |
||
358 | * @throws InvalidWhereQueryCriteriaException |
||
359 | */ |
||
360 | public function whereUnkeyword($value) { |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * @return WhereQuery |
||
366 | * @throws InvalidWhereQueryCriteriaException |
||
367 | */ |
||
368 | public function whereUnanswered() { |
||
370 | } |
||
371 | |||
372 | /** |
||
373 | * @return WhereQuery |
||
374 | * @throws InvalidWhereQueryCriteriaException |
||
375 | */ |
||
376 | public function whereUndeleted() { |
||
378 | } |
||
379 | |||
380 | /** |
||
381 | * @return WhereQuery |
||
382 | * @throws InvalidWhereQueryCriteriaException |
||
383 | */ |
||
384 | public function whereUnflagged() { |
||
385 | return $this->where('UNFLAGGED'); |
||
386 | } |
||
387 | |||
388 | /** |
||
389 | * @return WhereQuery |
||
390 | * @throws InvalidWhereQueryCriteriaException |
||
391 | */ |
||
392 | public function whereUnseen() { |
||
393 | return $this->where('UNSEEN'); |
||
394 | } |
||
395 | |||
396 | /** |
||
397 | * @return WhereQuery |
||
398 | * @throws InvalidWhereQueryCriteriaException |
||
399 | */ |
||
400 | public function whereNoXSpam(){ |
||
402 | } |
||
403 | |||
404 | /** |
||
405 | * @return WhereQuery |
||
406 | * @throws InvalidWhereQueryCriteriaException |
||
407 | */ |
||
408 | public function whereIsXSpam(){ |
||
409 | return $this->where("X-Spam-Flag YES"); |
||
410 | } |
||
411 | |||
412 | /** |
||
413 | * @param $country_code |
||
414 | * |
||
415 | * @return WhereQuery |
||
416 | * @throws InvalidWhereQueryCriteriaException |
||
417 | */ |
||
418 | public function whereLanguage($country_code){ |
||
420 | } |
||
421 | } |
||
422 |