| Conditions | 10 |
| Paths | 9 |
| Total Lines | 29 |
| Code Lines | 15 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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 |
||
| 152 | protected function validate(): void |
||
| 153 | { |
||
| 154 | if ($this->errors !== []) { |
||
| 155 | // already validated |
||
| 156 | return; |
||
| 157 | } |
||
| 158 | |||
| 159 | $this->errors = []; |
||
| 160 | |||
| 161 | foreach ($this->rules as $field => $rules) { |
||
| 162 | $hasValue = $this->hasValue($field); |
||
| 163 | $value = $this->getValue($field); |
||
| 164 | |||
| 165 | foreach ($this->provider->getRules($rules) as $rule) { |
||
| 166 | if (!$hasValue && $rule->ignoreEmpty($value) && !$rule->hasConditions()) { |
||
| 167 | continue; |
||
| 168 | } |
||
| 169 | |||
| 170 | foreach ($rule->getConditions() as $condition) { |
||
| 171 | if (!$condition->isMet($this, $field, $value)) { |
||
| 172 | // condition is not met, skipping validation |
||
| 173 | continue 2; |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | if (!$rule->validate($this, $field, $value)) { |
||
| 178 | // got error, jump to next field |
||
| 179 | $this->errors[$field] = $rule->getMessage($field, $value); |
||
| 180 | break; |
||
| 181 | } |
||
| 186 |