Test Setup Failed
Pull Request — latest (#3)
by Mark
32:07
created

Normalize   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 22
eloc 56
c 1
b 0
f 0
dl 0
loc 172
rs 10
ccs 58
cts 58
cp 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
B emojis() 0 41 9
A toObject() 0 3 1
A shortcodes() 0 12 2
A properties() 0 12 2
A toArray() 0 3 1
A toString() 0 3 2
A toInteger() 0 3 1
A toBoolean() 0 3 1
A setType() 0 15 2
A toFloat() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace UnicornFail\Emoji\Util;
6
7
use UnicornFail\Emoji\Dataset\Emoji;
8
9
/**
10
 * {@internal}
11
 */
12
final class Normalize
13
{
14
    public const TYPE_METHODS = [
15
        'array' => 'toArray',
16
        'bool' => 'toBoolean',
17
        'boolean' => 'toBoolean',
18
        'double' => 'toFloat',
19
        'float' => 'toFloat',
20
        'int' => 'toInteger',
21
        'integer' => 'toInteger',
22
        'object' => 'toObject',
23
        'string' => 'toString',
24
    ];
25
26
    public const TYPES = ['array', 'bool', 'boolean', 'double', 'float', 'int', 'integer', 'null', 'object', 'string'];
27
28
    /**
29
     * @param mixed   $emojis
30
     * @param Emoji[] $dataset
31
     *
32
     * @return Emoji[]
33
     */
34
    public static function emojis($emojis = [], string $index = 'hexcode', array &$dataset = []): array
35 33
    {
36
        if ($emojis instanceof Emoji) {
37 33
            $emojis = [$emojis];
38
        } elseif ($emojis instanceof \Iterator) {
39 18
            $emojis = \iterator_to_array($emojis);
40 18
        } else {
41 18
            $emojis = (array) $emojis;
42 3
        }
43
44
        /** @var Emoji[] $normalized */
45 18
        $normalized = [];
46
47 18
        /** @var iterable|string[]|Emoji[] $emoji */
48
        foreach ($emojis as &$emoji) {
49
            if (\is_array($emoji)) {
50
                $emoji = new Emoji($emoji);
51 33
            }
52
53
            if (! $emoji instanceof Emoji) {
54
                throw new \RuntimeException(\sprintf('Passed array item must be an instance of %s.', Emoji::class));
55
            }
56
57
            $normalized[] = $emoji;
58
        }
59 33
60
        foreach ($normalized as $emoji) {
61 33
            /** @var string[] $keys */
62 9
            $keys = \array_filter((array) $emoji->$index);
63 33
            foreach ($keys as $k) {
64 21
                if (isset($dataset[$k])) {
65
                    continue;
66 33
                }
67
68
                $dataset[$k] = $emoji;
69
70 33
                self::emojis($emoji->skins, $index, $dataset);
71
            }
72
        }
73 33
74 18
        return $dataset;
75 9
    }
76
77
    /**
78 18
     * @param mixed[]  $properties
79 3
     * @param string[] $types
80
     *
81
     * @return mixed[]
82 18
     */
83
    public static function properties(array $properties, array $types): array
84
    {
85 33
        $properties += \array_fill_keys(\array_keys($types), null);
86
87
        /** @psalm-var string $value */
88 699
        foreach ($properties as $key => $value) {
89
            /** @psalm-var string $value */
90
            $value            = Property::cast($types[$key] ?? '?string', $value);
91 699
            $properties[$key] = $value;
92 675
        }
93
94
        return $properties;
95
    }
96
97 24
    /**
98 24
     * @param mixed $value
99 15
     */
100
    public static function setType(&$value, string $type): bool
101
    {
102 24
        // Immediately return if not a valid type.
103 24
        if (! isset(self::TYPE_METHODS[$type])) {
104
            $value = null;
105
106 24
            return false;
107 24
        }
108 24
109 24
        $method = self::TYPE_METHODS[$type];
110 15
111 15
        /** @psalm-var string $value */
112
        $value = self::$method($value);
113
114
        return true;
115 24
    }
116
117
    /**
118
     * @param string|array<array-key, string> $shortcode
119
     *
120
     * @return string[]
121
     */
122
    public static function shortcodes($shortcode): array
123
    {
124 57
        $normalized = [];
125
126 57
        /** @var string|string[] $shortcodes */
127
        foreach (\func_get_args() as $shortcodes) {
128
            $normalized = \array_merge($normalized, \array_map(static function ($shortcode) {
129 57
                return \preg_replace('/[^a-z0-9-]/', '-', \strtolower(\trim((string) $shortcode, ':(){}[]')));
130
            }, (array) $shortcodes));
131 57
        }
132 57
133
        return \array_values(\array_unique(\array_filter($normalized)));
134
    }
135 57
136
    /**
137
     * @param mixed $value
138
     *
139
     * @return mixed[]
140
     */
141 90
    public static function toArray($value): array
142
    {
143
        return (array) ($value ?? []);
144 90
    }
145 6
146
    /**
147 6
     * @param mixed $value
148
     */
149
    public static function toBoolean($value): bool
150 84
    {
151
        return (bool) ($value ?? false);
152
    }
153 84
154
    /**
155 84
     * @param mixed $value
156
     */
157
    public static function toFloat($value): float
158
    {
159
        return (float) ($value ?? 0.0);
160
    }
161
162
    /**
163 738
     * @param mixed $value
164
     */
165 738
    public static function toInteger($value): int
166
    {
167
        return (int) ($value ?? 0);
168 738
    }
169
170 126
    /**
171 738
     * @param mixed $value
172
     */
173
    public static function toObject($value): object
174 738
    {
175
        return (object) ($value ?? new \stdClass());
176
    }
177
178
    /**
179
     * @param mixed $value
180
     */
181
    public static function toString($value): string
182 3
    {
183
        return \is_array($value) ? \implode($value) : (string) ($value ?? '');
184 3
    }
185
}
186