| Conditions | 13 |
| Paths | 20 |
| Total Lines | 47 |
| Code Lines | 28 |
| 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 | foreach ($type['params'] as $possibleType) { |
||
| 71 | $finalType = null; |
||
| 72 | |||
| 73 | if (!$context->getMetadataStack()->isEmpty()) { |
||
| 74 | $propertyMetadata = $context->getMetadataStack()->top(); |
||
| 75 | if (null !== $propertyMetadata->unionDiscriminatorField) { |
||
| 76 | if (!array_key_exists($propertyMetadata->unionDiscriminatorField, $data)) { |
||
| 77 | throw new NonVisitableTypeException('Union Discriminator Field \'' . $propertyMetadata->unionDiscriminatorField . '\' not found in data'); |
||
| 78 | } |
||
| 79 | |||
| 80 | $lkup = $data[$propertyMetadata->unionDiscriminatorField]; |
||
| 81 | if (!empty($propertyMetadata->unionDiscriminatorMap)) { |
||
| 82 | if (array_key_exists($lkup, $propertyMetadata->unionDiscriminatorMap)) { |
||
| 83 | $finalType = [ |
||
| 84 | 'name' => $propertyMetadata->unionDiscriminatorMap[$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 | if ($this->isPrimitiveType($possibleType['name']) && $this->testPrimitive($data, $possibleType['name'], $context->getFormat())) { |
||
| 104 | return $context->getNavigator()->accept($data, $possibleType); |
||
| 105 | } |
||
| 106 | } |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | return null; |
||
| 111 | } |
||
| 157 |