| Conditions | 14 |
| Paths | 41 |
| Total Lines | 41 |
| 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 /** MicroInjector */ |
||
| 68 | private function loadComponent($options) |
||
| 69 | { |
||
| 70 | if (empty($options['class']) || !class_exists($options['class'])) { |
||
| 71 | return false; |
||
| 72 | } |
||
| 73 | |||
| 74 | $className = $options['class']; |
||
| 75 | $object = null; |
||
|
|
|||
| 76 | |||
| 77 | $options['arguments'] = !empty($options['arguments']) ? $this->buildParams($options['arguments']) : null; |
||
| 78 | $options['property'] = !empty($options['property']) ? $this->buildParams($options['property']) : null; |
||
| 79 | $options['calls'] = !empty($options['calls']) ? $this->buildCalls($options['calls']) : null; |
||
| 80 | |||
| 81 | $object = $this->makeObject($className, $options['arguments']); |
||
| 82 | if (!$object) { |
||
| 83 | return false; |
||
| 84 | } |
||
| 85 | |||
| 86 | if (!empty($options['property'])) { // load properties |
||
| 87 | foreach ($options['property'] as $property => $value) { |
||
| 88 | if (property_exists($object, $property)) { |
||
| 89 | $object->$property = $value; |
||
| 90 | } |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | if (!empty($options['calls'])) { // run methods |
||
| 95 | foreach ($options['calls'] as $method => $arguments) { |
||
| 96 | if (method_exists($object, $method)) { |
||
| 97 | $reflectionMethod = new \ReflectionMethod($className, $method); |
||
| 98 | if ($reflectionMethod->getNumberOfParameters() === 0) { |
||
| 99 | $object->$method(); |
||
| 100 | } else { |
||
| 101 | call_user_func_array([$object, $method], $arguments); |
||
| 102 | } |
||
| 103 | } |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | return true; |
||
| 108 | } |
||
| 109 | |||
| 188 | } |
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.