Passed
Push — master ( 78d0af...d10895 )
by Brian
02:44
created

Util::compare()   D

Complexity

Conditions 26
Paths 26

Size

Total Lines 84
Code Lines 72

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 71
CRAP Score 26

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 72
c 1
b 0
f 0
dl 0
loc 84
ccs 71
cts 71
cp 1
rs 4.1666
cc 26
nc 26
nop 3
crap 26

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Bmatovu\Ussd\Support;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Str;
7
8
class Util
9
{
10 2
    public static function regex($pattern): string
11
    {
12 2
        return Str::startsWith($pattern, '/') ? $pattern : "/{$pattern}/";
13
    }
14 11
    public static function compare($key, $condition, $value): bool
15
    {
16
        switch ($condition) {
17
                // -------------------- Numbers --------------------
18 11
            case 'lt':
19 1
                return $key < $value;
20 11
            case 'gt':
21 1
                return $key > $value;
22 11
            case 'lte':
23 1
                return $key <= $value;
24 11
            case 'gte':
25 1
                return $key >= $value;
26 11
            case 'eq':
27 6
                return $key == $value;
28 6
            case 'ne':
29 1
                return $key != $value;
30 6
            case 'btn':
31 1
                list($min, $max) = explode(',', $value);
32 1
                return ($min <= $key) && ($key <= $max);
33
                // -------------------- Strings --------------------
34 6
            case 'str.equals':
35 1
                return $key == $value;
36 6
            case 'str.not_equals':
37 1
                return $key != $value;
38 6
            case 'str.starts':
39 1
                return Str::startsWith($key, $value);
40 6
            case 'str.ends':
41 1
                return Str::endsWith($key, $value);
42 6
            case 'str.contains':
43 1
                $values = explode(',', $value);
44 1
                return Str::contains($key, $values);
45
                // -------------------- Regex --------------------
46 5
            case 'regex.match':
47 1
                return preg_match(static::regex($value), $key);
0 ignored issues
show
Bug Best Practice introduced by
The expression return preg_match(static::regex($value), $key) returns the type integer which is incompatible with the type-hinted return boolean.
Loading history...
48
                // -------------------- Arrays --------------------
49 4
            case 'arr.in':
50 1
                $values = explode(',', $value);
51 1
                return in_array($key, $values);
52 4
            case 'arr.not_in':
53 1
                $values = explode(',', $value);
54 1
                return !in_array($key, $values);
55
                // -------------------- Dates --------------------
56 3
            case 'date.equals':
57
                // Format: Y-m-d
58 1
                $key = new Carbon($key);
59 1
                $value = new Carbon($value);
60 1
                return $key->toDateString() == $value->toDateString();
61 3
            case 'date.before':
62 1
                $key = new Carbon($key);
63 1
                $value = new Carbon($value);
64 1
                return $key->isBefore($value);
65 3
            case 'date.after':
66 1
                $key = new Carbon($key);
67 1
                $value = new Carbon($value);
68 1
                return $key->isAfter($value);
69 3
            case 'date.between':
70 1
                $key = new Carbon($key);
71
72 1
                list($start, $end) = explode(',', $value);
73 1
                $start = new Carbon($start);
74 1
                $end = new Carbon($end);
75 1
                return $key->between($start, $end);
76
                // -------------------- Time --------------------
77 2
            case 'time.equals':
78
                // Format: H:i:s
79 1
                $key = new Carbon($key);
80 1
                return $key->toTimeString('second') == $value;
81 2
            case 'time.before':
82 1
                $key = (new Carbon($key))->format('His');
83 1
                $value = now()->setTimeFromTimeString($value)->format('His');
84 1
                return $key < $value;
85 2
            case 'time.after':
86 1
                $key = (new Carbon($key))->format('His');
87 1
                $value = now()->setTimeFromTimeString($value)->format('His');
88 1
                return $key > $value;
89 2
            case 'time.between':
90 1
                $key = (new Carbon($key))->format('His');
91
92 1
                list($start, $end) = explode(',', $value);
93 1
                $min = now()->setTimeFromTimeString($start)->format('His');
94 1
                $max = now()->setTimeFromTimeString($end)->format('His');
95 1
                return ($min <= $key) && ($key <= $max);
96
            default:
97 1
                return $key == $value;
98
        }
99
    }
100
}
101