| Conditions | 16 |
| Paths | 28 |
| Total Lines | 62 |
| Code Lines | 38 |
| 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 (1 === count($type['params'])) { |
||
| 72 | if ($data[$type['params'][0]]) { |
||
| 73 | $lookupField = $type['params'][0]; |
||
| 74 | |||
| 75 | if (!array_key_exists($lookupField, $data)) { |
||
| 76 | throw new NonVisitableTypeException('Union Discriminator Field \'' . $lookupField . '\' not found in data'); |
||
| 77 | } |
||
| 78 | |||
| 79 | $lkup = $data[$lookupField]; |
||
| 80 | $finalType = [ |
||
| 81 | 'name' => $lkup, |
||
| 82 | 'params' => [], |
||
| 83 | ]; |
||
| 84 | } |
||
| 85 | } elseif (2 === count($type['params'])) { |
||
| 86 | if (is_array($type['params'][1]) && !array_key_exists('name', $type['params'][1])) { |
||
| 87 | $lookupField = $type['params'][0]; |
||
| 88 | $unionMap = $type['params'][1]; |
||
| 89 | |||
| 90 | if (!array_key_exists($lookupField, $data)) { |
||
| 91 | throw new NonVisitableTypeException('Union Discriminator Field \'' . $lookupField . '\' not found in data'); |
||
| 92 | } |
||
| 93 | |||
| 94 | $lkup = $data[$lookupField]; |
||
| 95 | if (!empty($unionMap)) { |
||
| 96 | if (array_key_exists($lkup, $unionMap)) { |
||
| 97 | $finalType = [ |
||
| 98 | 'name' => $unionMap[$lkup], |
||
| 99 | 'params' => [], |
||
| 100 | ]; |
||
| 101 | } else { |
||
| 102 | throw new NonVisitableTypeException('Union Discriminator Map does not contain key \'' . $lkup . '\''); |
||
| 103 | } |
||
| 104 | } else { |
||
| 105 | $finalType = [ |
||
| 106 | 'name' => $lkup, |
||
| 107 | 'params' => [], |
||
| 108 | ]; |
||
| 109 | } |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | if (null !== $finalType && null !== $finalType['name']) { |
||
| 114 | return $context->getNavigator()->accept($data, $finalType); |
||
| 115 | } else { |
||
| 116 | foreach ($type['params'] as $possibleType) { |
||
| 117 | $finalType = null; |
||
| 118 | |||
| 119 | if ($this->isPrimitiveType($possibleType['name']) && $this->testPrimitive($data, $possibleType['name'], $context->getFormat())) { |
||
| 120 | return $context->getNavigator()->accept($data, $possibleType); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | return null; |
||
| 126 | } |
||
| 172 |