Conditions | 13 |
Paths | 10 |
Total Lines | 36 |
Code Lines | 21 |
Lines | 6 |
Ratio | 16.67 % |
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 |
||
83 | protected function extractRelationships(array $data, EntityMetadata $metadata) |
||
84 | { |
||
85 | $flattened = []; |
||
86 | if (!isset($data['relationships']) || !is_array($data['relationships'])) { |
||
87 | return $flattened; |
||
88 | } |
||
89 | |||
90 | foreach ($metadata->getRelationships() as $key => $relMeta) { |
||
91 | if (!isset($data['relationships'][$key])) { |
||
92 | continue; |
||
93 | } |
||
94 | $rel = $data['relationships'][$key]; |
||
95 | // Need to use array_key_exists over isset because 'null' is valid. Creating a key map for each relationship is too costly since every relationship needs the data check. |
||
96 | if (false === array_key_exists('data', $rel)) { |
||
97 | throw NormalizerException::badRequest(sprintf('The "data" member was missing from the payload for relationship "%s"', $key)); |
||
98 | } |
||
99 | |||
100 | if (empty($rel['data'])) { |
||
101 | $flattened[$key] = $relMeta->isOne() ? null : []; |
||
102 | continue; |
||
103 | } |
||
104 | |||
105 | if (!is_array($rel['data'])) { |
||
106 | throw NormalizerException::badRequest(sprintf('The "data" member is not valid in the payload for relationship "%s"', $key)); |
||
107 | } |
||
108 | |||
109 | View Code Duplication | if (true === $relMeta->isOne() && true === $this->isSequentialArray($rel['data'])) { |
|
110 | throw NormalizerException::badRequest(sprintf('The data payload for relationship "%s" is malformed. Data types of "one" must be an associative array, sequential found.', $key)); |
||
111 | } |
||
112 | View Code Duplication | if (true === $relMeta->isMany() && false === $this->isSequentialArray($rel['data'])) { |
|
113 | throw NormalizerException::badRequest(sprintf('The data payload for relationship "%s" is malformed. Data types of "many" must be a sequential array, associative found.', $key)); |
||
114 | } |
||
115 | $flattened[$key] = $rel['data']; |
||
116 | } |
||
117 | return $flattened; |
||
118 | } |
||
119 | |||
164 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.