Total Complexity | 50 |
Total Lines | 318 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 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 |
||
43 | final class ReflectionTypeAdapter extends TypeAdapter implements ObjectConstructorAware |
||
44 | { |
||
45 | use ObjectConstructorAwareTrait; |
||
46 | |||
47 | /** |
||
48 | * @var ConstructorConstructor |
||
49 | */ |
||
50 | private $constructorConstructor; |
||
51 | |||
52 | /** |
||
53 | * @var PropertyCollection |
||
54 | */ |
||
55 | private $properties; |
||
56 | |||
57 | /** |
||
58 | * @var DefaultClassMetadata |
||
59 | */ |
||
60 | private $classMetadata; |
||
61 | |||
62 | /** |
||
63 | * @var Excluder |
||
64 | */ |
||
65 | private $excluder; |
||
66 | |||
67 | /** |
||
68 | * @var TypeAdapterProvider |
||
69 | */ |
||
70 | private $typeAdapterProvider; |
||
71 | |||
72 | /** |
||
73 | * @var null|string |
||
74 | */ |
||
75 | private $classVirtualProperty; |
||
76 | |||
77 | /** |
||
78 | * @var bool |
||
79 | */ |
||
80 | private $skipSerialize; |
||
81 | |||
82 | /** |
||
83 | * @var bool |
||
84 | */ |
||
85 | private $skipDeserialize; |
||
86 | |||
87 | /** |
||
88 | * @var bool |
||
89 | */ |
||
90 | private $hasClassSerializationStrategies; |
||
91 | |||
92 | /** |
||
93 | * @var bool |
||
94 | */ |
||
95 | private $hasPropertySerializationStrategies; |
||
96 | |||
97 | /** |
||
98 | * @var bool |
||
99 | */ |
||
100 | private $hasClassDeserializationStrategies; |
||
101 | |||
102 | /** |
||
103 | * @var bool |
||
104 | */ |
||
105 | private $hasPropertyDeserializationStrategies; |
||
106 | |||
107 | /** |
||
108 | * An memory cache of used type adapters |
||
109 | * |
||
110 | * @var TypeAdapter[] |
||
111 | */ |
||
112 | private $adapters = []; |
||
113 | |||
114 | /** |
||
115 | * A memory cache of read properties |
||
116 | * |
||
117 | * @var Property[] |
||
118 | */ |
||
119 | private $propertyCache = []; |
||
120 | |||
121 | /** |
||
122 | * Constructor |
||
123 | * |
||
124 | * @param ConstructorConstructor $constructorConstructor |
||
125 | * @param ObjectConstructor $objectConstructor |
||
126 | * @param DefaultClassMetadata $classMetadata |
||
127 | * @param Excluder $excluder |
||
128 | * @param TypeAdapterProvider $typeAdapterProvider |
||
129 | * @param null|string $classVirtualProperty |
||
130 | */ |
||
131 | public function __construct( |
||
132 | ConstructorConstructor $constructorConstructor, |
||
133 | ObjectConstructor $objectConstructor, |
||
134 | DefaultClassMetadata $classMetadata, |
||
135 | Excluder $excluder, |
||
136 | TypeAdapterProvider $typeAdapterProvider, |
||
137 | ?string $classVirtualProperty |
||
138 | ) { |
||
139 | $this->constructorConstructor = $constructorConstructor; |
||
140 | $this->objectConstructor = $objectConstructor; |
||
141 | $this->classMetadata = $classMetadata; |
||
142 | $this->excluder = $excluder; |
||
143 | $this->typeAdapterProvider = $typeAdapterProvider; |
||
144 | $this->classVirtualProperty = $classVirtualProperty; |
||
145 | |||
146 | $this->properties = $classMetadata->getPropertyCollection(); |
||
147 | $this->skipSerialize = $classMetadata->skipSerialize(); |
||
148 | $this->skipDeserialize = $classMetadata->skipDeserialize(); |
||
149 | $this->hasClassSerializationStrategies = $this->excluder->hasClassSerializationStrategies(); |
||
150 | $this->hasPropertySerializationStrategies = $this->excluder->hasPropertySerializationStrategies(); |
||
151 | $this->hasClassDeserializationStrategies = $this->excluder->hasClassDeserializationStrategies(); |
||
152 | $this->hasPropertyDeserializationStrategies = $this->excluder->hasPropertyDeserializationStrategies(); |
||
153 | } |
||
154 | /** |
||
155 | * Read the next value, convert it to its type and return it |
||
156 | * |
||
157 | * @param JsonReadable $reader |
||
158 | * @return object |
||
159 | */ |
||
160 | public function read(JsonReadable $reader) |
||
161 | { |
||
162 | if ($this->skipDeserialize) { |
||
163 | $reader->skipValue(); |
||
164 | return null; |
||
165 | } |
||
166 | |||
167 | if ($reader->peek() === JsonToken::NULL) { |
||
168 | $reader->nextNull(); |
||
169 | return null; |
||
170 | } |
||
171 | |||
172 | $object = $this->objectConstructor->construct(); |
||
173 | $exclusionData = $this->hasClassDeserializationStrategies || $this->hasPropertyDeserializationStrategies |
||
174 | ? new DefaultDeserializationExclusionData(clone $object, $reader) |
||
175 | : null; |
||
176 | |||
177 | if ($this->hasClassDeserializationStrategies && $exclusionData) { |
||
178 | $this->excluder->applyClassDeserializationExclusionData($exclusionData); |
||
179 | |||
180 | if ($this->excluder->excludeClassByDeserializationStrategy($this->classMetadata)) { |
||
181 | $reader->skipValue(); |
||
182 | return null; |
||
183 | } |
||
184 | } |
||
185 | |||
186 | if ($this->hasPropertyDeserializationStrategies && $exclusionData) { |
||
187 | $this->excluder->applyPropertyDeserializationExclusionData($exclusionData); |
||
188 | } |
||
189 | |||
190 | $reader->beginObject(); |
||
191 | |||
192 | if ($this->classVirtualProperty !== null) { |
||
193 | $reader->nextName(); |
||
194 | $reader->beginObject(); |
||
195 | } |
||
196 | |||
197 | $usesExisting = $reader->getContext()->usesExistingObject(); |
||
198 | |||
199 | while ($reader->hasNext()) { |
||
200 | $name = $reader->nextName(); |
||
201 | $property = $this->propertyCache[$name] ?? $this->propertyCache[$name] = $this->properties->getBySerializedName($name); |
||
202 | |||
203 | if ($property === null) { |
||
204 | $reader->skipValue(); |
||
205 | continue; |
||
206 | } |
||
207 | |||
208 | $exclusionAnnotation = $property->getAnnotation(ExclusionStrategy::class); |
||
209 | if ($exclusionAnnotation !== null) { |
||
210 | $type = TypeToken::create($exclusionAnnotation->getValue()); |
||
211 | if ($type->isA(PropertySerializationExclusionStrategy::class)) { |
||
212 | /** @var PropertyDeserializationExclusionStrategy $exclusionStrategy */ |
||
213 | $exclusionStrategy = $this->constructorConstructor->get($type)->construct(); |
||
214 | if ($exclusionStrategy instanceof DeserializationExclusionDataAware) { |
||
215 | $propertyExclusionData = new DefaultDeserializationExclusionData(clone $object, $reader); |
||
216 | $exclusionStrategy->setDeserializationExclusionData($propertyExclusionData); |
||
217 | } |
||
218 | |||
219 | if ($exclusionStrategy->skipDeserializingProperty($property)) { |
||
220 | $reader->skipValue(); |
||
221 | continue; |
||
222 | } |
||
223 | } |
||
224 | } |
||
225 | |||
226 | if ( |
||
227 | $property->skipDeserialize() |
||
228 | || ($this->hasPropertyDeserializationStrategies && $this->excluder->excludePropertyByDeserializationStrategy($property)) |
||
229 | ) { |
||
230 | $reader->skipValue(); |
||
231 | continue; |
||
232 | } |
||
233 | |||
234 | $realName = $property->getName(); |
||
235 | |||
236 | $adapter = $this->adapters[$realName] ?? null; |
||
237 | if ($adapter === null) { |
||
238 | /** @var JsonAdapter $jsonAdapterAnnotation */ |
||
239 | $jsonAdapterAnnotation = $property->getAnnotations()->get(JsonAdapter::class); |
||
240 | $adapter = null === $jsonAdapterAnnotation |
||
241 | ? $this->typeAdapterProvider->getAdapter($property->getType()) |
||
242 | : $this->typeAdapterProvider->getAdapterFromAnnotation( |
||
243 | $property->getType(), |
||
244 | $jsonAdapterAnnotation |
||
245 | ); |
||
246 | $this->adapters[$realName] = $adapter; |
||
247 | } |
||
248 | |||
249 | if ($adapter instanceof ObjectConstructorAware && $usesExisting) { |
||
250 | try { |
||
251 | $nestedObject = $property->get($object); |
||
|
|||
252 | } /** @noinspection BadExceptionsProcessingInspection */ catch (TypeError $error) { |
||
253 | // this may occur when attempting to get a nested object that doesn't exist and |
||
254 | // the method return is not nullable. The type error only occurs because we are |
||
255 | // may be calling the getter before data exists. |
||
256 | $nestedObject = null; |
||
257 | } |
||
258 | |||
259 | if ($nestedObject !== null) { |
||
260 | $adapter->setObjectConstructor(new CreateFromInstance($nestedObject)); |
||
261 | } |
||
262 | } |
||
263 | |||
264 | $property->set($object, $adapter->read($reader)); |
||
265 | } |
||
266 | $reader->endObject(); |
||
267 | |||
268 | if ($this->classVirtualProperty !== null) { |
||
269 | $reader->endObject(); |
||
270 | } |
||
271 | |||
272 | return $object; |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Write the value to the writer for the type |
||
277 | * |
||
278 | * @param JsonWritable $writer |
||
279 | * @param object $value |
||
280 | * @return void |
||
281 | */ |
||
282 | public function write(JsonWritable $writer, $value): void |
||
361 | } |
||
362 | } |
||
363 | } |
||
364 |