Passed
Push — master ( 01984b...00f43d )
by Malte
03:00
created

WhereQuery   A

Complexity

Total Complexity 40

Size/Duplication

Total Lines 329
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 62
dl 0
loc 329
rs 9.2
c 0
b 0
f 0
wmc 40

30 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 __call() 0 7 2
B where() 0 21 7
A whereNew() 0 2 1
A whereBcc() 0 2 1
A whereDeleted() 0 2 1
A whereKeyword() 0 2 1
A whereAnswered() 0 2 1
A andWhere() 0 5 2
A whereFlagged() 0 2 1
A whereBody() 0 2 1
A whereAll() 0 2 1
A whereNot() 0 2 1
A whereUnkeyword() 0 2 1
A whereOld() 0 2 1
A whereUnanswered() 0 2 1
A whereUndeleted() 0 2 1
A whereOn() 0 3 1
A whereTo() 0 2 1
A whereText() 0 2 1
A whereUnflagged() 0 2 1
A whereSince() 0 3 1
A whereSeen() 0 2 1
A whereRecent() 0 2 1
A whereUnseen() 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 unkeyword($value)
36
 * @method WhereQuery to($value)
37
 * @method WhereQuery text($value)
38
 * @method WhereQuery subject($value)
39
 * @method WhereQuery since($date)
40
 * @method WhereQuery on($date)
41
 * @method WhereQuery keyword($value)
42
 * @method WhereQuery from($value)
43
 * @method WhereQuery flagged()
44
 * @method WhereQuery cc($value)
45
 * @method WhereQuery body($value)
46
 * @method WhereQuery before($date)
47
 * @method WhereQuery bcc($value)
48
 *
49
 * @mixin Query
50
 */
