| Conditions | 11 |
| Paths | 8 |
| Total Lines | 33 |
| Code Lines | 17 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 10 | ||
| Bugs | 1 | 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 |
||
| 116 | private function parseValue($value, $types, \ReflectionProperty $property, $object) |
||
| 117 | { |
||
| 118 | foreach ($types as $type) { |
||
| 119 | |||
| 120 | foreach ($this->context->getParsers() as $parser) { |
||
| 121 | if ($parser instanceof ValueParserInterface) { |
||
| 122 | if (is_array($value) && preg_match('/[\w\d_]+\[\]/', $type)) { |
||
| 123 | |||
| 124 | //support for nesting children |
||
| 125 | //https://github.com/rafrsr/lib-array2object/issues/1 |
||
| 126 | if (count($value) === 1 && is_array(current($value))) { |
||
| 127 | if (array_key_exists(0, current($value))) { |
||
| 128 | $value = current($value); |
||
| 129 | } elseif (in_array('[]', $types, true)) { |
||
| 130 | //https://github.com/rafrsr/lib-array2object/issues/1#issuecomment-228155603 |
||
| 131 | $value = [current($value)]; |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | foreach ($value as $key => &$arrayValue) { |
||
| 136 | $arrayValue = $parser->toObjectValue($arrayValue, str_replace('[]', null, $type), $property, $object); |
||
| 137 | } |
||
| 138 | } else { |
||
| 139 | $value = $parser->toObjectValue($value, str_replace('[]', null, $type), $property, $object); |
||
| 140 | } |
||
| 141 | } else { |
||
| 142 | throw new \InvalidArgumentException(sprintf("%s is not a valid parser.", get_class($parser))); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | return $value; |
||
| 148 | } |
||
| 149 | } |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.