| Conditions | 10 |
| Paths | 19 |
| Total Lines | 41 |
| Code Lines | 22 |
| 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 |
||
| 34 | } |
||
| 35 | |||
| 36 | 7 | $options = Options::fromArray($options); |
|
| 37 | |||
| 38 | 6 | $arguments = self::marshalArguments($options, $reflectionClass); |
|
| 39 | |||
| 40 | 5 | return new $className(...$arguments); |
|
| 41 | } |
||
| 42 | |||
| 43 | 6 | protected static function marshalArguments(Options $options, ReflectionClass $reflectionClass) : array |
|
| 44 | { |
||
| 45 | 6 | if (!$reflectionClass->hasMethod('__construct')) { |
|
| 46 | 1 | return []; |
|
| 47 | } |
||
| 48 | |||
| 49 | 5 | $arguments = []; |
|
| 50 | |||
| 51 | 5 | $className = $reflectionClass->getName(); |
|
| 52 | 5 | $constructorParameters = $reflectionClass->getConstructor()->getParameters(); |
|
| 53 | |||
| 54 | 5 | foreach ($constructorParameters as $parameter) { |
|
| 55 | /* @var $parameter \BetterReflection\Reflection\ReflectionParameter */ |
||
| 56 | 5 | $parameterName = $parameter->getName(); |
|
| 57 | |||
| 58 | 5 | $argument = null; |
|
| 59 | try { |
||
| 60 | 5 | $argument = $options->get($parameterName); |
|
| 61 | |||
| 62 | 4 | if (null !== ($parameterType = $parameter->getType())) { |
|
| 63 | 4 | $parameterTypeObject = $parameterType->getTypeObject(); |
|
| 64 | |||
| 65 | 4 | if (is_array($argument) && $parameterTypeObject instanceof Object_) { |
|
| 66 | 4 | $argument = static::create((string) $parameterTypeObject->getFqsen(), $argument); |
|
| 67 | } |
||
| 68 | } |
||
| 69 | 4 | } catch (OptionNotSetException $ex) { |
|
| 70 | 4 | if (!$parameter->isOptional()) { |
|
| 71 | 1 | throw InvalidOptionsException::forMissingMandatoryParameter($className, $parameterName); |
|
| 72 | } |
||
| 73 | |||
| 74 | 3 | $argument = $parameter->getDefaultValue(); |
|
| 75 | } |
||
| 83 |