| Conditions | 13 |
| Paths | 44 |
| Total Lines | 46 |
| Code Lines | 22 |
| 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 | #!/usr/bin/env php |
||
| 24 | function create_dataset(string $locale, array $shortcodes): Dataset |
||
| 25 | { |
||
| 26 | $data = null; |
||
| 27 | |||
| 28 | // Load the data, key by hexcode. |
||
| 29 | $file = sprintf('%s/%s/data.json', EMOJIBASE_DATA_DIRECTORY, $locale); |
||
| 30 | if (file_exists($file) && ($c = file_get_contents($file)) && ($json = json_decode($c, true))) { |
||
| 31 | $data = array_column($json, null, 'hexcode'); |
||
| 32 | } |
||
| 33 | |||
| 34 | if (! isset($data)) { |
||
| 35 | throw new \RuntimeException(sprintf('Unable to load JSON: %s', $file)); |
||
| 36 | } |
||
| 37 | |||
| 38 | // Merge any skin variations into the main list (faster performance). |
||
| 39 | foreach ($data as $hexcode => &$item) { |
||
| 40 | if (! isset($item['shortcodes'])) { |
||
| 41 | $item['shortcodes'] = []; |
||
| 42 | } |
||
| 43 | |||
| 44 | // Process shortcodes. |
||
| 45 | $item += ['shortcodes' => []]; |
||
| 46 | if (isset($shortcodes[$hexcode])) { |
||
| 47 | $item['shortcodes'] = Normalize::shortcodes($item['shortcodes'], $shortcodes[$hexcode]); |
||
| 48 | } |
||
| 49 | |||
| 50 | if (! isset($item['skins']) || ! count($item['skins'])) { |
||
| 51 | continue; |
||
| 52 | } |
||
| 53 | |||
| 54 | $item['skins'] = array_column($item['skins'], null, 'hexcode'); |
||
| 55 | |||
| 56 | foreach ($item['skins'] as $skinHexcode => &$skin) { |
||
| 57 | if (isset($data[$skinHexcode])) { |
||
| 58 | continue; |
||
| 59 | } |
||
| 60 | |||
| 61 | // Process shortcodes. |
||
| 62 | $skin += ['shortcodes' => []]; |
||
| 63 | if (isset($shortcodes[$skinHexcode])) { |
||
| 64 | $skin['shortcodes'] = Normalize::shortcodes($skin['shortcodes'], $shortcodes[$skinHexcode]); |
||
| 65 | } |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | return new Dataset($data); |
||
| 70 | } |
||
| 109 |