| Conditions | 12 | 
| Paths | 43 | 
| Total Lines | 86 | 
| Code Lines | 54 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 1 | ||
| Bugs | 0 | Features | 1 | 
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:
If many parameters/temporary variables are present:
| 1 | <?php  | 
            ||
| 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) { | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 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 | }  |