| Conditions | 7 |
| Paths | 6 |
| Total Lines | 53 |
| Code Lines | 38 |
| 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 |
||
| 51 | protected function addValidationMethod($parameterName, array $unionValues) |
||
| 52 | { |
||
| 53 | $method = new PhpMethod('temp'); |
||
| 54 | $rules = clone $this->getRules(); |
||
| 55 | $rules->setMethod($method); |
||
| 56 | |||
| 57 | // gather validation rules |
||
| 58 | foreach ($unionValues as $unionValue) { |
||
| 59 | $rules->setAttribute(new StructAttribute($this->getGenerator(), 'any', $unionValue)); |
||
| 60 | $rules->applyRules('value'); |
||
| 61 | } |
||
| 62 | |||
| 63 | // adapt content, remove duplicated rules |
||
| 64 | // duplicated rules is base don the fact that validation rules are composed by 4 lines so we check existing rule every 4-line block of text |
||
| 65 | $exceptions = 0; |
||
| 66 | $exceptionsTests = []; |
||
| 67 | $exceptionsArray = []; |
||
| 68 | $children = []; |
||
| 69 | $methodChildren = $method->getChildren(); |
||
| 70 | $chilrenCount = count($methodChildren); |
||
| 71 | $existingValidationRules = []; |
||
| 72 | for ($i = 0;$i < $chilrenCount;$i += 4) { |
||
| 73 | $validationRules = array_slice($methodChildren, ((int) $i / 4) * 4, 4); |
||
| 74 | if (!in_array($validationRules, $existingValidationRules)) { |
||
| 75 | foreach ($validationRules as $validationRule) { |
||
| 76 | if (is_string($validationRule) && false !== strpos($validationRule, 'throw new')) { |
||
| 77 | $exceptionName = sprintf('$exception%d', $exceptions++); |
||
| 78 | $validationRule = str_replace('throw new', sprintf('%s = new', $exceptionName), $validationRule); |
||
| 79 | $exceptionsTests[] = sprintf('isset(%s)', $exceptionName); |
||
| 80 | $exceptionsArray[] = $exceptionName; |
||
| 81 | } |
||
| 82 | $children[] = $validationRule; |
||
| 83 | } |
||
| 84 | } |
||
| 85 | $existingValidationRules[] = $validationRules; |
||
| 86 | } |
||
| 87 | |||
| 88 | // populate final validation method |
||
| 89 | $method = new PhpMethod($this->getValidationMethodName($parameterName), [ |
||
| 90 | new PhpFunctionParameter('value', PhpFunctionParameter::NO_VALUE), |
||
| 91 | ], PhpMethod::ACCESS_PUBLIC, false, true); |
||
| 92 | $method->addChild('$message = \'\';'); |
||
| 93 | array_walk($children, [ |
||
| 94 | $method, |
||
| 95 | 'addChild', |
||
| 96 | ]); |
||
| 97 | $method |
||
| 98 | ->addChild(sprintf('if (%s) {', implode(' && ', $exceptionsTests))) |
||
| 99 | ->addChild($method->getIndentedString(sprintf('$message = sprintf("The value %%s does not match any of the union rules: %s. See following errors:\n%%s", var_export($value, true), implode("\n", array_map(function(\InvalidArgumentException $e) { return sprintf(\' - %%s\', $e->getMessage()); }, [%s])));', implode(', ', $unionValues), implode(', ', $exceptionsArray)), 1)) |
||
| 100 | ->addChild('}') |
||
| 101 | ->addChild(sprintf('unset(%s);', implode(', ', $exceptionsArray))) |
||
| 102 | ->addChild('return $message;'); |
||
| 103 | $this->getMethods()->add($method); |
||
| 104 | } |
||
| 124 |