| Conditions | 10 |
| Paths | 29 |
| Total Lines | 41 |
| Code Lines | 21 |
| 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 |
||
| 31 | public function validate($constraints) |
||
| 32 | { |
||
| 33 | $violations = new ConstraintViolationList(); |
||
| 34 | |||
| 35 | if (empty($constraints)) { |
||
| 36 | return $violations; |
||
| 37 | } |
||
| 38 | |||
| 39 | if (is_string($constraints)) { |
||
| 40 | $constraints = $this->getConstraintsFromString($constraints); |
||
| 41 | } |
||
| 42 | |||
| 43 | if (!is_array($constraints)) { |
||
| 44 | throw new InvalidArgumentException('Constraints: Format not supported'); |
||
| 45 | } |
||
| 46 | |||
| 47 | $validator = Validation::createValidator(); |
||
| 48 | |||
| 49 | foreach ($constraints as $rule => $options) { |
||
| 50 | if (is_string($options) && $options == 'required') { |
||
| 51 | $rule = $options; |
||
| 52 | $options = true; |
||
| 53 | } |
||
| 54 | |||
| 55 | $errors = $validator->validate($rule, [ |
||
| 56 | new NotBlank(), |
||
| 57 | new Choice(self::ALLOWED_RULES), |
||
| 58 | ]); |
||
| 59 | |||
| 60 | if (count($errors) === 0) { |
||
| 61 | $errors = $this->validateRule($rule, $options); |
||
| 62 | } |
||
| 63 | |||
| 64 | if (count($errors) !== 0) { |
||
| 65 | foreach ($errors as $error) { |
||
| 66 | $violations->add($error); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | return $violations; |
||
| 72 | } |
||
| 141 |