Passed
Push — master ( 00f43d...a0d391 )
by Malte
02:56
created

WhereQuery   B

Complexity

Total Complexity 45

Size/Duplication

Total Lines 375
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 71
dl 0
loc 375
rs 8.8
c 0
b 0
f 0
wmc 45

34 Methods

Rating   Name   Duplication   Size   Complexity  
A orWhere() 0 5 2
A whereFrom() 0 2 1
A whereBefore() 0 3 1
A whereCc() 0 2 1
A validate_criteria() 0 8 2
A whereNot() 0 2 1
A __call() 0 16 3
A whereUnkeyword() 0 2 1
B where() 0 21 7
A whereOld() 0 2 1
A whereUnanswered() 0 2 1
A whereUndeleted() 0 2 1
A whereNew() 0 2 1
A whereOn() 0 3 1
A whereTo() 0 2 1
A whereNoXSpam() 0 2 1
A whereBcc() 0 2 1
A whereDeleted() 0 2 1
A whereText() 0 2 1
A whereKeyword() 0 2 1
A whereMessageId() 0 2 1
A whereAnswered() 0 2 1
A andWhere() 0 5 2
A whereUnflagged() 0 2 1
A whereSince() 0 3 1
A whereLanguage() 0 2 1
A whereSeen() 0 2 1
A whereRecent() 0 2 1
A whereFlagged() 0 2 1
A whereIsXSpam() 0 2 1
A whereBody() 0 2 1
A whereUnseen() 0 2 1
A whereAll() 0 2 1
A whereSubject() 0 2 1

How to fix   Complexity   

Complex Class

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
2
/*
3
* File:     Query.php
4
* Category: -
5
* Author:   M. Goldenbaum
6
* Created:  21.07.18 18:54
7
* Updated:  -
8
*
9
* Description:
10
*  -
11
*/
12
13
namespace Webklex\IMAP\Query;
14
15
use Webklex\IMAP\Exceptions\InvalidWhereQueryCriteriaException;
16
use Webklex\IMAP\Exceptions\MethodNotFoundException;
17
use Webklex\IMAP\Exceptions\MessageSearchValidationException;
18
19
/**
20
 * Class WhereQuery
21
 *
22
 * @package Webklex\IMAP\Query
23
 * 
24
 * @method WhereQuery all()
25
 * @method WhereQuery answered()
26
 * @method WhereQuery deleted()
27
 * @method WhereQuery new()
28
 * @method WhereQuery old()
29
 * @method WhereQuery recent()
30
 * @method WhereQuery seen()
31
 * @method WhereQuery unanswered()
32
 * @method WhereQuery undeleted()
33
 * @method WhereQuery unflagged()
34
 * @method WhereQuery unseen()
35
 * @method WhereQuery not()
36
 * @method WhereQuery unkeyword($value)
37
 * @method WhereQuery to($value)
38
 * @method WhereQuery text($value)
39
 * @method WhereQuery subject($value)
40
 * @method WhereQuery since($date)
41
 * @method WhereQuery on($date)
42
 * @method WhereQuery keyword($value)
43
 * @method WhereQuery from($value)
44
 * @method WhereQuery flagged()
45
 * @method WhereQuery cc($value)
46
 * @method WhereQuery body($value)
47
 * @method WhereQuery before($date)
48
 * @method WhereQuery bcc($value)
49
 *
50
 * @mixin Query
51
 */
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);
0 ignored issues
show
Deprecated Code introduced by
The function camel_case() has been deprecated: Str::camel() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

76
        $name = /** @scrutinizer ignore-deprecated */ camel_case($name);

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.

Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $arguments can also be of type null; however, parameter $param_arr of call_user_func_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

85
            return call_user_func_array([$that, $method], /** @scrutinizer ignore-type */ $arguments);
Loading history...
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
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $value is correct as it would always require null to be passed?
Loading history...
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) {
144
        $this->query->push(['OR']);
145
        if($closure !== null) $closure($this);
146
147
        return $this;
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) {
244
        return $this->where('FROM', $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() {
270
        return $this->where('NOT');
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) {
358
        return $this->where('UNKEYWORD', $value);
359
    }
360
361
    /**
362
     * @return WhereQuery
363
     * @throws InvalidWhereQueryCriteriaException
364
     */
365
    public function whereUnanswered() {
366
        return $this->where('UNANSWERED');
367
    }
368
369
    /**
370
     * @return WhereQuery
371
     * @throws InvalidWhereQueryCriteriaException
372
     */
373
    public function whereUndeleted() {
374
        return $this->where('UNDELETED');
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(){
408
        return $this->where("X-Spam-Flag NO");
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){
426
        return $this->where("Content-Language $country_code");
427
    }
428
}