| Conditions | 10 |
| Paths | 9 |
| Total Lines | 50 |
| Code Lines | 25 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 99 | private function parseJsonToCodeTitleMap(string $json): array |
||
| 100 | { |
||
| 101 | $decoded = json_decode($json, true); |
||
| 102 | |||
| 103 | if (JSON_ERROR_NONE !== json_last_error()) { |
||
| 104 | throw new ValidatorException('Invalid JSON for search engine fields.'); |
||
| 105 | } |
||
| 106 | |||
| 107 | if (!is_array($decoded)) { |
||
| 108 | throw new ValidatorException('Search engine fields JSON must be an object or an array.'); |
||
| 109 | } |
||
| 110 | |||
| 111 | // Canonical wrapper: {"fields":[...], ...} |
||
| 112 | if (isset($decoded['fields'])) { |
||
| 113 | if (!is_array($decoded['fields'])) { |
||
| 114 | throw new ValidatorException('"fields" must be an array.'); |
||
| 115 | } |
||
| 116 | |||
| 117 | return $this->parseListOfFields($decoded['fields']); |
||
| 118 | } |
||
| 119 | |||
| 120 | // List format: [{"code":"c","title":"Course"}] |
||
| 121 | if ($this->isList($decoded)) { |
||
| 122 | return $this->parseListOfFields($decoded); |
||
| 123 | } |
||
| 124 | |||
| 125 | // Object formats: |
||
| 126 | // - {"c":"Course"} |
||
| 127 | // - {"course":{"prefix":"C","title":"Course"}} |
||
| 128 | $map = []; |
||
| 129 | |||
| 130 | foreach ($decoded as $key => $value) { |
||
| 131 | if (is_string($value)) { |
||
| 132 | $code = $this->normalizeCode((string) $key); |
||
| 133 | $title = $this->normalizeTitle($value); |
||
| 134 | $map[$code] = $title; |
||
| 135 | continue; |
||
| 136 | } |
||
| 137 | |||
| 138 | if (is_array($value) && isset($value['prefix'], $value['title'])) { |
||
| 139 | $code = $this->normalizeCode((string) $value['prefix']); |
||
| 140 | $title = $this->normalizeTitle($value['title']); |
||
| 141 | $map[$code] = $title; |
||
| 142 | continue; |
||
| 143 | } |
||
| 144 | |||
| 145 | throw new ValidatorException('Invalid search fields JSON structure.'); |
||
| 146 | } |
||
| 147 | |||
| 148 | return $map; |
||
| 149 | } |
||
| 216 |