51
class WhereQuery extends Query {
52
53
    /**
54
     * @var array $available_criteria
55
     */
56
    protected $available_criteria = [
57
        'OR', 'AND',
58
        'ALL', 'ANSWERED', 'BCC', 'BEFORE', 'BODY', 'CC', 'DELETED', 'FLAGGED', 'FROM', 'KEYWORD',
59
        'NEW', 'NOT', 'OLD', 'ON', 'RECENT', 'SEEN', 'SINCE', 'SUBJECT', 'TEXT', 'TO',
60
        'UNANSWERED', 'UNDELETED', 'UNFLAGGED', 'UNKEYWORD', 'UNSEEN'
61
    ];
62
63
    /**
64
     * Magic method in order to allow alias usage of all "where" methods
65
     * @param string $name
66
     * @param array|null $arguments
67
     *
68
     * @return mixed
69
     * @throws MethodNotFoundException
70
     */
71
    public function __call($name, $arguments) {
72
        $method = 'where'.ucfirst($name);
73
        if(method_exists($this, $method) === true){
74
            return call_user_func_array([$this, $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

74
            return call_user_func_array([$this, $method], /** @scrutinizer ignore-type */ $arguments);
Loading history...
75
        }
76
77
        throw new MethodNotFoundException();
78
    }
79
80
    /**
81
     * Validate a given criteria
82
     * @param $criteria
83
     *
84
     * @return string
85
     * @throws InvalidWhereQueryCriteriaException
86
     */
87
    protected function validate_criteria($criteria) {
88
        $criteria = strtoupper($criteria);
89
90
        if(in_array($criteria, $this->available_criteria) === false) {
91
            throw new InvalidWhereQueryCriteriaException();
92
        }
93
94
        return $criteria;
95
    }
96
97
    /**
98
     * @param mixed $criteria
99
     * @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...
100
     * 
101
     * @return $this
102
     * @throws InvalidWhereQueryCriteriaException
103
     */
104
    public function where($criteria, $value = null) {
105
        if(is_array($criteria)){
106
            foreach($criteria as $arguments){
107
                if(count($arguments) == 1){
108
                    $this->where($arguments[0]);
109
                }elseif(count($arguments) == 2){
110
                    $this->where($arguments[0], $arguments[1]);
111
                }
112
            }
113
        }else{
114
            $criteria = $this->validate_criteria($criteria);
115
            $value = $this->parse_value($value);
116
117
            if($value === null || $value === ''){
118
                $this->query->push([$criteria]);
119
            }else{
120
                $this->query->push([$criteria, $value]);
121
            }
122
        }
123
124
        return $this;
125
    }
126
127
    /**
128
     * @param \Closure $closure
129
     *
130
     * @return $this
131
     */
132
    public function orWhere(\Closure $closure = null) {
133
        $this->query->push(['OR']);
134
        if($closure !== null) $closure($this);
135
136
        return $this;
137
    }
138
139
    /**
140
     * @param \Closure $closure
141
     *
142
     * @return $this
143
     */
144
    public function andWhere(\Closure $closure = null) {
145
        $this->query->push(['AND']);
146
        if($closure !== null) $closure($this);
147
148
        return $this;
149
    }
150
151
    /**
152
     * @return WhereQuery
153
     * @throws InvalidWhereQueryCriteriaException
154
     */
155
    public function whereAll() {
156
        return $this->where('ALL');
157
    }
158
159
    /**
160
     * @return WhereQuery
161
     * @throws InvalidWhereQueryCriteriaException
162
     */
163
    public function whereAnswered() {
164
        return $this->where('ANSWERED');
165
    }
166
167
    /**
168
     * @param string $value
169
     * 
170
     * @return WhereQuery
171
     * @throws InvalidWhereQueryCriteriaException
172
     */
173
    public function whereBcc($value) {
174
        return $this->where('BCC', $value);
175
    }
176
177
    /**
178
     * @param mixed $value
179
     * @return WhereQuery
180
     * @throws InvalidWhereQueryCriteriaException
181
     * @throws MessageSearchValidationException
182
     */
183
    public function whereBefore($value) {
184
        $date = $this->parse_date($value);
185
        return $this->where('BEFORE', $date);
186
    }
187
188
    /**
189
     * @param string $value
190
     *
191
     * @return WhereQuery
192
     * @throws InvalidWhereQueryCriteriaException
193
     */
194
    public function whereBody($value) {
195
        return $this->where('BODY', $value);
196
    }
197
198
    /**
199
     * @param string $value
200
     *
201
     * @return WhereQuery
202
     * @throws InvalidWhereQueryCriteriaException
203
     */
204
    public function whereCc($value) {
205
        return $this->where('CC', $value);
206
    }
207
208
    /**
209
     * @return WhereQuery
210
     * @throws InvalidWhereQueryCriteriaException
211
     */
212
    public function whereDeleted() {
213
        return $this->where('DELETED');
214
    }
215
216
    /**
217
     * @param string $value
218
     *
219
     * @return WhereQuery
220
     * @throws InvalidWhereQueryCriteriaException
221
     */
222
    public function whereFlagged($value) {
223
        return $this->where('FLAGGED', $value);
224
    }
225
226
    /**
227
     * @param string $value
228
     *
229
     * @return WhereQuery
230
     * @throws InvalidWhereQueryCriteriaException
231
     */
232
    public function whereFrom($value) {
233
        return $this->where('FROM', $value);
234
    }
235
236
    /**
237
     * @param string $value
238
     *
239
     * @return WhereQuery
240
     * @throws InvalidWhereQueryCriteriaException
241
     */
242
    public function whereKeyword($value) {
243
        return $this->where('KEYWORD', $value);
244
    }
245
246
    /**
247
     * @return WhereQuery
248
     * @throws InvalidWhereQueryCriteriaException
249
     */
250
    public function whereNew() {
251
        return $this->where('NEW');
252
    }
253
254
    /**
255
     * @return WhereQuery
256
     * @throws InvalidWhereQueryCriteriaException
257
     */
258
    public function whereNot() {
259
        return $this->where('NOT');
260
    }
261
262
    /**
263
     * @return WhereQuery
264
     * @throws InvalidWhereQueryCriteriaException
265
     */
266
    public function whereOld() {
267
        return $this->where('OLD');
268
    }
269
270
    /**
271
     * @param mixed $value
272
     *
273
     * @return WhereQuery
274
     * @throws MessageSearchValidationException
275
     * @throws InvalidWhereQueryCriteriaException
276
     */
277
    public function whereOn($value) {
278
        $date = $this->parse_date($value);
279
        return $this->where('ON', $date);
280
    }
281
282
    /**
283
     * @return WhereQuery
284
     * @throws InvalidWhereQueryCriteriaException
285
     */
286
    public function whereRecent() {
287
        return $this->where('RECENT');
288
    }
289
290
    /**
291
     * @return WhereQuery
292
     * @throws InvalidWhereQueryCriteriaException
293
     */
294
    public function whereSeen() {
295
        return $this->where('SEEN');
296
    }
297
298
    /**
299
     * @param mixed $value
300
     *
301
     * @return WhereQuery
302
     * @throws MessageSearchValidationException
303
     * @throws InvalidWhereQueryCriteriaException
304
     */
305
    public function whereSince($value) {
306
        $date = $this->parse_date($value);
307
        return $this->where('SINCE', $date);
308
    }
309
310
    /**
311
     * @param string $value
312
     *
313
     * @return WhereQuery
314
     * @throws InvalidWhereQueryCriteriaException
315
     */
316
    public function whereSubject($value) {
317
        return $this->where('SUBJECT', $value);
318
    }
319
320
    /**
321
     * @param string $value
322
     *
323
     * @return WhereQuery
324
     * @throws InvalidWhereQueryCriteriaException
325
     */
326
    public function whereText($value) {
327
        return $this->where('TEXT', $value);
328
    }
329
330
    /**
331
     * @param string $value
332
     *
333
     * @return WhereQuery
334
     * @throws InvalidWhereQueryCriteriaException
335
     */
336
    public function whereTo($value) {
337
        return $this->where('TO', $value);
338
    }
339
340
    /**
341
     * @param string $value
342
     *
343
     * @return WhereQuery
344
     * @throws InvalidWhereQueryCriteriaException
345
     */
346
    public function whereUnkeyword($value) {
347
        return $this->where('UNKEYWORD', $value);
348
    }
349
350
    /**
351
     * @return WhereQuery
352
     * @throws InvalidWhereQueryCriteriaException
353
     */
354
    public function whereUnanswered() {
355
        return $this->where('UNANSWERED');
356
    }
357
358
    /**
359
     * @return WhereQuery
360
     * @throws InvalidWhereQueryCriteriaException
361
     */
362
    public function whereUndeleted() {
363
        return $this->where('UNDELETED');
364
    }
365
366
    /**
367
     * @return WhereQuery
368
     * @throws InvalidWhereQueryCriteriaException
369
     */
370
    public function whereUnflagged() {
371
        return $this->where('UNFLAGGED');
372
    }
373
374
    /**
375
     * @return WhereQuery
376
     * @throws InvalidWhereQueryCriteriaException
377
     */
378
    public function whereUnseen() {
379
        return $this->where('UNSEEN');
380
    }
381
}