| Total Complexity | 44 |
| Total Lines | 285 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ReflectionTypeAdapter often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ReflectionTypeAdapter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | final class ReflectionTypeAdapter extends TypeAdapter implements ObjectConstructorAware |
||
| 37 | { |
||
| 38 | use ObjectConstructorAwareTrait; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var PropertyCollection |
||
| 42 | */ |
||
| 43 | private $properties; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var ClassMetadata |
||
| 47 | */ |
||
| 48 | private $classMetadata; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var Excluder |
||
| 52 | */ |
||
| 53 | private $excluder; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var TypeAdapterProvider |
||
| 57 | */ |
||
| 58 | private $typeAdapterProvider; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var null|string |
||
| 62 | */ |
||
| 63 | private $classVirtualProperty; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var bool |
||
| 67 | */ |
||
| 68 | private $skipSerialize; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var bool |
||
| 72 | */ |
||
| 73 | private $skipDeserialize; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var bool |
||
| 77 | */ |
||
| 78 | private $hasClassSerializationStrategies; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var bool |
||
| 82 | */ |
||
| 83 | private $hasPropertySerializationStrategies; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var bool |
||
| 87 | */ |
||
| 88 | private $hasClassDeserializationStrategies; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var bool |
||
| 92 | */ |
||
| 93 | private $hasPropertyDeserializationStrategies; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * An memory cache of used type adapters |
||
| 97 | * |
||
| 98 | * @var TypeAdapter[] |
||
| 99 | */ |
||
| 100 | private $adapters = []; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * A memory cache of read properties |
||
| 104 | * |
||
| 105 | * @var Property[] |
||
| 106 | */ |
||
| 107 | private $propertyCache = []; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Constructor |
||
| 111 | * |
||
| 112 | * @param ObjectConstructor $objectConstructor |
||
| 113 | * @param ClassMetadata $classMetadata |
||
| 114 | * @param Excluder $excluder |
||
| 115 | * @param TypeAdapterProvider $typeAdapterProvider |
||
| 116 | * @param null|string $classVirtualProperty |
||
| 117 | */ |
||
| 118 | public function __construct( |
||
| 119 | ObjectConstructor $objectConstructor, |
||
| 120 | ClassMetadata $classMetadata, |
||
| 121 | Excluder $excluder, |
||
| 122 | TypeAdapterProvider $typeAdapterProvider, |
||
| 123 | ?string $classVirtualProperty |
||
| 124 | ) { |
||
| 125 | $this->objectConstructor = $objectConstructor; |
||
| 126 | $this->classMetadata = $classMetadata; |
||
| 127 | $this->excluder = $excluder; |
||
| 128 | $this->typeAdapterProvider = $typeAdapterProvider; |
||
| 129 | $this->classVirtualProperty = $classVirtualProperty; |
||
| 130 | |||
| 131 | $this->properties = $classMetadata->getProperties(); |
||
| 132 | $this->skipSerialize = $classMetadata->skipSerialize(); |
||
| 133 | $this->skipDeserialize = $classMetadata->skipDeserialize(); |
||
| 134 | $this->hasClassSerializationStrategies = $this->excluder->hasClassSerializationStrategies(); |
||
| 135 | $this->hasPropertySerializationStrategies = $this->excluder->hasPropertySerializationStrategies(); |
||
| 136 | $this->hasClassDeserializationStrategies = $this->excluder->hasClassDeserializationStrategies(); |
||
| 137 | $this->hasPropertyDeserializationStrategies = $this->excluder->hasPropertyDeserializationStrategies(); |
||
| 138 | } |
||
| 139 | /** |
||
| 140 | * Read the next value, convert it to its type and return it |
||
| 141 | * |
||
| 142 | * @param JsonReadable $reader |
||
| 143 | * @return object |
||
| 144 | */ |
||
| 145 | public function read(JsonReadable $reader) |
||
| 146 | { |
||
| 147 | if ($this->skipDeserialize) { |
||
| 148 | $reader->skipValue(); |
||
| 149 | return null; |
||
| 150 | } |
||
| 151 | |||
| 152 | if ($reader->peek() === JsonToken::NULL) { |
||
| 153 | $reader->nextNull(); |
||
| 154 | return null; |
||
| 155 | } |
||
| 156 | |||
| 157 | $object = $this->objectConstructor->construct(); |
||
| 158 | $exclusionData = $this->hasClassDeserializationStrategies || $this->hasPropertyDeserializationStrategies |
||
| 159 | ? new DefaultDeserializationExclusionData( |
||
| 160 | $reader->getPayload(), |
||
| 161 | clone $object, |
||
| 162 | $reader->getContext(), |
||
| 163 | function() use ($reader) { return $reader->getPath(); } |
||
| 164 | ) |
||
| 165 | : null; |
||
| 166 | |||
| 167 | if ($this->hasClassDeserializationStrategies && $exclusionData) { |
||
| 168 | $this->excluder->applyClassDeserializationExclusionData($exclusionData); |
||
| 169 | |||
| 170 | if ($this->excluder->excludeClassByDeserializationStrategy($this->classMetadata)) { |
||
| 171 | $reader->skipValue(); |
||
| 172 | return null; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | if ($this->hasPropertyDeserializationStrategies && $exclusionData) { |
||
| 177 | $this->excluder->applyPropertyDeserializationExclusionData($exclusionData); |
||
| 178 | } |
||
| 179 | |||
| 180 | $reader->beginObject(); |
||
| 181 | |||
| 182 | if ($this->classVirtualProperty !== null) { |
||
| 183 | $reader->nextName(); |
||
| 184 | $reader->beginObject(); |
||
| 185 | } |
||
| 186 | |||
| 187 | $usesExisting = $reader->getContext()->usesExistingObject(); |
||
| 188 | |||
| 189 | while ($reader->hasNext()) { |
||
| 190 | $name = $reader->nextName(); |
||
| 191 | $property = $this->propertyCache[$name] ?? $this->propertyCache[$name] = $this->properties->getBySerializedName($name); |
||
| 192 | |||
| 193 | if ($property === null) { |
||
| 194 | $reader->skipValue(); |
||
| 195 | continue; |
||
| 196 | } |
||
| 197 | |||
| 198 | $realName = $property->getName(); |
||
| 199 | |||
| 200 | if ( |
||
| 201 | $property->skipDeserialize() |
||
| 202 | || ( |
||
| 203 | $this->hasPropertyDeserializationStrategies && $exclusionData |
||
| 204 | && $this->excluder->excludePropertyByDeserializationStrategy($property) |
||
| 205 | ) |
||
| 206 | ) { |
||
| 207 | $reader->skipValue(); |
||
| 208 | continue; |
||
| 209 | } |
||
| 210 | |||
| 211 | $adapter = $this->adapters[$realName] ?? null; |
||
| 212 | if ($adapter === null) { |
||
| 213 | /** @var JsonAdapter $jsonAdapterAnnotation */ |
||
| 214 | $jsonAdapterAnnotation = $property->getAnnotations()->get(JsonAdapter::class); |
||
| 215 | $adapter = null === $jsonAdapterAnnotation |
||
| 216 | ? $this->typeAdapterProvider->getAdapter($property->getType()) |
||
| 217 | : $this->typeAdapterProvider->getAdapterFromAnnotation( |
||
| 218 | $property->getType(), |
||
| 219 | $jsonAdapterAnnotation |
||
| 220 | ); |
||
| 221 | $this->adapters[$realName] = $adapter; |
||
| 222 | } |
||
| 223 | |||
| 224 | if ($adapter instanceof ObjectConstructorAware && $usesExisting) { |
||
| 225 | try { |
||
| 226 | $nestedObject = $property->get($object); |
||
| 227 | } /** @noinspection BadExceptionsProcessingInspection */ catch (TypeError $error) { |
||
| 228 | // this may occur when attempting to get a nested object that doesn't exist and |
||
| 229 | // the method return is not nullable. The type error only occurs because we are |
||
| 230 | // may be calling the getter before data exists. |
||
| 231 | $nestedObject = null; |
||
| 232 | } |
||
| 233 | |||
| 234 | if ($nestedObject !== null) { |
||
| 235 | $adapter->setObjectConstructor(new CreateFromInstance($nestedObject)); |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | $property->set($object, $adapter->read($reader)); |
||
| 240 | } |
||
| 241 | $reader->endObject(); |
||
| 242 | |||
| 243 | if ($this->classVirtualProperty !== null) { |
||
| 244 | $reader->endObject(); |
||
| 245 | } |
||
| 246 | |||
| 247 | return $object; |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Write the value to the writer for the type |
||
| 252 | * |
||
| 253 | * @param JsonWritable $writer |
||
| 254 | * @param object $value |
||
| 255 | * @return void |
||
| 256 | */ |
||
| 257 | public function write(JsonWritable $writer, $value): void |
||
| 321 | } |
||
| 322 | } |
||
| 323 | } |
||
| 324 |