| Conditions | 10 |
| Paths | 24 |
| Total Lines | 53 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 77 | public function normalize($object, $format = null, array $context = array()) |
||
| 78 | { |
||
| 79 | $this->setCircularReferenceHandler(function ($object) { |
||
|
|
|||
| 80 | return []; |
||
| 81 | }); |
||
| 82 | |||
| 83 | $data = parent::normalize($object, $format, $context); |
||
| 84 | |||
| 85 | if (!is_array($data)) { |
||
| 86 | return $data; |
||
| 87 | } |
||
| 88 | |||
| 89 | // Get all metadata |
||
| 90 | $classMetadata = $this->metadataFactory->getMetadataFor($object); |
||
| 91 | $attributesMetadata = $classMetadata->getAttributesMetadata(); |
||
| 92 | $classAnnotation = $this->getNormalizerClassAnnotation($classMetadata); |
||
| 93 | |||
| 94 | $jsonApi = [ |
||
| 95 | 'type' => $classAnnotation->getName(), |
||
| 96 | 'id' => $object->getId(), |
||
| 97 | 'attributes' => [], |
||
| 98 | 'relationships' => [] |
||
| 99 | ]; |
||
| 100 | |||
| 101 | foreach ($data as $atrName => $atrVal) { |
||
| 102 | $attrMetadataName = $this->nameConverter ? $this->nameConverter->denormalize($atrName) : $atrName; |
||
| 103 | if (!array_key_exists($attrMetadataName, $attributesMetadata)) { |
||
| 104 | continue; |
||
| 105 | } |
||
| 106 | |||
| 107 | $attributeMetadata = $attributesMetadata[$attrMetadataName]; |
||
| 108 | if (!$attributeMetadata instanceof AttributeMetadataInterface) { |
||
| 109 | continue; |
||
| 110 | } |
||
| 111 | |||
| 112 | $attribute = $attributeMetadata->getAttribute(); |
||
| 113 | $relationship = $attributeMetadata->getRelationship(); |
||
| 114 | |||
| 115 | if ($attribute instanceof Attribute) { |
||
| 116 | $name = $attribute->getName() ?: strtolower($atrName); |
||
| 117 | |||
| 118 | $jsonApi['attributes'][$name] = $atrVal; |
||
| 119 | } |
||
| 120 | |||
| 121 | if ($relationship instanceof Relationship) { |
||
| 122 | $name = $relationship->getName() ?: strtolower($atrName); |
||
| 123 | |||
| 124 | $jsonApi['relationships'][$name]["data"] = $atrVal; |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | return $jsonApi; |
||
| 129 | } |
||
| 130 | |||
| 161 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.