| Conditions | 19 |
| Paths | 145 |
| Total Lines | 68 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| 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 |
||
| 74 | public function deserializeUnion(DeserializationVisitorInterface $visitor, mixed $data, array $type, DeserializationContext $context): mixed |
||
| 75 | { |
||
| 76 | if ($data instanceof \SimpleXMLElement) { |
||
| 77 | throw new RuntimeException('XML deserialisation into union types is not supported yet.'); |
||
| 78 | } |
||
| 79 | |||
| 80 | $alternativeName = null; |
||
| 81 | |||
| 82 | foreach ($type['params'] as $possibleType) { |
||
| 83 | $propertyMetadata = $context->getMetadataStack()->top(); |
||
| 84 | $finalType = null; |
||
| 85 | if (null !== $propertyMetadata->unionDiscriminatorField) { |
||
| 86 | if (!array_key_exists($propertyMetadata->unionDiscriminatorField, $data)) { |
||
| 87 | throw new NonVisitableTypeException("Union Discriminator Field '$propertyMetadata->unionDiscriminatorField' not found in data"); |
||
| 88 | } |
||
| 89 | |||
| 90 | $lkup = $data[$propertyMetadata->unionDiscriminatorField]; |
||
| 91 | if (!empty($propertyMetadata->unionDiscriminatorMap)) { |
||
| 92 | if (array_key_exists($lkup, $propertyMetadata->unionDiscriminatorMap)) { |
||
| 93 | $finalType = [ |
||
| 94 | 'name' => $propertyMetadata->unionDiscriminatorMap[$lkup], |
||
| 95 | 'params' => [], |
||
| 96 | ]; |
||
| 97 | } else { |
||
| 98 | throw new NonVisitableTypeException("Union Discriminator Map does not contain key '$lkup'"); |
||
| 99 | } |
||
| 100 | } else { |
||
| 101 | $finalType = [ |
||
| 102 | 'name' => $lkup, |
||
| 103 | 'params' => [], |
||
| 104 | ]; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | if (null !== $finalType && null !== $finalType['name']) { |
||
| 109 | return $context->getNavigator()->accept($data, $finalType); |
||
| 110 | } else { |
||
| 111 | try { |
||
| 112 | $previousVisitorRequireSetting = $visitor->getRequireAllRequiredProperties(); |
||
| 113 | if ($this->requireAllProperties) { |
||
| 114 | $visitor->setRequireAllRequiredProperties($this->requireAllProperties); |
||
| 115 | } |
||
| 116 | |||
| 117 | if ($this->isPrimitiveType($possibleType['name']) && (is_array($data) || !$this->testPrimitive($data, $possibleType['name'], $context->getFormat()))) { |
||
| 118 | continue; |
||
| 119 | } |
||
| 120 | |||
| 121 | $accept = $context->getNavigator()->accept($data, $possibleType); |
||
| 122 | if ($this->requireAllProperties) { |
||
| 123 | $visitor->setRequireAllRequiredProperties($previousVisitorRequireSetting); |
||
| 124 | } |
||
| 125 | |||
| 126 | return $accept; |
||
| 127 | } catch (NonVisitableTypeException $e) { |
||
| 128 | continue; |
||
| 129 | } catch (PropertyMissingException $e) { |
||
| 130 | continue; |
||
| 131 | } catch (NonStringCastableTypeException $e) { |
||
| 132 | continue; |
||
| 133 | } catch (NonIntCastableTypeException $e) { |
||
| 134 | continue; |
||
| 135 | } catch (NonFloatCastableTypeException $e) { |
||
| 136 | continue; |
||
| 137 | } |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | return null; |
||
| 142 | } |
||
| 192 |