| Conditions | 13 |
| Paths | 19 |
| Total Lines | 48 |
| Code Lines | 29 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 64 | public function deserializeUnion(DeserializationVisitorInterface $visitor, mixed $data, array $type, DeserializationContext $context): mixed |
||
| 65 | { |
||
| 66 | if ($data instanceof \SimpleXMLElement) { |
||
| 67 | throw new RuntimeException('XML deserialisation into union types is not supported yet.'); |
||
| 68 | } |
||
| 69 | |||
| 70 | $finalType = null; |
||
| 71 | if (2 === count($type['params'])) { |
||
| 72 | if (!is_array($type['params'][0]) || !array_key_exists('name', $type['params'][0])) { |
||
| 73 | $lookupField = $type['params'][0]; |
||
| 74 | $unionMap = $type['params'][1]; |
||
| 75 | |||
| 76 | if (!array_key_exists($lookupField, $data)) { |
||
| 77 | throw new NonVisitableTypeException('Union Discriminator Field \'' . $lookupField . '\' not found in data'); |
||
| 78 | } |
||
| 79 | |||
| 80 | $lkup = $data[$lookupField]; |
||
| 81 | if (!empty($unionMap)) { |
||
| 82 | if (array_key_exists($lkup, $unionMap)) { |
||
| 83 | $finalType = [ |
||
| 84 | 'name' => $unionMap[$lkup], |
||
| 85 | 'params' => [], |
||
| 86 | ]; |
||
| 87 | } else { |
||
| 88 | throw new NonVisitableTypeException('Union Discriminator Map does not contain key \'' . $lkup . '\''); |
||
| 89 | } |
||
| 90 | } else { |
||
| 91 | $finalType = [ |
||
| 92 | 'name' => $lkup, |
||
| 93 | 'params' => [], |
||
| 94 | ]; |
||
| 95 | } |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | if (null !== $finalType && null !== $finalType['name']) { |
||
| 100 | return $context->getNavigator()->accept($data, $finalType); |
||
| 101 | } else { |
||
| 102 | foreach ($type['params'] as $possibleType) { |
||
| 103 | $finalType = null; |
||
| 104 | |||
| 105 | if ($this->isPrimitiveType($possibleType['name']) && $this->testPrimitive($data, $possibleType['name'], $context->getFormat())) { |
||
| 106 | return $context->getNavigator()->accept($data, $possibleType); |
||
| 107 | } |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | return null; |
||
| 112 | } |
||
| 158 |