| Conditions | 16 |
| Paths | 16 |
| Total Lines | 36 |
| Code Lines | 25 |
| Lines | 12 |
| Ratio | 33.33 % |
| 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 |
||
| 35 | public function parse($rawBody, $contentType) |
||
| 36 | { |
||
| 37 | $array = parent::parse($rawBody, $contentType); |
||
| 38 | if ($type = ArrayHelper::getValue($array, 'data.type')) { |
||
| 39 | $formName = $this->typeToFormName($type); |
||
| 40 | if ($attributes = ArrayHelper::getValue($array, 'data.attributes')) { |
||
| 41 | $result[$formName] = array_combine($this->parseMemberNames(array_keys($attributes)), array_values($attributes)); |
||
|
|
|||
| 42 | } elseif ($id = ArrayHelper::getValue($array, 'data.id')) { |
||
| 43 | $result[$formName] = ['id' => $id, 'type' => $type]; |
||
| 44 | } |
||
| 45 | if ($relationships = ArrayHelper::getValue($array, 'data.relationships')) { |
||
| 46 | foreach ($relationships as $name => $relationship) { |
||
| 47 | if (isset($relationship[0])) { |
||
| 48 | View Code Duplication | foreach ($relationship as $item) { |
|
| 49 | if (isset($item['type']) && isset($item['id'])) { |
||
| 50 | $formName = $this->typeToFormName($item['type']); |
||
| 51 | $result[$name][$formName][] = $item; |
||
| 52 | } |
||
| 53 | } |
||
| 54 | } elseif (isset($relationship['type']) && isset($relationship['id'])) { |
||
| 55 | $formName = $this->typeToFormName($relationship['type']); |
||
| 56 | $result[$name][$formName] = $relationship; |
||
| 57 | } |
||
| 58 | } |
||
| 59 | } |
||
| 60 | } else { |
||
| 61 | $data = ArrayHelper::getValue($array, 'data', []); |
||
| 62 | View Code Duplication | foreach ($data as $relationLink) { |
|
| 63 | if (isset($relationLink['type']) && isset($relationLink['id'])) { |
||
| 64 | $formName = $this->typeToFormName($relationLink['type']); |
||
| 65 | $result[$formName][] = $relationLink; |
||
| 66 | } |
||
| 67 | } |
||
| 68 | } |
||
| 69 | return isset($result) ? $result : $array; |
||
| 70 | } |
||
| 71 | |||
| 96 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.