| Conditions | 12 |
| Paths | 13 |
| Total Lines | 26 |
| Code Lines | 16 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 21 | public function validate(mixed $value, object $rule, ?ValidationContext $context = null): Result |
||
| 22 | { |
||
| 23 | if (!$rule instanceof Number) { |
||
| 24 | throw new UnexpectedRuleException(Number::class, $rule); |
||
| 25 | } |
||
| 26 | |||
| 27 | $result = new Result(); |
||
| 28 | |||
| 29 | if (is_bool($value) || !is_scalar($value)) { |
||
| 30 | $message = $rule->asInteger ? 'Value must be an integer.' : 'Value must be a number.'; |
||
| 31 | $result->addError($message, ['value' => $value]); |
||
| 32 | return $result; |
||
| 33 | } |
||
| 34 | |||
| 35 | $pattern = $rule->asInteger ? $rule->integerPattern : $rule->numberPattern; |
||
| 36 | |||
| 37 | if (!preg_match($pattern, NumericHelper::normalize($value))) { |
||
| 38 | $message = $rule->asInteger ? 'Value must be an integer.' : 'Value must be a number.'; |
||
| 39 | $result->addError($message, ['value' => $value]); |
||
| 40 | } elseif ($rule->min !== null && $value < $rule->min) { |
||
| 41 | $result->addError($rule->tooSmallMessage, ['min' => $rule->min]); |
||
| 42 | } elseif ($rule->max !== null && $value > $rule->max) { |
||
| 43 | $result->addError($rule->tooBigMessage, ['max' => $rule->max]); |
||
| 44 | } |
||
| 45 | |||
| 46 | return $result; |
||
| 47 | } |
||
| 49 |