Total Complexity | 60 |
Total Lines | 491 |
Duplicated Lines | 0 % |
Changes | 11 | ||
Bugs | 2 | Features | 3 |
Complex classes like Query 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 Query, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class Query { |
||
30 | |||
31 | /** @var array $query */ |
||
32 | protected $query; |
||
33 | |||
34 | /** @var string $raw_query */ |
||
35 | protected $raw_query; |
||
36 | |||
37 | /** @var string $charset */ |
||
38 | protected $charset; |
||
39 | |||
40 | /** @var Client $client */ |
||
41 | protected $client; |
||
42 | |||
43 | /** @var int $limit */ |
||
44 | protected $limit = null; |
||
45 | |||
46 | /** @var int $page */ |
||
47 | protected $page = 1; |
||
48 | |||
49 | /** @var int $fetch_options */ |
||
50 | protected $fetch_options = null; |
||
51 | |||
52 | /** @var int $fetch_body */ |
||
53 | protected $fetch_body = true; |
||
54 | |||
55 | /** @var int $fetch_attachment */ |
||
56 | protected $fetch_attachment = true; |
||
57 | |||
58 | /** @var int $fetch_flags */ |
||
59 | protected $fetch_flags = true; |
||
60 | |||
61 | /** @var string $date_format */ |
||
62 | protected $date_format; |
||
63 | |||
64 | /** |
||
65 | * Query constructor. |
||
66 | * @param Client $client |
||
67 | * @param string $charset |
||
68 | */ |
||
69 | public function __construct(Client $client, $charset = 'UTF-8') { |
||
70 | $this->setClient($client); |
||
71 | |||
72 | if(config('imap.options.fetch') === IMAP::FT_PEEK) $this->leaveUnread(); |
||
73 | |||
74 | $this->date_format = config('imap.date_format', 'd M y'); |
||
75 | |||
76 | $this->charset = $charset; |
||
77 | $this->query = collect(); |
||
|
|||
78 | $this->boot(); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Instance boot method for additional functionality |
||
83 | */ |
||
84 | protected function boot(){} |
||
85 | |||
86 | /** |
||
87 | * Parse a given value |
||
88 | * @param mixed $value |
||
89 | * |
||
90 | * @return string |
||
91 | */ |
||
92 | protected function parse_value($value){ |
||
93 | switch(true){ |
||
94 | case $value instanceof \Carbon\Carbon: |
||
95 | $value = $value->format($this->date_format); |
||
96 | break; |
||
97 | } |
||
98 | |||
99 | return (string) $value; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Check if a given date is a valid carbon object and if not try to convert it |
||
104 | * @param $date |
||
105 | * |
||
106 | * @return Carbon |
||
107 | * @throws MessageSearchValidationException |
||
108 | */ |
||
109 | protected function parse_date($date) { |
||
110 | if($date instanceof \Carbon\Carbon) return $date; |
||
111 | |||
112 | try { |
||
113 | $date = Carbon::parse($date); |
||
114 | } catch (\Exception $e) { |
||
115 | throw new MessageSearchValidationException(); |
||
116 | } |
||
117 | |||
118 | return $date; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Don't mark messages as read when fetching |
||
123 | * |
||
124 | * @return $this |
||
125 | */ |
||
126 | public function leaveUnread() { |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Mark all messages as read when fetching |
||
134 | * |
||
135 | * @return $this |
||
136 | */ |
||
137 | public function markAsRead() { |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Perform an imap search request |
||
145 | * |
||
146 | * @return \Illuminate\Support\Collection |
||
147 | * @throws \Webklex\IMAP\Exceptions\ConnectionFailedException |
||
148 | */ |
||
149 | protected function search(){ |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Count all available messages matching the current search criteria |
||
186 | * |
||
187 | * @return int |
||
188 | * @throws \Webklex\IMAP\Exceptions\ConnectionFailedException |
||
189 | */ |
||
190 | public function count() { |
||
191 | return $this->search()->count(); |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Fetch the current query and return all found messages |
||
196 | * |
||
197 | * @return MessageCollection |
||
198 | * @throws GetMessagesFailedException |
||
199 | */ |
||
200 | public function get() { |
||
240 | } |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Paginate the current query |
||
245 | * @param int $per_page |
||
246 | * @param null $page |
||
247 | * @param string $page_name |
||
248 | * |
||
249 | * @return \Illuminate\Pagination\LengthAwarePaginator |
||
250 | * @throws GetMessagesFailedException |
||
251 | */ |
||
252 | public function paginate($per_page = 5, $page = null, $page_name = 'imap_page'){ |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Create an idle like instance to catch incoming messages |
||
261 | * @param callable|null $callback |
||
262 | * @param int $timeout |
||
263 | * |
||
264 | * @throws \Webklex\IMAP\Exceptions\ConnectionFailedException |
||
265 | * @throws \Webklex\IMAP\Exceptions\InvalidMessageDateException |
||
266 | */ |
||
267 | public function idle(callable $callback = null, $timeout = 10){ |
||
268 | $known_messages = []; |
||
269 | $this->getClient()->overview()->each(function($message) use(&$known_messages){ |
||
270 | /** @var object $message */ |
||
271 | $known_messages[] = $message->uid; |
||
272 | }); |
||
273 | while ($this->getClient()->isConnected()){ |
||
274 | $this->getClient()->expunge(); |
||
275 | $new_messages = []; |
||
276 | $this->getClient()->overview()->each(function($message) use(&$new_messages, &$known_messages){ |
||
277 | /** @var object $message */ |
||
278 | if (in_array($message->uid, $known_messages) == false) { |
||
279 | $new_messages[] = $message; |
||
280 | $known_messages[] = $message->uid; |
||
281 | } |
||
282 | }); |
||
283 | |||
284 | foreach($new_messages as $msg) { |
||
285 | $message = new Message($msg->uid, $msg->msgno, $this->getClient()); |
||
286 | MessageNewEvent::dispatch($message); |
||
287 | if ($callback){ |
||
288 | $callback($message); |
||
289 | } |
||
290 | } |
||
291 | |||
292 | sleep($timeout); |
||
293 | } |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * Get the raw IMAP search query |
||
298 | * |
||
299 | * @return string |
||
300 | */ |
||
301 | public function generate_query() { |
||
302 | $query = ''; |
||
303 | $this->query->each(function($statement) use(&$query) { |
||
304 | if (count($statement) == 1) { |
||
305 | $query .= $statement[0]; |
||
306 | } else { |
||
307 | if($statement[1] === null){ |
||
308 | $query .= $statement[0]; |
||
309 | }else{ |
||
310 | $query .= $statement[0].' "'.$statement[1].'"'; |
||
311 | } |
||
312 | } |
||
313 | $query .= ' '; |
||
314 | |||
315 | }); |
||
316 | |||
317 | $this->raw_query = trim($query); |
||
318 | |||
319 | return $this->raw_query; |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * @return Client |
||
324 | * @throws \Webklex\IMAP\Exceptions\ConnectionFailedException |
||
325 | */ |
||
326 | public function getClient() { |
||
327 | $this->client->checkConnection(); |
||
328 | return $this->client; |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * Set the limit and page for the current query |
||
333 | * @param int $limit |
||
334 | * @param int $page |
||
335 | * |
||
336 | * @return $this |
||
337 | */ |
||
338 | public function limit($limit, $page = 1) { |
||
339 | if($page >= 1) $this->page = $page; |
||
340 | $this->limit = $limit; |
||
341 | |||
342 | return $this; |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * @return array |
||
347 | */ |
||
348 | public function getQuery() { |
||
349 | return $this->query; |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * @param array $query |
||
354 | * @return Query |
||
355 | */ |
||
356 | public function setQuery($query) { |
||
357 | $this->query = $query; |
||
358 | return $this; |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * @return string |
||
363 | */ |
||
364 | public function getRawQuery() { |
||
365 | return $this->raw_query; |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * @param string $raw_query |
||
370 | * @return Query |
||
371 | */ |
||
372 | public function setRawQuery($raw_query) { |
||
373 | $this->raw_query = $raw_query; |
||
374 | return $this; |
||
375 | } |
||
376 | |||
377 | /** |
||
378 | * @return string |
||
379 | */ |
||
380 | public function getCharset() { |
||
381 | return $this->charset; |
||
382 | } |
||
383 | |||
384 | /** |
||
385 | * @param string $charset |
||
386 | * @return Query |
||
387 | */ |
||
388 | public function setCharset($charset) { |
||
389 | $this->charset = $charset; |
||
390 | return $this; |
||
391 | } |
||
392 | |||
393 | /** |
||
394 | * @param Client $client |
||
395 | * @return Query |
||
396 | */ |
||
397 | public function setClient(Client $client) { |
||
398 | $this->client = $client; |
||
399 | return $this; |
||
400 | } |
||
401 | |||
402 | /** |
||
403 | * @return int |
||
404 | */ |
||
405 | public function getLimit() { |
||
406 | return $this->limit; |
||
407 | } |
||
408 | |||
409 | /** |
||
410 | * @param int $limit |
||
411 | * @return Query |
||
412 | */ |
||
413 | public function setLimit($limit) { |
||
416 | } |
||
417 | |||
418 | /** |
||
419 | * @return int |
||
420 | */ |
||
421 | public function getPage() { |
||
422 | return $this->page; |
||
423 | } |
||
424 | |||
425 | /** |
||
426 | * @param int $page |
||
427 | * @return Query |
||
428 | */ |
||
429 | public function setPage($page) { |
||
430 | $this->page = $page; |
||
431 | return $this; |
||
432 | } |
||
433 | |||
434 | /** |
||
435 | * @param boolean $fetch_options |
||
436 | * @return Query |
||
437 | */ |
||
438 | public function setFetchOptions($fetch_options) { |
||
439 | $this->fetch_options = $fetch_options; |
||
440 | return $this; |
||
441 | } |
||
442 | |||
443 | /** |
||
444 | * @param boolean $fetch_options |
||
445 | * @return Query |
||
446 | */ |
||
447 | public function fetchOptions($fetch_options) { |
||
448 | return $this->setFetchOptions($fetch_options); |
||
449 | } |
||
450 | |||
451 | /** |
||
452 | * @return int |
||
453 | */ |
||
454 | public function getFetchOptions() { |
||
455 | return $this->fetch_options; |
||
456 | } |
||
457 | |||
458 | /** |
||
459 | * @return boolean |
||
460 | */ |
||
461 | public function getFetchBody() { |
||
462 | return $this->fetch_body; |
||
463 | } |
||
464 | |||
465 | /** |
||
466 | * @param boolean $fetch_body |
||
467 | * @return Query |
||
468 | */ |
||
469 | public function setFetchBody($fetch_body) { |
||
470 | $this->fetch_body = $fetch_body; |
||
471 | return $this; |
||
472 | } |
||
473 | |||
474 | /** |
||
475 | * @param boolean $fetch_body |
||
476 | * @return Query |
||
477 | */ |
||
478 | public function fetchBody($fetch_body) { |
||
479 | return $this->setFetchBody($fetch_body); |
||
480 | } |
||
481 | |||
482 | /** |
||
483 | * @return boolean |
||
484 | */ |
||
485 | public function getFetchAttachment() { |
||
486 | return $this->fetch_attachment; |
||
487 | } |
||
488 | |||
489 | /** |
||
490 | * @param boolean $fetch_attachment |
||
491 | * @return Query |
||
492 | */ |
||
493 | public function setFetchAttachment($fetch_attachment) { |
||
494 | $this->fetch_attachment = $fetch_attachment; |
||
495 | return $this; |
||
496 | } |
||
497 | |||
498 | /** |
||
499 | * @param boolean $fetch_attachment |
||
500 | * @return Query |
||
501 | */ |
||
502 | public function fetchAttachment($fetch_attachment) { |
||
503 | return $this->setFetchAttachment($fetch_attachment); |
||
504 | } |
||
505 | |||
506 | /** |
||
507 | * @return int |
||
508 | */ |
||
509 | public function getFetchFlags() { |
||
511 | } |
||
512 | |||
513 | /** |
||
514 | * @param int $fetch_flags |
||
515 | * @return Query |
||
516 | */ |
||
517 | public function setFetchFlags($fetch_flags) { |
||
518 | $this->fetch_flags = $fetch_flags; |
||
519 | return $this; |
||
520 | } |
||
521 | } |
||
522 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..