| Conditions | 6 |
| Paths | 9 |
| Total Lines | 52 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 55 | public function createCommand(array $arguments, ArgumentsProcessor $argumentsProcessor) |
||
| 56 | { |
||
| 57 | $classReflection = new \ReflectionClass($this->commandClass); |
||
| 58 | |||
| 59 | $inputArguments = $argumentsProcessor->process($arguments); |
||
| 60 | |||
| 61 | $arrayDivide = function (array $array, callable $predicate) { |
||
| 62 | return [ |
||
| 63 | array_filter($array, function ($value, $key) use ($predicate) { return $predicate($key, $value); }, ARRAY_FILTER_USE_BOTH), |
||
| 64 | array_filter($array, function ($value, $key) use ($predicate) { return !$predicate($key, $value); }, ARRAY_FILTER_USE_BOTH), |
||
| 65 | ]; |
||
| 66 | }; |
||
| 67 | |||
| 68 | $isKeyInteger = function ($key) { |
||
| 69 | return is_integer($key); |
||
| 70 | }; |
||
| 71 | |||
| 72 | list($orderedArguments, $namedArguments) = $arrayDivide($inputArguments, $isKeyInteger); |
||
| 73 | |||
| 74 | $constructorArguments = []; |
||
| 75 | |||
| 76 | foreach ($this->parameters() as $commandArgument) { |
||
| 77 | $argument = array_key_exists($commandArgument->getName(), $namedArguments) ? |
||
| 78 | $namedArguments[$commandArgument->getName()] : |
||
| 79 | array_shift($orderedArguments); |
||
| 80 | |||
| 81 | if (null === $argument) { |
||
| 82 | throw new MissingCommandArgument( |
||
| 83 | sprintf("Missing argument %s for '%s' command.", $commandArgument->getPosition() + 1, $this->commandName) |
||
| 84 | ); |
||
| 85 | } |
||
| 86 | |||
| 87 | if (null !== $commandArgument->getClass()) { |
||
| 88 | $argumentClass = $commandArgument->getClass()->getName(); |
||
| 89 | |||
| 90 | if (!$argument instanceof $argumentClass) { |
||
| 91 | throw new InvalidCommandArgument( |
||
| 92 | sprintf( |
||
| 93 | "Invalid argument for '%s' command. Expected parameter %s to be instance of '%s'.", |
||
| 94 | $this->commandName, |
||
| 95 | $commandArgument->getPosition() + 1, |
||
| 96 | $argumentClass |
||
| 97 | ) |
||
| 98 | ); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | $constructorArguments[$commandArgument->getPosition()] = $argument; |
||
| 103 | } |
||
| 104 | |||
| 105 | return $classReflection->newInstanceArgs($constructorArguments); |
||
| 106 | } |
||
| 107 | } |
||
| 108 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.