| Conditions | 11 |
| Paths | 48 |
| Total Lines | 36 |
| Code Lines | 24 |
| 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 declare(strict_types=1); |
||
| 43 | private function getParameterValueFromRequest(\ReflectionParameter $parameter) { |
||
| 44 | $paramName = $parameter->getName(); |
||
| 45 | $type = $parameter->getType(); |
||
| 46 | if ($type === null) { |
||
| 47 | $typeName = null; |
||
| 48 | } else { |
||
| 49 | \assert($type instanceof \ReflectionNamedType); // we don't use union types introduced in PHP8 |
||
| 50 | $typeName = $type->getName(); |
||
| 51 | } |
||
| 52 | |||
| 53 | if ($typeName === 'array') { |
||
| 54 | $parameterValue = $this->getRepeatedParam($paramName); |
||
| 55 | } else { |
||
| 56 | $parameterValue = $this->request->getParam($paramName); |
||
| 57 | } |
||
| 58 | |||
| 59 | if (\array_key_exists($paramName, $this->customFilters)) { |
||
| 60 | $parameterValue = $this->customFilters[$paramName]($parameterValue); |
||
| 61 | } |
||
| 62 | |||
| 63 | if ($parameterValue === null) { |
||
| 64 | if ($parameter->isOptional()) { |
||
| 65 | $parameterValue = $parameter->getDefaultValue(); |
||
| 66 | } elseif (!$parameter->allowsNull()) { |
||
| 67 | throw new RequestParameterExtractorException("Required parameter '$paramName' missing"); |
||
| 68 | } |
||
| 69 | } else { |
||
| 70 | // cast non-null values to requested type |
||
| 71 | if ($typeName === 'int' || $typeName === 'integer') { |
||
| 72 | $parameterValue = (int)$parameterValue; |
||
| 73 | } elseif ($typeName === 'bool' || $typeName === 'boolean') { |
||
| 74 | $parameterValue = \filter_var($parameterValue, FILTER_VALIDATE_BOOLEAN); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | return $parameterValue; |
||
| 79 | } |
||
| 126 |