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 |
||
113 | final protected function validate(): void |
||
114 | { |
||
115 | if ($this->errors !== []) { |
||
116 | // already validated |
||
117 | return; |
||
118 | } |
||
119 | |||
120 | $this->errors = []; |
||
121 | |||
122 | foreach ($this->rules as $field => $rules) { |
||
123 | $hasValue = $this->hasValue($field); |
||
124 | $value = $this->getValue($field); |
||
125 | |||
126 | foreach ($this->provider->getRules($rules) as $rule) { |
||
127 | if (!$hasValue && $rule->ignoreEmpty($value) && !$rule->hasConditions()) { |
||
128 | continue; |
||
129 | } |
||
130 | |||
131 | foreach ($rule->getConditions() as $condition) { |
||
132 | if (!$condition->isMet($this, $field, $value)) { |
||
133 | // condition is not met, skipping validation |
||
134 | continue 2; |
||
135 | } |
||
136 | } |
||
137 | |||
138 | if (!$rule->validate($this, $field, $value)) { |
||
139 | // got error, jump to next field |
||
140 | $this->errors[$field] = $rule->getMessage($field, $value); |
||
141 | break; |
||
142 | } |
||
147 |