| Conditions | 27 |
| Paths | 4358 |
| Total Lines | 113 |
| Code Lines | 66 |
| 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 |
||
| 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 | } |
||
| 364 |