Passed
Push — master ( 0b1820...2a9526 )
by Pol
01:56
created

Operator::of()   D

Complexity

Conditions 29
Paths 1

Size

Total Lines 66
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 54
CRAP Score 29

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 29
eloc 54
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 66
ccs 54
cts 54
cp 1
crap 29
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
/**
13
 * @psalm-immutable
14
 */
15
final class Operator
16
{
17
    public const OP_AND = 'and';
18
19
    public const OP_BINARY_AND = '&';
20
21
    public const OP_BINARY_OR = '|';
22
23
    public const OP_BINARY_SHIFT_LEFT = '<<';
24
25
    public const OP_BINARY_SHIFT_RIGHT = '>>';
26
27
    public const OP_BINARY_XOR = '^';
28
29
    public const OP_DIV = '/';
30
31
    public const OP_EQUAL = '===';
32
33
    public const OP_GT = '>';
34
35
    public const OP_GTE = '>=';
36
37
    public const OP_INSTANCEOF = 'instanceof';
38
39
    public const OP_LIKE = '==';
40
41
    public const OP_LT = '<';
42
43
    public const OP_LTE = '<=';
44
45
    public const OP_MINUS = '-';
46
47
    public const OP_MODULO = '%';
48
49
    public const OP_MULT = '*';
50
51
    public const OP_NOT_EQUAL = '!==';
52
53
    public const OP_NOT_LIKE = '!=';
54
55
    public const OP_OR = 'or';
56
57
    public const OP_PLUS = '+';
58
59
    public const OP_SPACESHIP = '<=>';
60
61
    public const OP_XOR = 'xor';
62
63
    /**
64
     * @psalm-pure
65
     *
66
     * @psalm-return Closure(string): Closure(numeric|bool): Closure(numeric|bool): mixed
67
     */
68 25
    public static function of(): Closure
69
    {
70 25
        return static fn (string $operator): Closure =>
71
            /**
72
             * @psalm-param numeric|bool $left
73
             */
74 24
            static fn ($left): Closure =>
75
                /**
76
                 * @psalm-param numeric|bool $right
77
                 *
78
                 * @param mixed $right
79
                 */
80 24
                static function ($right) use ($operator, $left) {
81
                    switch ($operator) {
82 24
                                case self::OP_AND:
83 24
                                case '&&':
84 1
                            return $left && $right;
85 23
                                case self::OP_MINUS:
86 1
                            return $left - $right;
87 22
                                case self::OP_PLUS:
88 1
                            return $left + $right;
89 21
                                case self::OP_MODULO:
90 1
                            return $left % $right;
91 20
                                case self::OP_DIV:
92 1
                            return $left / $right;
93 19
                                case self::OP_MULT:
94 1
                            return $left * $right;
95 18
                                case self::OP_OR:
96 18
                                case '||':
97 1
                            return $left || $right;
98 17
                                case self::OP_XOR:
99 1
                            return $left xor $right;
100 16
                                case '<<':
101 1
                            return $left << $right;
102 15
                                case '>>':
103 1
                            return $left >> $right;
104 14
                                case '&':
105 1
                            return $left & $right;
106 13
                                case '|':
107 1
                            return $left | $right;
108 12
                                case '^':
109 1
                            return $left ^ $right;
110 11
                                case '>':
111 1
                            return $left > $right;
112 10
                                case '>=':
113 1
                            return $left >= $right;
114 9
                                case '<':
115 1
                            return $left < $right;
116 8
                                case '<=':
117 1
                            return $left <= $right;
118 7
                                case 'instanceof':
119 1
                            return $left instanceof $right;
120 6
                                case '!==':
121 1
                            return $left !== $right;
122 5
                                case '!=':
123 5
                                case '<>':
124 1
                            return $left != $right;
125 4
                                case '===':
126 1
                            return $left === $right;
127 3
                                case '==':
128 1
                            return $left !== $right;
129 2
                                case '<=>':
130 1
                            return $left <=> $right;
131
                    }
132
133 1
                    throw new InvalidArgumentException('Unable to find the operator.');
134 25
                };
135
    }
136
}
137