Passed
Push — master ( 6d2530...b472bc )
by Pol
01:42
created

Operator::of()   D

Complexity

Conditions 29
Paths 1

Size

Total Lines 56
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 51
CRAP Score 29.0059

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 29
eloc 52
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 56
ccs 51
cts 52
cp 0.9808
crap 29.0059
rs 4.1666

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
declare(strict_types=1);
4
5
namespace loophp\fpt;
6
7
use Closure;
8
use InvalidArgumentException;
9
10
// phpcs:disable Generic.Files.LineLength.TooLong
11
12
final class Operator
13
{
14
    public const OP_AND = 'and';
15
16
    public const OP_BINARY_AND = '&';
17
18
    public const OP_BINARY_OR = '|';
19
20
    public const OP_BINARY_SHIFT_LEFT = '<<';
21
22
    public const OP_BINARY_SHIFT_RIGHT = '>>';
23
24
    public const OP_BINARY_XOR = '^';
25
26
    public const OP_DIV = '/';
27
28
    public const OP_EQUAL = '===';
29
30
    public const OP_GT = '>';
31
32
    public const OP_GTE = '>=';
33
34
    public const OP_INSTANCEOF = 'instanceof';
35
36
    public const OP_LIKE = '==';
37
38
    public const OP_LT = '<';
39
40
    public const OP_LTE = '<=';
41
42
    public const OP_MINUS = '-';
43
44
    public const OP_MODULO = '%';
45
46
    public const OP_MULT = '*';
47
48
    public const OP_NOT_EQUAL = '!==';
49
50
    public const OP_NOT_LIKE = '!=';
51
52
    public const OP_OR = 'or';
53
54
    public const OP_PLUS = '+';
55
56
    public const OP_SPACESHIP = '<=>';
57
58
    public const OP_XOR = 'xor';
59
60 24
    public static function of(): Closure
61
    {
62 24
        return static fn (string $operator): Closure => static fn ($left): Closure => static function ($right) use ($operator, $left) {
63
            switch ($operator) {
64 23
                        case self::OP_AND:
65 23
                        case '&&':
66 1
                    return $left && $right;
67 22
                        case self::OP_MINUS:
68 1
                    return $left - $right;
69 21
                        case self::OP_PLUS:
70 1
                    return $left + $right;
71 20
                        case self::OP_MODULO:
72 1
                    return $left % $right;
73 19
                        case self::OP_DIV:
74 1
                    return $left / $right;
75 18
                        case self::OP_MULT:
76 1
                    return $left * $right;
77 17
                        case self::OP_OR:
78 17
                        case '||':
79 1
                    return $left || $right;
80 16
                        case self::OP_XOR:
81 1
                    return $left xor $right;
82 15
                        case '<<':
83 1
                    return $left << $right;
84 14
                        case '>>':
85 1
                    return $left >> $right;
86 13
                        case '&':
87 1
                    return $left & $right;
88 12
                        case '|':
89 1
                    return $left | $right;
90 11
                        case '^':
91 1
                    return $left ^ $right;
92 10
                        case '>':
93 1
                    return $left > $right;
94 9
                        case '>=':
95 1
                    return $left >= $right;
96 8
                        case '<':
97 1
                    return $left < $right;
98 7
                        case '<=':
99 1
                    return $left <= $right;
100 6
                        case 'instanceof':
101 1
                    return $left instanceof $right;
102 5
                        case '!==':
103 1
                    return $left !== $right;
104 4
                        case '!=':
105 4
                        case '<>':
106 1
                    return $left != $right;
107 3
                        case '===':
108 1
                    return $left === $right;
109 2
                        case '==':
110 1
                    return $left !== $right;
111 1
                        case '<=>':
112 1
                    return $left <=> $right;
113
            }
114
115
            throw new InvalidArgumentException('Unable to find the operator.');
116 24
        };
117
    }
118
}
119