Passed
Push — master ( 0272f5...3a0c5f )
by Aleksandr
28:04 queued 25:53
created

QB_OPERATOR   F

Complexity

Total Complexity 136

Size/Duplication

Total Lines 333
Duplicated Lines 0 %

Test Coverage

Coverage 98.92%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 243
dl 0
loc 333
ccs 92
cts 93
cp 0.9892
rs 2
c 1
b 0
f 0
wmc 136

12 Methods

Rating   Name   Duplication   Size   Complexity  
A isBetween() 0 8 3
A isNull() 0 8 3
D getCase() 0 45 21
A append() 0 10 5
D sql() 0 49 21
A isNeedsArray() 0 10 5
D applyTo() 0 39 21
D expr() 0 49 21
A prepend() 0 10 5
B isLike() 0 12 7
A isEmpty() 0 8 3
D isValueRequired() 0 25 21

How to fix   Complexity   

Complex Class

Complex classes like QB_OPERATOR 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 QB_OPERATOR, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * This file is part of the eav package.
4
 * @author    Aleksandr Drobotik <[email protected]>
5
 * @copyright 2023 Aleksandr Drobotik
6
 * @license   https://opensource.org/license/mit  The MIT License
7
 */
8
declare(strict_types=1);
9
10
namespace Drobotik\Eav\Enum;
11
12
use Drobotik\Eav\Exception\QueryBuilderException;
13
14
class QB_OPERATOR
15
{
16
    public const EQUAL = 'equal';
17
    public const NOT_EQUAL = 'not_equal';
18
    public const IN = 'in';
19
    public const NOT_IN = 'not_in';
20
    public const LESS = 'less';
21
    public const LESS_OR_EQUAL = 'less_or_equal';
22
    public const GREATER = 'greater';
23
    public const GREATER_OR_EQUAL = 'greater_or_equal';
24
    public const BETWEEN = 'between';
25
    public const NOT_BETWEEN = 'not_between';
26
    public const BEGINS_WITH = 'begins_with';
27
    public const NOT_BEGINS_WITH = 'not_begins_with';
28
    public const CONTAINS = 'contains';
29
    public const NOT_CONTAINS = 'not_contains';
30
    public const ENDS_WITH = 'ends_with';
31
    public const NOT_ENDS_WITH = 'not_ends_with';
32
    public const IS_EMPTY = 'is_empty';
33
    public const IS_NOT_EMPTY = 'is_not_empty';
34
    public const IS_NULL = 'is_null';
35
    public const IS_NOT_NULL = 'is_not_null';
36
37
    /**
38
     * @throws QueryBuilderException
39
     */
40 1
    public static function getCase(string $case) : string
41
    {
42
        switch ($case) {
43
            case self::EQUAL:
44 1
                return self::EQUAL;
45
            case self::NOT_EQUAL:
46 1
                return self::NOT_EQUAL;
47
            case self::IN:
48 1
                return self::IN;
49
            case self::NOT_IN:
50 1
                return self::NOT_IN;
51
            case self::LESS:
52 1
                return self::LESS;
53
            case self::LESS_OR_EQUAL:
54 1
                return self::LESS_OR_EQUAL;
55
            case self::GREATER:
56 1
                return self::GREATER;
57
            case self::GREATER_OR_EQUAL:
58 1
                return self::GREATER_OR_EQUAL;
59
            case self::BETWEEN:
60 1
                return self::BETWEEN;
61
            case self::NOT_BETWEEN:
62 1
                return self::NOT_BETWEEN;
63
            case self::BEGINS_WITH:
64 1
                return self::BEGINS_WITH;
65
            case self::NOT_BEGINS_WITH:
66 1
                return self::NOT_BEGINS_WITH;
67
            case self::CONTAINS:
68 1
                return self::CONTAINS;
69
            case self::NOT_CONTAINS:
70 1
                return self::NOT_CONTAINS;
71
            case self::ENDS_WITH:
72 1
                return self::ENDS_WITH;
73
            case self::NOT_ENDS_WITH:
74 1
                return self::NOT_ENDS_WITH;
75
            case self::IS_EMPTY:
76 1
                return self::IS_EMPTY;
77
            case self::IS_NOT_EMPTY:
78 1
                return self::IS_NOT_EMPTY;
79
            case self::IS_NULL:
80 1
                return self::IS_NULL;
81
            case self::IS_NOT_NULL:
82 1
                return self::IS_NOT_NULL;
83
            default:
84 1
                throw QueryBuilderException::unsupportedOperator($case);
85
        }
86
    }
87 1
    public static function isValueRequired(string $name): bool {
88
        switch ($name) {
89
            case self::EQUAL:
90
            case self::NOT_EQUAL:
91
            case self::IN:
92
            case self::NOT_IN:
93
            case self::LESS:
94
            case self::LESS_OR_EQUAL:
95
            case self::GREATER:
96
            case self::GREATER_OR_EQUAL:
97
            case self::BETWEEN:
98
            case self::NOT_BETWEEN:
99
            case self::BEGINS_WITH:
100
            case self::NOT_BEGINS_WITH:
101
            case self::CONTAINS:
102
            case self::NOT_CONTAINS:
103
            case self::ENDS_WITH:
104
            case self::NOT_ENDS_WITH:
105 1
                return true;
106
107
            case self::IS_EMPTY:
108
            case self::IS_NOT_EMPTY:
109
            case self::IS_NULL:
110
            case self::IS_NOT_NULL:
0 ignored issues
show
introduced by
The function implicitly returns null when this case condition does not match. This is incompatible with the type-hinted return boolean. Consider adding a default case to the switch.
Loading history...
111 1
                return false;
112
        }
113
    }
114
115
116 1
    public static function applyTo(string $name): array {
117
        switch ($name) {
118
            case self::EQUAL:
119
            case self::NOT_EQUAL:
120
            case self::IN:
121
            case self::NOT_IN:
122
            case self::LESS:
123
            case self::LESS_OR_EQUAL:
124
            case self::GREATER:
125
            case self::GREATER_OR_EQUAL:
126
            case self::BETWEEN:
127
            case self::NOT_BETWEEN:
128 1
                return [
129 1
                    ATTR_TYPE::INTEGER,
130 1
                    ATTR_TYPE::DECIMAL,
131 1
                    ATTR_TYPE::DATETIME
132 1
                ];
133
134
            case self::BEGINS_WITH:
135
            case self::NOT_BEGINS_WITH:
136
            case self::CONTAINS:
137
            case self::NOT_CONTAINS:
138
            case self::ENDS_WITH:
139
            case self::NOT_ENDS_WITH:
140
            case self::IS_EMPTY:
141
            case self::IS_NOT_EMPTY:
142 1
                return [
143 1
                    ATTR_TYPE::STRING,
144 1
                    ATTR_TYPE::TEXT
145 1
                ];
146
147
            case self::IS_NULL:
148
            case self::IS_NOT_NULL:
0 ignored issues
show
introduced by
The function implicitly returns null when this case condition does not match. This is incompatible with the type-hinted return array. Consider adding a default case to the switch.
Loading history...
149 1
                return [
150 1
                    ATTR_TYPE::STRING,
151 1
                    ATTR_TYPE::TEXT,
152 1
                    ATTR_TYPE::INTEGER,
153 1
                    ATTR_TYPE::DECIMAL,
154 1
                    ATTR_TYPE::DATETIME
155 1
                ];
156
        }
157
    }
158
159 1
    public static function sql(string $name): string {
160
        switch ($name) {
161
            case self::EQUAL:
162
            case self::IS_EMPTY:
163 1
                return '=';
164
165
            case self::NOT_EQUAL:
166
            case self::IS_NOT_EMPTY:
167 1
                return '!=';
168
169
            case self::IN:
170 1
                return 'IN';
171
172
            case self::NOT_IN:
173 1
                return 'NOT IN';
174
175
            case self::LESS:
176 1
                return '<';
177
178
            case self::LESS_OR_EQUAL:
179 1
                return '<=';
180
181
            case self::GREATER:
182 1
                return '>';
183
184
            case self::GREATER_OR_EQUAL:
185 1
                return '>=';
186
187
            case self::BETWEEN:
188 1
                return 'BETWEEN';
189
190
            case self::NOT_BETWEEN:
191 1
                return 'NOT BETWEEN';
192
193
            case self::BEGINS_WITH:
194
            case self::CONTAINS:
195
            case self::ENDS_WITH:
196 1
                return 'LIKE';
197
198
            case self::NOT_BEGINS_WITH:
199
            case self::NOT_CONTAINS:
200
            case self::NOT_ENDS_WITH:
201 1
                return 'NOT LIKE';
202
203
            case self::IS_NULL:
204 1
                return 'NULL';
205
206
            case self::IS_NOT_NULL:
0 ignored issues
show
introduced by
The function implicitly returns null when this case condition does not match. This is incompatible with the type-hinted return string. Consider adding a default case to the switch.
Loading history...
207 1
                return 'NOT NULL';
208
        }
209
    }
210
211 1
    public static function prepend(string $name): string|bool {
212
        switch ($name) {
213
            case self::ENDS_WITH:
214
            case self::NOT_ENDS_WITH:
215
            case self::CONTAINS:
216
            case self::NOT_CONTAINS:
217 1
                return '%';
218
219
            default:
220 1
                return false;
221
        }
222
    }
223
224 1
    public static function append(string $name): string|bool {
225
        switch ($name) {
226
            case self::BEGINS_WITH:
227
            case self::NOT_BEGINS_WITH:
228
            case self::CONTAINS:
229
            case self::NOT_CONTAINS:
230 1
                return '%';
231
232
            default:
233 1
                return false;
234
        }
235
    }
236
237 1
    public static function isNeedsArray(string $name): bool {
238
        switch ($name) {
239
            case self::NOT_IN:
240
            case self::IN:
241
            case self::NOT_BETWEEN:
242
            case self::BETWEEN:
243 1
                return true;
244
245
            default:
246 1
                return false;
247
        }
248
    }
249
250 1
    public static function isNull(string $name): bool {
251
        switch ($name) {
252
            case self::IS_NOT_NULL:
253
            case self::IS_NULL:
254 1
                return true;
255
256
            default:
257 1
                return false;
258
        }
259
    }
260
261 1
    public static function isEmpty(string $name): bool {
262
        switch ($name) {
263
            case self::IS_EMPTY:
264
            case self::IS_NOT_EMPTY:
265 1
                return true;
266
267
            default:
268 1
                return false;
269
        }
270
    }
271
272 1
    public static function isBetween(string $name): bool {
273
        switch ($name) {
274
            case self::BETWEEN:
275
            case self::NOT_BETWEEN:
276 1
                return true;
277
278
            default:
279 1
                return false;
280
        }
281
    }
282
283 1
    public static function isLike(string $name): bool {
284
        switch ($name) {
285
            case self::BEGINS_WITH:
286
            case self::CONTAINS:
287
            case self::ENDS_WITH:
288
            case self::NOT_BEGINS_WITH:
289
            case self::NOT_ENDS_WITH:
290
            case self::NOT_CONTAINS:
291 1
                return true;
292
293
            default:
294
                return false;
295
        }
296
    }
297
298 1
    public static function expr(string $name): string {
299
        switch ($name) {
300
            case self::EQUAL:
301
            case self::IS_EMPTY:
302 1
                return 'eq';
303
304
            case self::NOT_EQUAL:
305
            case self::IS_NOT_EMPTY:
306 1
                return 'neq';
307
308
            case self::IN:
309 1
                return 'in';
310
311
            case self::NOT_IN:
312 1
                return 'notIn';
313
314
            case self::LESS:
315 1
                return 'lt';
316
317
            case self::LESS_OR_EQUAL:
318 1
                return 'lte';
319
320
            case self::GREATER:
321 1
                return 'gt';
322
323
            case self::GREATER_OR_EQUAL:
324 1
                return 'gte';
325
326
            case self::BETWEEN:
327 1
                return 'between';
328
329
            case self::NOT_BETWEEN:
330 1
                return 'notBetween';
331
332
            case self::BEGINS_WITH:
333
            case self::CONTAINS:
334
            case self::ENDS_WITH:
335 1
                return 'like';
336
337
            case self::NOT_BEGINS_WITH:
338
            case self::NOT_CONTAINS:
339
            case self::NOT_ENDS_WITH:
340 1
                return 'notLike';
341
342
            case self::IS_NULL:
343 1
                return 'isNull';
344
345
            case self::IS_NOT_NULL:
0 ignored issues
show
introduced by
The function implicitly returns null when this case condition does not match. This is incompatible with the type-hinted return string. Consider adding a default case to the switch.
Loading history...
346 1
                return 'isNotNull';
347
        }
348
    }
349
}