Total Complexity | 64 |
Total Lines | 318 |
Duplicated Lines | 0 % |
Changes | 4 | ||
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 = []) |
||
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 = []) |
||
148 | { |
||
149 | if (null === ($data[$fieldName] ?? null) && $type->isNullable()) { |
||
150 | return null; |
||
151 | } |
||
152 | switch ($type->getBuiltinType()) { |
||
153 | case Type::BUILTIN_TYPE_INT: |
||
154 | return Utils::toInt($data[$fieldName]); |
||
155 | case Type::BUILTIN_TYPE_FLOAT: |
||
156 | return Utils::toFloat($data[$fieldName]); |
||
157 | case Type::BUILTIN_TYPE_STRING: |
||
158 | return Utils::toString($data[$fieldName]); |
||
159 | case Type::BUILTIN_TYPE_BOOL: |
||
160 | return Utils::toBool($data[$fieldName]); |
||
161 | case Type::BUILTIN_TYPE_OBJECT: |
||
162 | $newContext = $context; |
||
163 | unset($newContext['object_to_populate']); |
||
164 | $newContext['key_prefix'] = $context['key_prefix'] ? ($context['key_prefix'] . '.' . $denormalizedFieldName) : $denormalizedFieldName; |
||
165 | $newContext['collection_resource'] = $type->getCollectionValueType() ? $type->getCollectionValueType()->getClassName() : null; |
||
166 | return $this->serializer->denormalize( |
||
167 | $data[$fieldName], |
||
168 | $type->getClassName() ?? 'stdClass', |
||
169 | $format, |
||
170 | $newContext |
||
171 | ); |
||
172 | case Type::BUILTIN_TYPE_ARRAY: |
||
173 | $subType = $type->getCollectionValueType(); |
||
174 | if ($subType && $subType->getClassName()) { |
||
175 | $newContext = $context; |
||
176 | unset($newContext['object_to_populate']); |
||
177 | $newContext['key_prefix'] = $context['key_prefix'] ? ($context['key_prefix'] . '.' . $denormalizedFieldName) : $denormalizedFieldName; |
||
178 | $newContext['collection_resource'] = $type->getCollectionValueType() ? $type->getCollectionValueType()->getClassName() : null; |
||
179 | return $this->serializer->denormalize( |
||
180 | $data[$fieldName], |
||
181 | $subType->getClassName() . '[]', |
||
182 | $format, |
||
183 | $newContext |
||
184 | ); |
||
185 | } |
||
186 | return (array) $data[$fieldName]; |
||
187 | default: |
||
188 | throw new CouldNotConvertException('int, float, string, bool, object, array', $type->getBuiltinType()); |
||
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 | } |
||
227 | } catch (Throwable $throwable) { |
||
228 | $errors->addThrowable($denormalizedFieldName, $throwable); |
||
229 | } |
||
230 | } |
||
231 | if ($errors->hasErrors()) { |
||
232 | throw new ValidationException($errors); |
||
233 | } |
||
234 | return $objectAccess->instantiate($reflectionClass, $parsedArguments); |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * {@inheritDoc} |
||
239 | */ |
||
240 | public function supportsDenormalization($data, $type, $format = null) |
||
241 | { |
||
242 | return (is_array($data) || $data instanceof stdClass) && class_exists($type); |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * {@inheritDoc} |
||
247 | */ |
||
248 | public function normalize($object, $format = null, array $context = []) |
||
249 | { |
||
250 | $context = $this->sanitizeContext($context); |
||
251 | /** @var ObjectAccessInterface $objectAccess */ |
||
252 | $objectAccess = $context['object_access']; |
||
253 | $reflectionClass = new ReflectionClass($object); |
||
254 | if ($this->classMetadataFactory && isset($context['groups'])) { |
||
255 | $objectAccess = $this->filterObjectAccess($objectAccess, $reflectionClass->name, $context['groups']); |
||
256 | } |
||
257 | $result = []; |
||
258 | foreach ($objectAccess->getGetterFields($reflectionClass) as $denormalizedFieldName) { |
||
259 | $fieldName = $this->nameConverter->normalize($denormalizedFieldName, $reflectionClass->name, $format, $context); |
||
260 | $value = $objectAccess->getValue($object, $denormalizedFieldName); |
||
261 | // circular reference |
||
262 | if (is_object($value) && in_array($value, $context['object_hierarchy'], true)) { |
||
263 | continue; |
||
264 | } |
||
265 | $result[$fieldName] = $this->toPrimitive($value, $fieldName, $format, $context); |
||
266 | } |
||
267 | return $result; |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * Adds FilteredObjectAccess decorator around the Object Access by reading the class metadata needed for the serializer. |
||
272 | */ |
||
273 | private function filterObjectAccess(ObjectAccessInterface $objectAccess, string $className, array $groups): ObjectAccessInterface |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * Try to convert any object or array to a native php type by calling the serializer again. |
||
289 | * |
||
290 | * @param $input |
||
291 | * @param string $fieldName |
||
292 | * @param string|null $format |
||
293 | * @param array $context |
||
294 | * @return array |
||
295 | */ |
||
296 | private function toPrimitive($input, string $fieldName, ?string $format = null, array $context = []) |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * {@inheritDoc} |
||
321 | */ |
||
322 | public function supportsNormalization($data, $format = null) |
||
323 | { |
||
324 | return is_object($data) && !$data instanceof Traversable; |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * Adds default context array values if they are missing. |
||
329 | * |
||
330 | * @param array $context |
||
331 | * @return array |
||
332 | */ |
||
333 | private function sanitizeContext(array $context): array |
||
348 | } |
||
349 | } |
||
350 |