Passed
Push — master ( eb3d86...848e05 )
by Smoren
02:38
created

RuleHelper::evaluate()   D

Complexity

Conditions 23
Paths 21

Size

Total Lines 56
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 23
eloc 42
c 1
b 0
f 0
nc 21
nop 3
dl 0
loc 56
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
namespace Smoren\Schemator\Util;
4
5
/**
6
 * @internal
7
 */
8
class RuleHelper
9
{
10
    /**
11
     * Checks rule for value
12
     * @param mixed $value value to check
13
     * @param string $rule rule for checking
14
     * @param array $args arguments for rule
15
     * @return bool
16
     */
17
    public static function evaluate($value, string $rule, array $args): bool
18
    {
19
        switch($rule) {
20
            case '=':
21
                if((string)$value === (string)$args[0]) {
22
                    return true;
23
                }
24
                break;
25
            case '!=':
26
                if((string)$value !== (string)$args[0]) {
27
                    return true;
28
                }
29
                break;
30
            case '>':
31
                if($value > $args[0]) {
32
                    return true;
33
                }
34
                break;
35
            case '>=':
36
                if($value >= $args[0]) {
37
                    return true;
38
                }
39
                break;
40
            case '<':
41
                if($value < $args[0]) {
42
                    return true;
43
                }
44
                break;
45
            case '<=':
46
                if($value <= $args[0]) {
47
                    return true;
48
                }
49
                break;
50
            case 'between':
51
                if($value >= $args[0] && $value <= $args[1]) {
52
                    return true;
53
                }
54
                break;
55
            case 'in':
56
                if(in_array($value, $args[0])) {
57
                    return true;
58
                }
59
                break;
60
            case 'not in':
61
                if(!in_array($value, $args[0])) {
62
                    return true;
63
                }
64
                break;
65
            case 'between strict':
66
                if($value > $args[0] && $value < $args[1]) {
67
                    return true;
68
                }
69
                break;
70
        }
71
72
        return false;
73
    }
74
}
75