| Total Complexity | 64 |
| Total Lines | 317 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
Complex classes like ApieObjectAccessNormalizer 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 ApieObjectAccessNormalizer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class ApieObjectAccessNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface |
||
| 31 | { |
||
| 32 | use SerializerAwareTrait; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var ObjectAccessInterface |
||
| 36 | */ |
||
| 37 | private $objectAccess; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var NameConverterInterface|AdvancedNameConverterInterface |
||
| 41 | */ |
||
| 42 | private $nameConverter; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var ClassMetadataFactoryInterface|null |
||
| 46 | */ |
||
| 47 | private $classMetadataFactory; |
||
| 48 | |||
| 49 | public function __construct( |
||
| 50 | ObjectAccessInterface $objectAccess = null, |
||
| 51 | NameConverterInterface $nameConverter = null, |
||
| 52 | ClassMetadataFactoryInterface $classMetadataFactory = null |
||
| 53 | ) { |
||
| 54 | $this->objectAccess = $objectAccess ?? new ObjectAccess(); |
||
| 55 | $this->nameConverter = $nameConverter ?? new NullNameConverter(); |
||
| 56 | $this->classMetadataFactory = $classMetadataFactory; |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * {@inheritDoc} |
||
| 61 | */ |
||
| 62 | public function denormalize($data, $type, $format = null, array $context = []) |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Try to convert a field value to the wanted Type. |
||
| 136 | * |
||
| 137 | * @internal |
||
| 138 | * |
||
| 139 | * @param array $data |
||
| 140 | * @param string $denormalizedFieldName |
||
| 141 | * @param string $fieldName |
||
| 142 | * @param Type $type |
||
| 143 | * @param string|null $format |
||
| 144 | * @param array $context |
||
| 145 | * @return array|bool|float|int|string|null |
||
| 146 | */ |
||
| 147 | public function denormalizeType(array $data, string $denormalizedFieldName, string $fieldName, Type $type, ?string $format = null, array $context = []) |
||
| 189 | } |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Try to get create a new instance of this class from the input $data we retrieve. |
||
| 194 | * |
||
| 195 | * @param array $data |
||
| 196 | * @param string $type |
||
| 197 | * @param ObjectAccessInterface $objectAccess |
||
| 198 | * @param string|null $format |
||
| 199 | * @param array $context |
||
| 200 | * @return object |
||
| 201 | */ |
||
| 202 | private function instantiate(array $data, string $type, ObjectAccessInterface $objectAccess, ?string $format = null, array $context = []) |
||
| 203 | { |
||
| 204 | $reflectionClass = new ReflectionClass($type); |
||
| 205 | $argumentTypes = $objectAccess->getConstructorArguments($reflectionClass); |
||
| 206 | $errors = new ErrorBag($context['key_prefix']); |
||
| 207 | $parsedArguments = []; |
||
| 208 | foreach ($argumentTypes as $denormalizedFieldName => $argumentType) { |
||
| 209 | try { |
||
| 210 | $fieldName = $this->nameConverter->normalize($denormalizedFieldName, $type, $format, $context); |
||
| 211 | if (!array_key_exists($fieldName, $data)) { |
||
| 212 | $constructor = $reflectionClass->getConstructor(); |
||
| 213 | foreach ($constructor->getParameters() as $parameter) { |
||
| 214 | if ($parameter->name === $denormalizedFieldName && $parameter->isDefaultValueAvailable()) { |
||
| 215 | $parsedArguments[] = $parameter->getDefaultValue(); |
||
| 216 | continue(2); |
||
| 217 | } |
||
| 218 | } |
||
| 219 | throw new MissingConstructorArgumentsException($fieldName . ' is required'); |
||
| 220 | } |
||
| 221 | if ($argumentType) { |
||
| 222 | $parsedArguments[] = $this->denormalizeType($data, $denormalizedFieldName, $fieldName, $argumentType, $format, $context); |
||
| 223 | } else { |
||
| 224 | $parsedArguments[] = $data[$fieldName]; |
||
| 225 | } |
||
| 226 | } catch (Throwable $throwable) { |
||
| 227 | $errors->addThrowable($denormalizedFieldName, $throwable); |
||
| 228 | } |
||
| 229 | } |
||
| 230 | if ($errors->hasErrors()) { |
||
| 231 | throw new ValidationException($errors); |
||
| 232 | } |
||
| 233 | return $objectAccess->instantiate($reflectionClass, $parsedArguments); |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * {@inheritDoc} |
||
| 238 | */ |
||
| 239 | public function supportsDenormalization($data, $type, $format = null) |
||
| 240 | { |
||
| 241 | return (is_array($data) || $data instanceof stdClass) && class_exists($type); |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * {@inheritDoc} |
||
| 246 | */ |
||
| 247 | public function normalize($object, $format = null, array $context = []) |
||
| 248 | { |
||
| 249 | $context = $this->sanitizeContext($context); |
||
| 250 | /** @var ObjectAccessInterface $objectAccess */ |
||
| 251 | $objectAccess = $context['object_access']; |
||
| 252 | $reflectionClass = new ReflectionClass($object); |
||
| 253 | if ($this->classMetadataFactory && isset($context['groups'])) { |
||
| 254 | $objectAccess = $this->filterObjectAccess($objectAccess, $reflectionClass->name, $context['groups']); |
||
| 255 | } |
||
| 256 | $result = []; |
||
| 257 | foreach ($objectAccess->getGetterFields($reflectionClass) as $denormalizedFieldName) { |
||
| 258 | $fieldName = $this->nameConverter->normalize($denormalizedFieldName, $reflectionClass->name, $format, $context); |
||
| 259 | $value = $objectAccess->getValue($object, $denormalizedFieldName); |
||
| 260 | // circular reference |
||
| 261 | if (is_object($value) && in_array($value, $context['object_hierarchy'], true)) { |
||
| 262 | continue; |
||
| 263 | } |
||
| 264 | $result[$fieldName] = $this->toPrimitive($value, $fieldName, $format, $context); |
||
| 265 | } |
||
| 266 | return $result; |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Adds FilteredObjectAccess decorator around the Object Access by reading the class metadata needed for the serializer. |
||
| 271 | */ |
||
| 272 | private function filterObjectAccess(ObjectAccessInterface $objectAccess, string $className, array $groups): ObjectAccessInterface |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Try to convert any object or array to a native php type by calling the serializer again. |
||
| 288 | * |
||
| 289 | * @param $input |
||
| 290 | * @param string $fieldName |
||
| 291 | * @param string|null $format |
||
| 292 | * @param array $context |
||
| 293 | * @return array |
||
| 294 | */ |
||
| 295 | private function toPrimitive($input, string $fieldName, ?string $format = null, array $context = []) |
||
| 296 | { |
||
| 297 | if (is_array($input)) { |
||
| 298 | $result = []; |
||
| 299 | foreach ($input as $key => $item) { |
||
| 300 | $newContext = $context; |
||
| 301 | unset($newContext['object_to_populate']); |
||
| 302 | $newContext['object_hierarchy'][] = $input; |
||
| 303 | $newContext['key_prefix'] .= '.' . $fieldName . '.' . $key; |
||
| 304 | $result[$key] = $this->toPrimitive($item, $key, $format, $newContext); |
||
| 305 | } |
||
| 306 | return $result; |
||
| 307 | } |
||
| 308 | if (is_object($input)) { |
||
| 309 | $newContext = $context; |
||
| 310 | unset($newContext['object_to_populate']); |
||
| 311 | $newContext['object_hierarchy'][] = $input; |
||
| 312 | $newContext['key_prefix'] .= '.' . $fieldName; |
||
| 313 | return $this->serializer->normalize($input, $format, $newContext); |
||
| 314 | } |
||
| 315 | return $input; |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * {@inheritDoc} |
||
| 320 | */ |
||
| 321 | public function supportsNormalization($data, $format = null) |
||
| 322 | { |
||
| 323 | return is_object($data) && !$data instanceof Traversable; |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Adds default context array values if they are missing. |
||
| 328 | * |
||
| 329 | * @param array $context |
||
| 330 | * @return array |
||
| 331 | */ |
||
| 332 | private function sanitizeContext(array $context): array |
||
| 347 | } |
||
| 348 | } |
||
| 349 |