| Conditions | 13 |
| Paths | 55 |
| Total Lines | 54 |
| Code Lines | 27 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 30 |
| CRAP Score | 13.0056 |
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 |
||
| 81 | 15 | public function validateItem($key, $input) |
|
| 82 | { |
||
| 83 | // w/out any rules element is valid |
||
| 84 | 15 | if (!isset($this->validators[$key])) { |
|
| 85 | return true; |
||
| 86 | } |
||
| 87 | |||
| 88 | 15 | $validators = $this->validators[$key]; |
|
| 89 | |||
| 90 | // check be validators |
||
| 91 | // extract input from ... |
||
| 92 | 15 | if (is_array($input) && isset($input[$key])) { |
|
| 93 | // array |
||
| 94 | 12 | $value = $input[$key]; |
|
| 95 | 15 | } elseif (is_object($input) && isset($input->{$key})) { |
|
| 96 | // object |
||
| 97 | 1 | $value = $input->{$key}; |
|
| 98 | 1 | } else { |
|
| 99 | // ... oh, not exists key |
||
| 100 | // check chains for required |
||
| 101 | 4 | $required = false; |
|
| 102 | 4 | foreach ($validators as $validator) { |
|
| 103 | /* @var Validator $validator */ |
||
| 104 | 4 | if ($validator->isRequired()) { |
|
| 105 | 3 | $required = true; |
|
| 106 | 3 | break; |
|
| 107 | } |
||
| 108 | 4 | } |
|
| 109 | |||
| 110 | 4 | if ($required) { |
|
| 111 | 3 | $value = ''; |
|
| 112 | 3 | } else { |
|
| 113 | 2 | return true; |
|
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | // run validators chain |
||
| 118 | 15 | foreach ($validators as $validator) { |
|
| 119 | /* @var Validator $validator */ |
||
| 120 | 15 | if (!$validator->getName()) { |
|
| 121 | // setup field name as property name |
||
| 122 | 15 | $validator->setName(ucfirst($key)); |
|
| 123 | 15 | } |
|
| 124 | |||
| 125 | 15 | if (!$validator->validate($value)) { |
|
| 126 | 9 | if (!isset($this->errors[$key])) { |
|
| 127 | 9 | $this->errors[$key] = array(); |
|
| 128 | 9 | } |
|
| 129 | 9 | $this->errors[$key][] = $validator->getError(); |
|
| 130 | 9 | return false; |
|
| 131 | } |
||
| 132 | 7 | } |
|
| 133 | 6 | return true; |
|
| 134 | } |
||
| 135 | |||
| 163 |