| Total Complexity | 65 | 
| Total Lines | 354 | 
| Duplicated Lines | 0 % | 
| Changes | 0 | ||
Complex classes like ItemNormalizer 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 ItemNormalizer, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 42 | final class ItemNormalizer extends AbstractItemNormalizer | ||
| 43 | { | ||
| 44 | use CacheKeyTrait; | ||
| 45 | use ClassInfoTrait; | ||
| 46 | use ContextTrait; | ||
| 47 | |||
| 48 | public const FORMAT = 'jsonapi'; | ||
| 49 | |||
| 50 | private $componentsCache = []; | ||
| 51 | |||
| 52 | public function __construct(PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, IriConverterInterface $iriConverter, ResourceClassResolverInterface $resourceClassResolver, ?PropertyAccessorInterface $propertyAccessor, ?NameConverterInterface $nameConverter, ResourceMetadataFactoryInterface $resourceMetadataFactory, array $defaultContext = [], iterable $dataTransformers = []) | ||
| 53 |     { | ||
| 54 | parent::__construct($propertyNameCollectionFactory, $propertyMetadataFactory, $iriConverter, $resourceClassResolver, $propertyAccessor, $nameConverter, null, null, false, $defaultContext, $dataTransformers, $resourceMetadataFactory); | ||
| 55 | } | ||
| 56 | |||
| 57 | /** | ||
| 58 |      * {@inheritdoc} | ||
| 59 | */ | ||
| 60 | public function supportsNormalization($data, $format = null): bool | ||
| 61 |     { | ||
| 62 | return self::FORMAT === $format && parent::supportsNormalization($data, $format); | ||
| 63 | } | ||
| 64 | |||
| 65 | /** | ||
| 66 |      * {@inheritdoc} | ||
| 67 | */ | ||
| 68 | public function normalize($object, $format = null, array $context = []) | ||
| 69 |     { | ||
| 70 |         if (null !== $this->getOutputClass($this->getObjectClass($object), $context)) { | ||
| 71 | return parent::normalize($object, $format, $context); | ||
| 72 | } | ||
| 73 | |||
| 74 |         if (!isset($context['cache_key'])) { | ||
| 75 | $context['cache_key'] = $this->getCacheKey($format, $context); | ||
| 76 | } | ||
| 77 | |||
| 78 | $resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class'] ?? null); | ||
| 79 | $context = $this->initContext($resourceClass, $context); | ||
| 80 | $iri = $this->iriConverter->getIriFromItem($object); | ||
| 81 | $context['iri'] = $iri; | ||
| 82 | $context['api_normalize'] = true; | ||
| 83 | |||
| 84 | $data = parent::normalize($object, $format, $context); | ||
| 85 |         if (!\is_array($data)) { | ||
| 86 | return $data; | ||
| 87 | } | ||
| 88 | |||
| 89 | $resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); | ||
| 90 | |||
| 91 | // Get and populate relations | ||
| 92 | $allRelationshipsData = $this->getComponents($object, $format, $context)['relationships']; | ||
| 93 | $populatedRelationContext = $context; | ||
| 94 | $relationshipsData = $this->getPopulatedRelations($object, $format, $populatedRelationContext, $allRelationshipsData); | ||
| 95 | $includedResourcesData = $this->getRelatedResources($object, $format, $context, $allRelationshipsData); | ||
| 96 | |||
| 97 | $resourceData = [ | ||
| 98 | 'id' => $context['iri'], | ||
| 99 | 'type' => $resourceMetadata->getShortName(), | ||
| 100 | ]; | ||
| 101 | |||
| 102 |         if ($data) { | ||
|  | |||
| 103 | $resourceData['attributes'] = $data; | ||
| 104 | } | ||
| 105 | |||
| 106 |         if ($relationshipsData) { | ||
| 107 | $resourceData['relationships'] = $relationshipsData; | ||
| 108 | } | ||
| 109 | |||
| 110 | $document = ['data' => $resourceData]; | ||
| 111 | |||
| 112 |         if ($includedResourcesData) { | ||
| 113 | $document['included'] = $includedResourcesData; | ||
| 114 | } | ||
| 115 | |||
| 116 | return $document; | ||
| 117 | } | ||
| 118 | |||
| 119 | /** | ||
| 120 |      * {@inheritdoc} | ||
| 121 | */ | ||
| 122 | public function supportsDenormalization($data, $type, $format = null): bool | ||
| 123 |     { | ||
| 124 | return self::FORMAT === $format && parent::supportsDenormalization($data, $type, $format); | ||
| 125 | } | ||
| 126 | |||
| 127 | /** | ||
| 128 |      * {@inheritdoc} | ||
| 129 | * | ||
| 130 | * @throws NotNormalizableValueException | ||
| 131 | */ | ||
| 132 | public function denormalize($data, $class, $format = null, array $context = []) | ||
| 133 |     { | ||
| 134 | // Avoid issues with proxies if we populated the object | ||
| 135 |         if (!isset($context[self::OBJECT_TO_POPULATE]) && isset($data['data']['id'])) { | ||
| 136 |             if (true !== ($context['api_allow_update'] ?? true)) { | ||
| 137 |                 throw new NotNormalizableValueException('Update is not allowed for this operation.'); | ||
| 138 | } | ||
| 139 | |||
| 140 | $context[self::OBJECT_TO_POPULATE] = $this->iriConverter->getItemFromIri( | ||
| 141 | $data['data']['id'], | ||
| 142 | $context + ['fetch_data' => false] | ||
| 143 | ); | ||
| 144 | } | ||
| 145 | |||
| 146 | // Merge attributes and relationships, into format expected by the parent normalizer | ||
| 147 | $dataToDenormalize = array_merge( | ||
| 148 | $data['data']['attributes'] ?? [], | ||
| 149 | $data['data']['relationships'] ?? [] | ||
| 150 | ); | ||
| 151 | |||
| 152 | return parent::denormalize( | ||
| 153 | $dataToDenormalize, | ||
| 154 | $class, | ||
| 155 | $format, | ||
| 156 | $context | ||
| 157 | ); | ||
| 158 | } | ||
| 159 | |||
| 160 | /** | ||
| 161 |      * {@inheritdoc} | ||
| 162 | */ | ||
| 163 | protected function getAttributes($object, $format = null, array $context): array | ||
| 164 |     { | ||
| 165 | return $this->getComponents($object, $format, $context)['attributes']; | ||
| 166 | } | ||
| 167 | |||
| 168 | /** | ||
| 169 |      * {@inheritdoc} | ||
| 170 | */ | ||
| 171 | protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = []): void | ||
| 172 |     { | ||
| 173 |         parent::setAttributeValue($object, $attribute, \is_array($value) && \array_key_exists('data', $value) ? $value['data'] : $value, $format, $context); | ||
| 174 | } | ||
| 175 | |||
| 176 | /** | ||
| 177 |      * {@inheritdoc} | ||
| 178 | * | ||
| 179 | * @see http://jsonapi.org/format/#document-resource-object-linkage | ||
| 180 | * | ||
| 181 | * @throws RuntimeException | ||
| 182 | * @throws NotNormalizableValueException | ||
| 183 | */ | ||
| 184 | protected function denormalizeRelation(string $attributeName, PropertyMetadata $propertyMetadata, string $className, $value, ?string $format, array $context) | ||
| 185 |     { | ||
| 186 |         if (!\is_array($value) || !isset($value['id'], $value['type'])) { | ||
| 187 |             throw new NotNormalizableValueException('Only resource linkage supported currently, see: http://jsonapi.org/format/#document-resource-object-linkage.'); | ||
| 188 | } | ||
| 189 | |||
| 190 |         try { | ||
| 191 | return $this->iriConverter->getItemFromIri($value['id'], $context + ['fetch_data' => true]); | ||
| 192 |         } catch (ItemNotFoundException $e) { | ||
| 193 | throw new RuntimeException($e->getMessage(), $e->getCode(), $e); | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | /** | ||
| 198 |      * {@inheritdoc} | ||
| 199 | * | ||
| 200 | * @see http://jsonapi.org/format/#document-resource-object-linkage | ||
| 201 | */ | ||
| 202 | protected function normalizeRelation(PropertyMetadata $propertyMetadata, $relatedObject, string $resourceClass, ?string $format, array $context) | ||
| 203 |     { | ||
| 204 |         if (null !== $relatedObject) { | ||
| 205 | $iri = $this->iriConverter->getIriFromItem($relatedObject); | ||
| 206 | $context['iri'] = $iri; | ||
| 207 | |||
| 208 |             if (isset($context['resources'])) { | ||
| 209 | $context['resources'][$iri] = $iri; | ||
| 210 | } | ||
| 211 | } | ||
| 212 | |||
| 213 |         if (null === $relatedObject || isset($context['api_included'])) { | ||
| 214 |             if (!$this->serializer instanceof NormalizerInterface) { | ||
| 215 |                 throw new LogicException(sprintf('The injected serializer must be an instance of "%s".', NormalizerInterface::class)); | ||
| 216 | } | ||
| 217 | |||
| 218 | $normalizedRelatedObject = $this->serializer->normalize($relatedObject, $format, $context); | ||
| 219 |             if (!\is_string($normalizedRelatedObject) && !\is_array($normalizedRelatedObject) && !$normalizedRelatedObject instanceof \ArrayObject && null !== $normalizedRelatedObject) { | ||
| 220 |                 throw new UnexpectedValueException('Expected normalized relation to be an IRI, array, \ArrayObject or null'); | ||
| 221 | } | ||
| 222 | |||
| 223 | return $normalizedRelatedObject; | ||
| 224 | } | ||
| 225 | |||
| 226 | return [ | ||
| 227 | 'data' => [ | ||
| 228 | 'type' => $this->resourceMetadataFactory->create($resourceClass)->getShortName(), | ||
| 229 | 'id' => $iri, | ||
| 230 | ], | ||
| 231 | ]; | ||
| 232 | } | ||
| 233 | |||
| 234 | /** | ||
| 235 |      * {@inheritdoc} | ||
| 236 | */ | ||
| 237 | protected function isAllowedAttribute($classOrObject, $attribute, $format = null, array $context = []): bool | ||
| 240 | } | ||
| 241 | |||
| 242 | /** | ||
| 243 | * Gets JSON API components of the resource: attributes, relationships, meta and links. | ||
| 244 | * | ||
| 245 | * @param object $object | ||
| 246 | */ | ||
| 247 | private function getComponents($object, ?string $format, array $context): array | ||
| 248 |     { | ||
| 249 | $cacheKey = $this->getObjectClass($object).'-'.$context['cache_key']; | ||
| 250 | |||
| 251 |         if (isset($this->componentsCache[$cacheKey])) { | ||
| 252 | return $this->componentsCache[$cacheKey]; | ||
| 253 | } | ||
| 254 | |||
| 255 | $attributes = parent::getAttributes($object, $format, $context); | ||
| 256 | |||
| 257 | $options = $this->getFactoryOptions($context); | ||
| 258 | |||
| 259 | $components = [ | ||
| 260 | 'links' => [], | ||
| 261 | 'relationships' => [], | ||
| 262 | 'attributes' => [], | ||
| 263 | 'meta' => [], | ||
| 264 | ]; | ||
| 265 | |||
| 266 |         foreach ($attributes as $attribute) { | ||
| 267 | $propertyMetadata = $this | ||
| 268 | ->propertyMetadataFactory | ||
| 269 | ->create($context['resource_class'], $attribute, $options); | ||
| 270 | |||
| 271 | $type = $propertyMetadata->getType(); | ||
| 272 | $isOne = $isMany = false; | ||
| 273 | |||
| 274 |             if (null !== $type) { | ||
| 275 |                 if ($type->isCollection()) { | ||
| 276 | $isMany = ($type->getCollectionValueType() && $className = $type->getCollectionValueType()->getClassName()) ? $this->resourceClassResolver->isResourceClass($className) : false; | ||
| 277 |                 } else { | ||
| 278 | $isOne = ($className = $type->getClassName()) ? $this->resourceClassResolver->isResourceClass($className) : false; | ||
| 279 | } | ||
| 280 | } | ||
| 281 | |||
| 282 |             if (!isset($className) || !$isOne && !$isMany) { | ||
| 283 | $components['attributes'][] = $attribute; | ||
| 284 | |||
| 285 | continue; | ||
| 286 | } | ||
| 287 | |||
| 288 | $relation = [ | ||
| 289 | 'name' => $attribute, | ||
| 290 | 'type' => $this->resourceMetadataFactory->create($className)->getShortName(), | ||
| 291 | 'cardinality' => $isOne ? 'one' : 'many', | ||
| 292 | ]; | ||
| 293 | |||
| 294 | $components['relationships'][] = $relation; | ||
| 295 | } | ||
| 296 | |||
| 297 |         if (false !== $context['cache_key']) { | ||
| 298 | $this->componentsCache[$cacheKey] = $components; | ||
| 299 | } | ||
| 300 | |||
| 301 | return $components; | ||
| 302 | } | ||
| 303 | |||
| 304 | /** | ||
| 305 | * Populates relationships keys. | ||
| 306 | * | ||
| 307 | * @param object $object | ||
| 308 | * | ||
| 309 | * @throws UnexpectedValueException | ||
| 310 | */ | ||
| 311 | private function getPopulatedRelations($object, ?string $format, array $context, array $relationships): array | ||
| 312 |     { | ||
| 313 | $data = []; | ||
| 314 | |||
| 315 |         if (!isset($context['resource_class'])) { | ||
| 316 | return $data; | ||
| 317 | } | ||
| 318 | |||
| 319 | unset($context['api_included']); | ||
| 320 |         foreach ($relationships as $relationshipDataArray) { | ||
| 321 | $relationshipName = $relationshipDataArray['name']; | ||
| 322 | |||
| 323 | $attributeValue = $this->getAttributeValue($object, $relationshipName, $format, $context); | ||
| 324 | |||
| 325 |             if ($this->nameConverter) { | ||
| 326 | $relationshipName = $this->nameConverter->normalize($relationshipName, $context['resource_class'], self::FORMAT, $context); | ||
| 327 | } | ||
| 328 | |||
| 329 |             if (!$attributeValue) { | ||
| 330 | continue; | ||
| 331 | } | ||
| 332 | |||
| 333 | $data[$relationshipName] = [ | ||
| 334 | 'data' => [], | ||
| 335 | ]; | ||
| 336 | |||
| 337 | // Many to one relationship | ||
| 338 |             if ('one' === $relationshipDataArray['cardinality']) { | ||
| 339 | unset($attributeValue['data']['attributes']); | ||
| 340 | $data[$relationshipName] = $attributeValue; | ||
| 341 | |||
| 342 | continue; | ||
| 343 | } | ||
| 344 | |||
| 345 | // Many to many relationship | ||
| 346 |             foreach ($attributeValue as $attributeValueElement) { | ||
| 347 |                 if (!isset($attributeValueElement['data'])) { | ||
| 348 |                     throw new UnexpectedValueException(sprintf('The JSON API attribute \'%s\' must contain a "data" key.', $relationshipName)); | ||
| 349 | } | ||
| 350 | unset($attributeValueElement['data']['attributes']); | ||
| 351 | $data[$relationshipName]['data'][] = $attributeValueElement['data']; | ||
| 352 | } | ||
| 353 | } | ||
| 354 | |||
| 355 | return $data; | ||
| 356 | } | ||
| 357 | |||
| 358 | /** | ||
| 359 | * Populates included keys. | ||
| 360 | */ | ||
| 361 | private function getRelatedResources($object, ?string $format, array $context, array $relationships): array | ||
| 396 | } | ||
| 397 | } | ||
| 398 | 
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.