| Conditions | 11 |
| Paths | 13 |
| Total Lines | 37 |
| 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 |
||
| 137 | private function parseValue($value, $types, \ReflectionProperty $property, $object) |
||
| 138 | { |
||
| 139 | foreach ($types as $type) { |
||
| 140 | foreach ($this->context->getParsers() as $parser) { |
||
| 141 | if ($parser instanceof ValueParserInterface) { |
||
| 142 | if (is_array($value) && strpos($type, '[]') !== false) { |
||
| 143 | |||
| 144 | //support for nesting children |
||
| 145 | //https://github.com/rafrsr/lib-array2object/issues/1 |
||
| 146 | if (count($value) === 1 && is_array(current($value))) { |
||
| 147 | if (array_key_exists(0, current($value))) { |
||
| 148 | $value = current($value); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | $tmpArray = []; |
||
| 153 | foreach ($value as $key => $arrayValue) { |
||
| 154 | $parsedValue = $parser->toObjectValue($arrayValue, str_replace('[]', null, $type), $property, $object); |
||
| 155 | //the annotation [] is used alone to ignore array keys |
||
| 156 | if (in_array('[]', $types, true)) { |
||
| 157 | $tmpArray[] = $parsedValue; |
||
| 158 | } else { |
||
| 159 | $tmpArray[$key] = $parsedValue; |
||
| 160 | } |
||
| 161 | } |
||
| 162 | $value = $tmpArray; |
||
| 163 | } else { |
||
| 164 | $value = $parser->toObjectValue($value, str_replace('[]', null, $type), $property, $object); |
||
| 165 | } |
||
| 166 | } else { |
||
| 167 | throw new \InvalidArgumentException(sprintf('%s is not a valid parser.', get_class($parser))); |
||
| 168 | } |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | return $value; |
||
| 173 | } |
||
| 174 | } |
||
| 175 |
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.