| Total Complexity | 44 |
| Total Lines | 238 |
| 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 |
||
| 29 | final class ItemNormalizer extends AbstractItemNormalizer |
||
| 30 | { |
||
| 31 | use CacheKeyTrait; |
||
| 32 | use ClassInfoTrait; |
||
| 33 | use ContextTrait; |
||
| 34 | |||
| 35 | public const FORMAT = 'jsonhal'; |
||
| 36 | |||
| 37 | private $componentsCache = []; |
||
| 38 | private $attributesMetadataCache = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * {@inheritdoc} |
||
| 42 | */ |
||
| 43 | public function supportsNormalization($data, $format = null): bool |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * {@inheritdoc} |
||
| 50 | */ |
||
| 51 | public function normalize($object, $format = null, array $context = []) |
||
| 52 | { |
||
| 53 | if (null !== $this->getOutputClass($this->getObjectClass($object), $context)) { |
||
| 54 | return parent::normalize($object, $format, $context); |
||
| 55 | } |
||
| 56 | |||
| 57 | if (!isset($context['cache_key'])) { |
||
| 58 | $context['cache_key'] = $this->getCacheKey($format, $context); |
||
| 59 | } |
||
| 60 | |||
| 61 | $resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class'] ?? null); |
||
| 62 | $context = $this->initContext($resourceClass, $context); |
||
| 63 | $iri = $this->iriConverter->getIriFromItem($object); |
||
| 64 | $context['iri'] = $iri; |
||
| 65 | $context['api_normalize'] = true; |
||
| 66 | |||
| 67 | $data = parent::normalize($object, $format, $context); |
||
| 68 | if (!\is_array($data)) { |
||
| 69 | return $data; |
||
| 70 | } |
||
| 71 | |||
| 72 | $metadata = [ |
||
| 73 | '_links' => [ |
||
| 74 | 'self' => [ |
||
| 75 | 'href' => $iri, |
||
| 76 | ], |
||
| 77 | ], |
||
| 78 | ]; |
||
| 79 | $components = $this->getComponents($object, $format, $context); |
||
| 80 | $metadata = $this->populateRelation($metadata, $object, $format, $context, $components, 'links'); |
||
| 81 | $metadata = $this->populateRelation($metadata, $object, $format, $context, $components, 'embedded'); |
||
| 82 | |||
| 83 | return $metadata + $data; |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * {@inheritdoc} |
||
| 88 | */ |
||
| 89 | public function supportsDenormalization($data, $type, $format = null): bool |
||
| 90 | { |
||
| 91 | // prevent the use of lower priority normalizers (e.g. serializer.normalizer.object) for this format |
||
| 92 | return self::FORMAT === $format; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * {@inheritdoc} |
||
| 97 | * |
||
| 98 | * @throws LogicException |
||
| 99 | */ |
||
| 100 | public function denormalize($data, $class, $format = null, array $context = []) |
||
| 101 | { |
||
| 102 | throw new LogicException(sprintf('%s is a read-only format.', self::FORMAT)); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * {@inheritdoc} |
||
| 107 | */ |
||
| 108 | protected function getAttributes($object, $format = null, array $context): array |
||
| 109 | { |
||
| 110 | return $this->getComponents($object, $format, $context)['states']; |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Gets HAL components of the resource: states, links and embedded. |
||
| 115 | * |
||
| 116 | * @param object $object |
||
| 117 | */ |
||
| 118 | private function getComponents($object, ?string $format, array $context): array |
||
| 119 | { |
||
| 120 | $cacheKey = $this->getObjectClass($object).'-'.$context['cache_key']; |
||
| 121 | |||
| 122 | if (isset($this->componentsCache[$cacheKey])) { |
||
| 123 | return $this->componentsCache[$cacheKey]; |
||
| 124 | } |
||
| 125 | |||
| 126 | $attributes = parent::getAttributes($object, $format, $context); |
||
| 127 | $options = $this->getFactoryOptions($context); |
||
| 128 | |||
| 129 | $components = [ |
||
| 130 | 'states' => [], |
||
| 131 | 'links' => [], |
||
| 132 | 'embedded' => [], |
||
| 133 | ]; |
||
| 134 | |||
| 135 | foreach ($attributes as $attribute) { |
||
| 136 | $propertyMetadata = $this->propertyMetadataFactory->create($context['resource_class'], $attribute, $options); |
||
| 137 | |||
| 138 | $type = $propertyMetadata->getType(); |
||
| 139 | $isOne = $isMany = false; |
||
| 140 | |||
| 141 | if (null !== $type) { |
||
| 142 | if ($type->isCollection()) { |
||
| 143 | $valueType = $type->getCollectionValueType(); |
||
| 144 | $isMany = null !== $valueType && ($className = $valueType->getClassName()) && $this->resourceClassResolver->isResourceClass($className); |
||
| 145 | } else { |
||
| 146 | $className = $type->getClassName(); |
||
| 147 | $isOne = $className && $this->resourceClassResolver->isResourceClass($className); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | if (!$isOne && !$isMany) { |
||
| 152 | $components['states'][] = $attribute; |
||
| 153 | continue; |
||
| 154 | } |
||
| 155 | |||
| 156 | $relation = ['name' => $attribute, 'cardinality' => $isOne ? 'one' : 'many']; |
||
| 157 | if ($propertyMetadata->isReadableLink()) { |
||
| 158 | $components['embedded'][] = $relation; |
||
| 159 | } |
||
| 160 | |||
| 161 | $components['links'][] = $relation; |
||
| 162 | } |
||
| 163 | |||
| 164 | if (false !== $context['cache_key']) { |
||
| 165 | $this->componentsCache[$cacheKey] = $components; |
||
| 166 | } |
||
| 167 | |||
| 168 | return $components; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Populates _links and _embedded keys. |
||
| 173 | * |
||
| 174 | * @param object $object |
||
| 175 | */ |
||
| 176 | private function populateRelation(array $data, $object, ?string $format, array $context, array $components, string $type): array |
||
| 177 | { |
||
| 178 | $class = $this->getObjectClass($object); |
||
| 179 | |||
| 180 | $attributesMetadata = \array_key_exists($class, $this->attributesMetadataCache) ? |
||
| 181 | $this->attributesMetadataCache[$class] : |
||
| 182 | $this->attributesMetadataCache[$class] = $this->classMetadataFactory ? $this->classMetadataFactory->getMetadataFor($class)->getAttributesMetadata() : null; |
||
| 183 | |||
| 184 | $key = '_'.$type; |
||
| 185 | foreach ($components[$type] as $relation) { |
||
| 186 | if (null !== $attributesMetadata && $this->isMaxDepthReached($attributesMetadata, $class, $relation['name'], $context)) { |
||
| 187 | continue; |
||
| 188 | } |
||
| 189 | |||
| 190 | $attributeValue = $this->getAttributeValue($object, $relation['name'], $format, $context); |
||
| 191 | if (empty($attributeValue)) { |
||
| 192 | continue; |
||
| 193 | } |
||
| 194 | |||
| 195 | $relationName = $relation['name']; |
||
| 196 | if ($this->nameConverter) { |
||
| 197 | $relationName = $this->nameConverter->normalize($relationName, $class, $format, $context); |
||
|
|
|||
| 198 | } |
||
| 199 | |||
| 200 | if ('one' === $relation['cardinality']) { |
||
| 201 | if ('links' === $type) { |
||
| 202 | $data[$key][$relationName]['href'] = $this->getRelationIri($attributeValue); |
||
| 203 | continue; |
||
| 204 | } |
||
| 205 | |||
| 206 | $data[$key][$relationName] = $attributeValue; |
||
| 207 | continue; |
||
| 208 | } |
||
| 209 | |||
| 210 | // many |
||
| 211 | $data[$key][$relationName] = []; |
||
| 212 | foreach ($attributeValue as $rel) { |
||
| 213 | if ('links' === $type) { |
||
| 214 | $rel = ['href' => $this->getRelationIri($rel)]; |
||
| 215 | } |
||
| 216 | |||
| 217 | $data[$key][$relationName][] = $rel; |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | return $data; |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Gets the IRI of the given relation. |
||
| 226 | * |
||
| 227 | * @throws UnexpectedValueException |
||
| 228 | */ |
||
| 229 | private function getRelationIri($rel): string |
||
| 230 | { |
||
| 231 | if (!(\is_array($rel) || \is_string($rel))) { |
||
| 232 | throw new UnexpectedValueException('Expected relation to be an IRI or array'); |
||
| 233 | } |
||
| 234 | |||
| 235 | return \is_string($rel) ? $rel : $rel['_links']['self']['href']; |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Is the max depth reached for the given attribute? |
||
| 240 | * |
||
| 241 | * @param AttributeMetadataInterface[] $attributesMetadata |
||
| 242 | */ |
||
| 243 | private function isMaxDepthReached(array $attributesMetadata, string $class, string $attribute, array &$context): bool |
||
| 267 | } |
||
| 268 | } |
||
| 269 |
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.