| Total Complexity | 63 |
| Total Lines | 316 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 1 | Features | 0 |
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( |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * {@inheritDoc} |
||
| 61 | */ |
||
| 62 | public function denormalize($data, $type, $format = null, array $context = []) |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Try to convert a field value to the wanted Type. |
||
| 134 | * |
||
| 135 | * @internal |
||
| 136 | * |
||
| 137 | * @param array $data |
||
| 138 | * @param string $denormalizedFieldName |
||
| 139 | * @param string $fieldName |
||
| 140 | * @param Type $type |
||
| 141 | * @param string|null $format |
||
| 142 | * @param array $context |
||
| 143 | * @return array|bool|float|int|string|null |
||
| 144 | */ |
||
| 145 | public function denormalizeType(array $data, string $denormalizedFieldName, string $fieldName, Type $type, ?string $format = null, array $context = []) |
||
| 146 | { |
||
| 147 | if (null === ($data[$fieldName] ?? null) && $type->isNullable()) { |
||
| 148 | return null; |
||
| 149 | } |
||
| 150 | switch ($type->getBuiltinType()) { |
||
| 151 | case Type::BUILTIN_TYPE_INT: |
||
| 152 | return Utils::toInt($data[$fieldName]); |
||
| 153 | case Type::BUILTIN_TYPE_FLOAT: |
||
| 154 | return Utils::toFloat($data[$fieldName]); |
||
| 155 | case Type::BUILTIN_TYPE_STRING: |
||
| 156 | return Utils::toString($data[$fieldName]); |
||
| 157 | case Type::BUILTIN_TYPE_BOOL: |
||
| 158 | return Utils::toBool($data[$fieldName]); |
||
| 159 | case Type::BUILTIN_TYPE_OBJECT: |
||
| 160 | $newContext = $context; |
||
| 161 | unset($newContext['object_to_populate']); |
||
| 162 | $newContext['key_prefix'] = $context['key_prefix'] ? ($context['key_prefix'] . '.' . $denormalizedFieldName) : $denormalizedFieldName; |
||
| 163 | $newContext['collection_resource'] = $type->getCollectionValueType() ? $type->getCollectionValueType()->getClassName() : null; |
||
| 164 | return $this->serializer->denormalize( |
||
| 165 | $data[$fieldName], |
||
| 166 | $type->getClassName() ?? 'stdClass', |
||
| 167 | $format, |
||
| 168 | $newContext |
||
| 169 | ); |
||
| 170 | case Type::BUILTIN_TYPE_ARRAY: |
||
| 171 | $subType = $type->getCollectionValueType(); |
||
| 172 | if ($subType && $subType->getClassName()) { |
||
| 173 | $newContext = $context; |
||
| 174 | unset($newContext['object_to_populate']); |
||
| 175 | $newContext['key_prefix'] = $context['key_prefix'] ? ($context['key_prefix'] . '.' . $denormalizedFieldName) : $denormalizedFieldName; |
||
| 176 | $newContext['collection_resource'] = $type->getCollectionValueType() ? $type->getCollectionValueType()->getClassName() : null; |
||
| 177 | return $this->serializer->denormalize( |
||
| 178 | $data[$fieldName], |
||
| 179 | $subType->getClassName() . '[]', |
||
| 180 | $format, |
||
| 181 | $newContext |
||
| 182 | ); |
||
| 183 | } |
||
| 184 | return (array) $data[$fieldName]; |
||
| 185 | default: |
||
| 186 | throw new CouldNotConvertException('int, float, string, bool, object, array', $type->getBuiltinType()); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Try to get create a new instance of this class from the input $data we retrieve. |
||
| 192 | * |
||
| 193 | * @param array $data |
||
| 194 | * @param string $type |
||
| 195 | * @param ObjectAccessInterface $objectAccess |
||
| 196 | * @param string|null $format |
||
| 197 | * @param array $context |
||
| 198 | * @return object |
||
| 199 | */ |
||
| 200 | private function instantiate(array $data, string $type, ObjectAccessInterface $objectAccess, ?string $format = null, array $context = []) |
||
| 201 | { |
||
| 202 | $reflectionClass = new ReflectionClass($type); |
||
| 203 | $argumentTypes = $objectAccess->getConstructorArguments($reflectionClass); |
||
| 204 | $errors = new ErrorBag($context['key_prefix']); |
||
| 205 | $parsedArguments = []; |
||
| 206 | foreach ($argumentTypes as $denormalizedFieldName => $argumentType) { |
||
| 207 | try { |
||
| 208 | $fieldName = $this->nameConverter->normalize($denormalizedFieldName, $type, $format, $context); |
||
| 209 | if (!array_key_exists($fieldName, $data)) { |
||
| 210 | $constructor = $reflectionClass->getConstructor(); |
||
| 211 | foreach ($constructor->getParameters() as $parameter) { |
||
| 212 | if ($parameter->name === $denormalizedFieldName && $parameter->isDefaultValueAvailable()) { |
||
| 213 | $parsedArguments[] = $parameter->getDefaultValue(); |
||
| 214 | continue(2); |
||
| 215 | } |
||
| 216 | } |
||
| 217 | throw new MissingConstructorArgumentsException($fieldName . ' is required'); |
||
| 218 | } |
||
| 219 | if ($argumentType) { |
||
| 220 | $parsedArguments[] = $this->denormalizeType($data, $denormalizedFieldName, $fieldName, $argumentType, $format, $context); |
||
| 221 | } else { |
||
| 222 | $parsedArguments[] = $data[$fieldName]; |
||
| 223 | |||
| 224 | } |
||
| 225 | } catch (Throwable $throwable) { |
||
| 226 | $errors->addThrowable($denormalizedFieldName, $throwable); |
||
| 227 | } |
||
| 228 | } |
||
| 229 | if ($errors->hasErrors()) { |
||
| 230 | throw new ValidationException($errors); |
||
| 231 | } |
||
| 232 | return $objectAccess->instantiate($reflectionClass, $parsedArguments); |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * {@inheritDoc} |
||
| 237 | */ |
||
| 238 | public function supportsDenormalization($data, $type, $format = null) |
||
| 239 | { |
||
| 240 | return (is_array($data) || $data instanceof stdClass) && class_exists($type); |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * {@inheritDoc} |
||
| 245 | */ |
||
| 246 | public function normalize($object, $format = null, array $context = []) |
||
| 247 | { |
||
| 248 | $context = $this->sanitizeContext($context); |
||
| 249 | /** @var ObjectAccessInterface $objectAccess */ |
||
| 250 | $objectAccess = $context['object_access']; |
||
| 251 | $reflectionClass = new ReflectionClass($object); |
||
| 252 | if ($this->classMetadataFactory && isset($context['groups'])) { |
||
| 253 | $objectAccess = $this->filterObjectAccess($objectAccess, $reflectionClass->name, $context['groups']); |
||
| 254 | } |
||
| 255 | $result = []; |
||
| 256 | foreach ($objectAccess->getGetterFields($reflectionClass) as $denormalizedFieldName) { |
||
| 257 | $fieldName = $this->nameConverter->normalize($denormalizedFieldName, $reflectionClass->name, $format, $context); |
||
| 258 | $value = $objectAccess->getValue($object, $denormalizedFieldName); |
||
| 259 | // circular reference |
||
| 260 | if (is_object($value) && in_array($value, $context['object_hierarchy'], true)) { |
||
| 261 | continue; |
||
| 262 | } |
||
| 263 | $result[$fieldName] = $this->toPrimitive($value, $fieldName, $format, $context); |
||
| 264 | } |
||
| 265 | return $result; |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Adds FilteredObjectAccess decorator around the Object Access by reading the class metadata needed for the serializer. |
||
| 270 | */ |
||
| 271 | private function filterObjectAccess(ObjectAccessInterface $objectAccess, string $className, array $groups): ObjectAccessInterface |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Try to convert any object or array to a native php type by calling the serializer again. |
||
| 287 | * |
||
| 288 | * @param $input |
||
| 289 | * @param string $fieldName |
||
| 290 | * @param string|null $format |
||
| 291 | * @param array $context |
||
| 292 | * @return array |
||
| 293 | */ |
||
| 294 | private function toPrimitive($input, string $fieldName, ?string $format = null, array $context = []) |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * {@inheritDoc} |
||
| 319 | */ |
||
| 320 | public function supportsNormalization($data, $format = null) |
||
| 321 | { |
||
| 322 | return is_object($data) && !$data instanceof Traversable; |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Adds default context array values if they are missing. |
||
| 327 | * |
||
| 328 | * @param array $context |
||
| 329 | * @return array |
||
| 330 | */ |
||
| 331 | private function sanitizeContext(array $context): array |
||
| 346 | } |
||
| 347 | } |
||
| 348 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.