Passed
Push — latest ( 5fe1f6...9a2102 )
by Mark
02:40
created

create_dataset()   C

Complexity

Conditions 13
Paths 44

Size

Total Lines 46
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 46
rs 6.6166
cc 13
nc 44
nop 2

How to fix   Complexity   

Long Method

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:

1
#!/usr/bin/env php
2
<?php
3
4
declare(strict_types=1);
5
6
const BASE_DIRECTORY = __DIR__ . '/../';
7
require_once BASE_DIRECTORY . '/vendor/autoload.php';
8
9
use UnicornFail\Emoji\Dataset;
10
use UnicornFail\Emoji\EmojibaseInterface;
11
use UnicornFail\Emoji\EmojibaseShortcodeInterface;
12
use UnicornFail\Emoji\Util\Normalize;
13
14
const BUILD_DIRECTORY          = BASE_DIRECTORY . '/build';
15
const EMOJIBASE_DATA_DIRECTORY = BASE_DIRECTORY . '/node_modules/emojibase-data';
16
17
if (! is_dir(EMOJIBASE_DATA_DIRECTORY) || ! interface_exists(EmojibaseInterface::class)) {
18
    throw new \RuntimeException('You must first run `npm install && npm run build` to build the datasets.');
19
}
20
21
/**
22
 * @param mixed[] $shortcodes
23
 */
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
}
71
72
// Clean up dataset directory.
73
if (is_dir(Dataset::DIRECTORY)) {
74
    $files = new RecursiveIteratorIterator(
75
        new RecursiveDirectoryIterator(Dataset::DIRECTORY, RecursiveDirectoryIterator::SKIP_DOTS),
76
        RecursiveIteratorIterator::CHILD_FIRST
77
    );
78
    foreach ($files as $fileInfo) {
79
        $todo = ($fileInfo->isDir() ? 'rmdir' : 'unlink');
80
        $todo($fileInfo->getRealPath());
81
    }
82
83
    /** @scrutinizer ignore-unhandled */ @rmdir(Dataset::DIRECTORY);
84
}
85
86
$baseDirectory = realpath(BASE_DIRECTORY);
87
88
// Archive datasets.
89
foreach (EmojibaseInterface::SUPPORTED_LOCALES as $locale) {
90
    foreach (EmojibaseShortcodeInterface::PRESETS as $preset) {
91
        // Skip presets that don't exist.
92
        $file = sprintf('%s/%s/shortcodes/%s.json', EMOJIBASE_DATA_DIRECTORY, $locale, $preset);
93
        if (! file_exists($file) || ! ($c = file_get_contents($file)) || ! ($shortcodes = json_decode($c, true))) {
94
            continue;
95
        }
96
97
        $destination = sprintf('%s/%s/%s.gz', Dataset::DIRECTORY, $locale, $preset);
98
        $relative    = str_replace($baseDirectory . '/src/..', '.', $destination);
99
        $directory   = dirname($destination);
100
        /** @scrutinizer ignore-unhandled */ @mkdir($directory, 0775, true);
101
102
        echo sprintf("Archiving %s\n", $relative);
103
        $dataset = create_dataset($locale, $shortcodes);
104
        file_put_contents($destination, $dataset->archive());
105
    }
106
}
107
108
echo "\nFinished!\n";
109