Completed
Push — master ( ebff66...d47983 )
by Markus
06:38 queued 02:43
created

CValidate::check()   C

Complexity

Conditions 12
Paths 43

Size

Total Lines 86
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 12
eloc 54
c 1
b 0
f 1
nc 43
nop 3
dl 0
loc 86
rs 5.034

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
namespace Anax\Validate;
3
4
/**
5
 * A helper to validate variables.
6
 *
7
 */
8
class CValidate 
9
{
10
11
    /**
12
     * Properties
13
     *
14
     */
15
    const REGEXP_EMAIL = '/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i';
16
17
18
19
    /**
20
     * Check if a value matches rules or throw exception.
21
     *
22
     * @param mixed   $value  to check
23
     * @param string  $rules  to apply when checking value
24
     * @param boolean $throws set to true to throw exception when check fails
25
     *
26
     * @return boolean true or false
27
     * @throws Exception when check fails
28
     */
29
    public function check($value, $rules, $throws = false) 
30
    {
31
        $tests = [
32
            'fail' => [
33
                'message' => 'Will always fail.', 
34
                'test' => function() {
35
                    return false;
36
                },
37
            ],
38
            'pass' => [
39
                'message' => 'Will always pass.', 
40
                'test' => function() {
41
                    return true;
42
                },
43
            ],
44
            'not_empty' => [
45
                'message' => 'Can not be empty.', 
46
                'test' => function($value) {
47
                    return !empty($value);
48
                },
49
            ],
50
            'not_equal' => [
51
                'message' => 'Not equal.', 
52
                'test' => function($value, $arg) {
53
                    return $value != $arg;
54
                },
55
            ],
56
            'numeric' => [
57
                'message' => 'Must be numeric.', 
58
                'test' => function($value) {
59
                    return is_numeric($value);
60
                }
61
            ],
62
            'int' => [
63
                'message' => 'Must be an integer.', 
64
                'test' => function($value) {
65
                    $int = (int) $value;
66
                    return "$int" == "$value";
67
                }
68
            ],
69
            'range' => [
70
                'message' => 'Out of range.', 
71
                'test' => function($value, $min, $max) {
72
                    return $value >= $min && $value <= $max;
73
                }
74
            ],
75
            'email_adress' => [
76
                'message' => 'Must be an email adress.', 
77
                'test' => function($value) { 
78
                    return preg_match(self::REGEXP_EMAIL, $value) === 1; 
79
                }
80
            ],
81
        ];
82
83
        foreach ($rules as $key => $val) {
0 ignored issues
show
Bug introduced by
The expression $rules of type string is not traversable.
Loading history...
84
            $rule = is_int($key) ? $val : $key;
85
86
            if (!isset($tests[$rule])) {
87
                throw new \Exception("Validation rule does not exist.");
88
            } 
89
    
90
            $param = is_int($key) ? null : $val;
91
            $test  =  $tests[$rule];
92
93
            if (is_callable($test['test'])) {
94
95
                if (isset($param) && is_array($param)) {
96
                    $param = array_merge([$value], $param);
97
                } else if (isset($param)) {
98
                    $param = [$value, $param];
99
                } else {
100
                    $param = [$value];
101
                }
102
103
                if (!call_user_func_array($test['test'], $param)) {
104
                    if ($throws) {
105
                        throw new \Exception($test['message']);
106
                    } else {
107
                        return false;
108
                    }
109
                }
110
            }
111
        } 
112
113
        return true;
114
    }
115
}