Total Complexity | 45 |
Total Lines | 375 |
Duplicated Lines | 0 % |
Changes | 0 |
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 |
||
52 | class WhereQuery extends Query { |
||
53 | |||
54 | /** |
||
55 | * @var array $available_criteria |
||
56 | */ |
||
57 | protected $available_criteria = [ |
||
58 | 'OR', 'AND', |
||
59 | 'ALL', 'ANSWERED', 'BCC', 'BEFORE', 'BODY', 'CC', 'DELETED', 'FLAGGED', 'FROM', 'KEYWORD', |
||
60 | 'NEW', 'NOT', 'OLD', 'ON', 'RECENT', 'SEEN', 'SINCE', 'SUBJECT', 'TEXT', 'TO', |
||
61 | 'UNANSWERED', 'UNDELETED', 'UNFLAGGED', 'UNKEYWORD', 'UNSEEN' |
||
62 | ]; |
||
63 | |||
64 | /** |
||
65 | * Magic method in order to allow alias usage of all "where" methods in an optional connection with "NOT" |
||
66 | * @param string $name |
||
67 | * @param array|null $arguments |
||
68 | * |
||
69 | * @return mixed |
||
70 | * @throws InvalidWhereQueryCriteriaException |
||
71 | * @throws MethodNotFoundException |
||
72 | */ |
||
73 | public function __call($name, $arguments) { |
||
74 | $that = $this; |
||
75 | |||
76 | $name = camel_case($name); |
||
|
|||
77 | |||
78 | if(strtolower(substr($name, 0, 3)) === 'not') { |
||
79 | $that = $that->whereNot(); |
||
80 | $name = substr($name, 3); |
||
81 | } |
||
82 | |||
83 | $method = 'where'.ucfirst($name); |
||
84 | if(method_exists($this, $method) === true){ |
||
85 | return call_user_func_array([$that, $method], $arguments); |
||
86 | } |
||
87 | |||
88 | throw new MethodNotFoundException("Method ".self::class.'::'.$method.'() is not supported'); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Validate a given criteria |
||
93 | * @param $criteria |
||
94 | * |
||
95 | * @return string |
||
96 | * @throws InvalidWhereQueryCriteriaException |
||
97 | */ |
||
98 | protected function validate_criteria($criteria) { |
||
99 | $criteria = strtoupper($criteria); |
||
100 | |||
101 | if(in_array($criteria, $this->available_criteria) === false) { |
||
102 | throw new InvalidWhereQueryCriteriaException(); |
||
103 | } |
||
104 | |||
105 | return $criteria; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @param mixed $criteria |
||
110 | * @param null $value |
||
111 | * |
||
112 | * @return $this |
||
113 | * @throws InvalidWhereQueryCriteriaException |
||
114 | */ |
||
115 | public function where($criteria, $value = null) { |
||
116 | if(is_array($criteria)){ |
||
117 | foreach($criteria as $arguments){ |
||
118 | if(count($arguments) == 1){ |
||
119 | $this->where($arguments[0]); |
||
120 | }elseif(count($arguments) == 2){ |
||
121 | $this->where($arguments[0], $arguments[1]); |
||
122 | } |
||
123 | } |
||
124 | }else{ |
||
125 | $criteria = $this->validate_criteria($criteria); |
||
126 | $value = $this->parse_value($value); |
||
127 | |||
128 | if($value === null || $value === ''){ |
||
129 | $this->query->push([$criteria]); |
||
130 | }else{ |
||
131 | $this->query->push([$criteria, $value]); |
||
132 | } |
||
133 | } |
||
134 | |||
135 | return $this; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @param \Closure $closure |
||
140 | * |
||
141 | * @return $this |
||
142 | */ |
||
143 | public function orWhere(\Closure $closure = null) { |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @param \Closure $closure |
||
152 | * |
||
153 | * @return $this |
||
154 | */ |
||
155 | public function andWhere(\Closure $closure = null) { |
||
156 | $this->query->push(['AND']); |
||
157 | if($closure !== null) $closure($this); |
||
158 | |||
159 | return $this; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * @return WhereQuery |
||
164 | * @throws InvalidWhereQueryCriteriaException |
||
165 | */ |
||
166 | public function whereAll() { |
||
167 | return $this->where('ALL'); |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @return WhereQuery |
||
172 | * @throws InvalidWhereQueryCriteriaException |
||
173 | */ |
||
174 | public function whereAnswered() { |
||
175 | return $this->where('ANSWERED'); |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @param string $value |
||
180 | * |
||
181 | * @return WhereQuery |
||
182 | * @throws InvalidWhereQueryCriteriaException |
||
183 | */ |
||
184 | public function whereBcc($value) { |
||
185 | return $this->where('BCC', $value); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * @param mixed $value |
||
190 | * @return WhereQuery |
||
191 | * @throws InvalidWhereQueryCriteriaException |
||
192 | * @throws MessageSearchValidationException |
||
193 | */ |
||
194 | public function whereBefore($value) { |
||
195 | $date = $this->parse_date($value); |
||
196 | return $this->where('BEFORE', $date); |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * @param string $value |
||
201 | * |
||
202 | * @return WhereQuery |
||
203 | * @throws InvalidWhereQueryCriteriaException |
||
204 | */ |
||
205 | public function whereBody($value) { |
||
206 | return $this->where('BODY', $value); |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * @param string $value |
||
211 | * |
||
212 | * @return WhereQuery |
||
213 | * @throws InvalidWhereQueryCriteriaException |
||
214 | */ |
||
215 | public function whereCc($value) { |
||
216 | return $this->where('CC', $value); |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * @return WhereQuery |
||
221 | * @throws InvalidWhereQueryCriteriaException |
||
222 | */ |
||
223 | public function whereDeleted() { |
||
224 | return $this->where('DELETED'); |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * @param string $value |
||
229 | * |
||
230 | * @return WhereQuery |
||
231 | * @throws InvalidWhereQueryCriteriaException |
||
232 | */ |
||
233 | public function whereFlagged($value) { |
||
234 | return $this->where('FLAGGED', $value); |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * @param string $value |
||
239 | * |
||
240 | * @return WhereQuery |
||
241 | * @throws InvalidWhereQueryCriteriaException |
||
242 | */ |
||
243 | public function whereFrom($value) { |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * @param string $value |
||
249 | * |
||
250 | * @return WhereQuery |
||
251 | * @throws InvalidWhereQueryCriteriaException |
||
252 | */ |
||
253 | public function whereKeyword($value) { |
||
254 | return $this->where('KEYWORD', $value); |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * @return WhereQuery |
||
259 | * @throws InvalidWhereQueryCriteriaException |
||
260 | */ |
||
261 | public function whereNew() { |
||
262 | return $this->where('NEW'); |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * @return WhereQuery |
||
267 | * @throws InvalidWhereQueryCriteriaException |
||
268 | */ |
||
269 | public function whereNot() { |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * @return WhereQuery |
||
275 | * @throws InvalidWhereQueryCriteriaException |
||
276 | */ |
||
277 | public function whereOld() { |
||
278 | return $this->where('OLD'); |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * @param mixed $value |
||
283 | * |
||
284 | * @return WhereQuery |
||
285 | * @throws MessageSearchValidationException |
||
286 | * @throws InvalidWhereQueryCriteriaException |
||
287 | */ |
||
288 | public function whereOn($value) { |
||
289 | $date = $this->parse_date($value); |
||
290 | return $this->where('ON', $date); |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * @return WhereQuery |
||
295 | * @throws InvalidWhereQueryCriteriaException |
||
296 | */ |
||
297 | public function whereRecent() { |
||
298 | return $this->where('RECENT'); |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * @return WhereQuery |
||
303 | * @throws InvalidWhereQueryCriteriaException |
||
304 | */ |
||
305 | public function whereSeen() { |
||
306 | return $this->where('SEEN'); |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * @param mixed $value |
||
311 | * |
||
312 | * @return WhereQuery |
||
313 | * @throws MessageSearchValidationException |
||
314 | * @throws InvalidWhereQueryCriteriaException |
||
315 | */ |
||
316 | public function whereSince($value) { |
||
317 | $date = $this->parse_date($value); |
||
318 | return $this->where('SINCE', $date); |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * @param string $value |
||
323 | * |
||
324 | * @return WhereQuery |
||
325 | * @throws InvalidWhereQueryCriteriaException |
||
326 | */ |
||
327 | public function whereSubject($value) { |
||
328 | return $this->where('SUBJECT', $value); |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * @param string $value |
||
333 | * |
||
334 | * @return WhereQuery |
||
335 | * @throws InvalidWhereQueryCriteriaException |
||
336 | */ |
||
337 | public function whereText($value) { |
||
338 | return $this->where('TEXT', $value); |
||
339 | } |
||
340 | |||
341 | /** |
||
342 | * @param string $value |
||
343 | * |
||
344 | * @return WhereQuery |
||
345 | * @throws InvalidWhereQueryCriteriaException |
||
346 | */ |
||
347 | public function whereTo($value) { |
||
348 | return $this->where('TO', $value); |
||
349 | } |
||
350 | |||
351 | /** |
||
352 | * @param string $value |
||
353 | * |
||
354 | * @return WhereQuery |
||
355 | * @throws InvalidWhereQueryCriteriaException |
||
356 | */ |
||
357 | public function whereUnkeyword($value) { |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * @return WhereQuery |
||
363 | * @throws InvalidWhereQueryCriteriaException |
||
364 | */ |
||
365 | public function whereUnanswered() { |
||
367 | } |
||
368 | |||
369 | /** |
||
370 | * @return WhereQuery |
||
371 | * @throws InvalidWhereQueryCriteriaException |
||
372 | */ |
||
373 | public function whereUndeleted() { |
||
375 | } |
||
376 | |||
377 | /** |
||
378 | * @return WhereQuery |
||
379 | * @throws InvalidWhereQueryCriteriaException |
||
380 | */ |
||
381 | public function whereUnflagged() { |
||
382 | return $this->where('UNFLAGGED'); |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * @return WhereQuery |
||
387 | * @throws InvalidWhereQueryCriteriaException |
||
388 | */ |
||
389 | public function whereUnseen() { |
||
390 | return $this->where('UNSEEN'); |
||
391 | } |
||
392 | |||
393 | /** |
||
394 | * @param $msg_id |
||
395 | * |
||
396 | * @return WhereQuery |
||
397 | * @throws InvalidWhereQueryCriteriaException |
||
398 | */ |
||
399 | public function whereMessageId($msg_id) { |
||
400 | return $this->where("Message-ID <$msg_id>"); |
||
401 | } |
||
402 | |||
403 | /** |
||
404 | * @return WhereQuery |
||
405 | * @throws InvalidWhereQueryCriteriaException |
||
406 | */ |
||
407 | public function whereNoXSpam(){ |
||
409 | } |
||
410 | |||
411 | /** |
||
412 | * @return WhereQuery |
||
413 | * @throws InvalidWhereQueryCriteriaException |
||
414 | */ |
||
415 | public function whereIsXSpam(){ |
||
416 | return $this->where("X-Spam-Flag YES"); |
||
417 | } |
||
418 | |||
419 | /** |
||
420 | * @param $country_code |
||
421 | * |
||
422 | * @return WhereQuery |
||
423 | * @throws InvalidWhereQueryCriteriaException |
||
424 | */ |
||
425 | public function whereLanguage($country_code){ |
||
427 | } |
||
428 | } |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.