Passed
Push — master ( 5f88dd...10f6bc )
by Smoren
12:50
created

RuleHelper::evaluate()   D

Complexity

Conditions 23
Paths 21

Size

Total Lines 68
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 23
eloc 44
nc 21
nop 3
dl 0
loc 68
rs 4.1666
c 0
b 0
f 0

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 Smoren\Schemator\Helpers;
4
5
/**
6
 * @internal
7
 */
8
class RuleHelper
9
{
10
    /**
11
     * Checks rule for value.
12
     *
13
     * @param mixed $value value to check
14
     * @param string $rule rule for checking
15
     * @param array<mixed> $args arguments for rule
16
     *
17
     * @return bool
18
     */
19
    public static function evaluate($value, string $rule, array $args): bool
20
    {
21
        switch ($rule) {
22
            case '=':
23
                /**
24
                 * @var scalar $lhs
25
                 * @var scalar $rhs
26
                 */
27
                [$lhs, $rhs] = [$value, $args[0]];
28
                if (strval($lhs) === strval($rhs)) {
29
                    return true;
30
                }
31
                break;
32
            case '!=':
33
                /**
34
                 * @var scalar $lhs
35
                 * @var scalar $rhs
36
                 */
37
                [$lhs, $rhs] = [$value, $args[0]];
38
                if (strval($lhs) !== strval($rhs)) {
39
                    return true;
40
                }
41
                break;
42
            case '>':
43
                if ($value > $args[0]) {
44
                    return true;
45
                }
46
                break;
47
            case '>=':
48
                if ($value >= $args[0]) {
49
                    return true;
50
                }
51
                break;
52
            case '<':
53
                if ($value < $args[0]) {
54
                    return true;
55
                }
56
                break;
57
            case '<=':
58
                if ($value <= $args[0]) {
59
                    return true;
60
                }
61
                break;
62
            case 'between':
63
                if ($value >= $args[0] && $value <= $args[1]) {
64
                    return true;
65
                }
66
                break;
67
            case 'between strict':
68
                if ($value > $args[0] && $value < $args[1]) {
69
                    return true;
70
                }
71
                break;
72
            case 'in':
73
                /** @var array{array<mixed>} $args */
74
                if (in_array($value, $args[0])) {
75
                    return true;
76
                }
77
                break;
78
            case 'not in':
79
                /** @var array{array<mixed>} $args */
80
                if (!in_array($value, $args[0])) {
81
                    return true;
82
                }
83
                break;
84
        }
85
86
        return false;
87
    }
88
}
89