• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

h4kuna / data-type / 36112491191

25 Sep 2026 08:21AM UTC coverage: 96.788% (+0.2%) from 96.618%
36112491191

push

github

h4kuna
build: require PHP 8.2, test on PHP 8.2 to 8.5

ActiveWait is a readonly class, which needs PHP 8.2. Static analysis
workflow now runs on main, which is the actual default branch.

663 of 685 relevant lines covered (96.79%)

0.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.83
/src/Basic/Arrays.php
1
<?php declare(strict_types=1);
2

3
namespace h4kuna\DataType\Basic;
4

5
use h4kuna\DataType;
6
use Nette\StaticClass;
7
use Nette\Utils\Strings;
8
use h4kuna\DataType\Basic\Strings as MyStrings;
9
use Stringable;
10

11
final class Arrays
12
{
13
        use StaticClass;
14

15
        /**
16
         * @template TKey of array-key
17
         * @template TValue
18
         * @template TDefault
19
         *
20
         * Better array_combine where values array does not need same size.
21
         * @param array<TKey> $keys
22
         * @param array<TValue> $values
23
         * @param TDefault $value
24
         * @return array<TKey, TValue|TDefault>
25
         */
26
        public static function combine(array $keys, array $values, mixed $value = null): array
1 ✔
27
        {
28
                $diff = count($keys) - count($values);
1 ✔
29

30
                if ($diff > 0) {
1 ✔
31
                        $values = array_merge($values, array_fill(0, $diff, $value));
1 ✔
32
                } elseif ($diff < 0) {
1 ✔
33
                        throw new DataType\Exceptions\InvalidArgumentsException('Array of values can\'t be bigger than keys.');
1 ✔
34
                }
35

36
                return array_combine($keys, $values);
1 ✔
37
        }
38

39

40
        /**
41
         * @deprecated use \h4kuna\DataType\Basic\Strings::startsWith
42
         */
43
        public static function startWith(string $haystack, string ...$needle): bool
1 ✔
44
        {
45
                return MyStrings::startWith($haystack, ...$needle);
1 ✔
46
        }
47

48

49
        /**
50
         * strict in_array
51
         * @param list<mixed>|array<mixed> $haystack
52
         */
53
        public static function contains(string $needle, array $haystack): bool
54
        {
55
                return in_array($needle, $haystack, true);
×
56
        }
57

58

59
        /**
60
         * @param array<scalar|Stringable|null> $array
61
         * @deprecated use join
62
         * Implode only values where strlen > 0 and you can define keys.
63
         */
64
        public static function concatWs(string $glue, array $array): string
1 ✔
65
        {
66
                return self::join($array, $glue);
1 ✔
67
        }
68

69

70
        /**
71
         * The original implode/join(',', ['', null, false, 'A']) return ',,,A' right is 'A'.
72
         * @param array<scalar|Stringable|null> $array
73
         */
74
        public static function join(array $array, string $delimiter = ','): string
1 ✔
75
        {
76
                return implode($delimiter, self::filter($array));
1 ✔
77
        }
78

79

80
        /**
81
         * The original explode(',', '') return [''] right is [].
82
         * @param non-empty-string $delimiter
83
         *
84
         * @return array<string>
85
         */
86
        public static function explode(string $value, string $delimiter = ',', ?int $limit = null): array
1 ✔
87
        {
88
                if ($value === '') {
1 ✔
89
                        return [];
1 ✔
90
                }
91

92
                return $limit === null
1 ✔
93
                        ? explode($delimiter, $value)
1 ✔
94
                        : explode($delimiter, $value, $limit);
1 ✔
95
        }
96

97

98
        /**
99
         * Remove false, empty string and null. Keys are preserved.
100
         * @template TKey of array-key
101
         * @template TValue
102
         *
103
         * @param array<TKey, TValue> $array
104
         * @return array<TKey, TValue>
105
         */
106
        public static function filter(array $array): array
1 ✔
107
        {
108
                return array_filter($array, static fn (mixed $value): bool => $value !== false && $value !== '' && $value !== null);
1 ✔
109
        }
110

111

112
        /**
113
         * COALESCE similar behavior database.
114
         * @param iterable<string|int, mixed> $array
115
         */
116
        public static function coalesce(iterable $array): mixed
1 ✔
117
        {
118
                foreach ($array as $v) {
1 ✔
119
                        if ($v !== null) {
1 ✔
120
                                return $v;
1 ✔
121
                        }
122
                }
123

124
                return null;
1 ✔
125
        }
126

127

128
        /**
129
         * Unset keys from array.
130
         * @template TValue
131
         *
132
         * @param array<TValue>|list<TValue> $array
133
         * @param string|int ...$keys
134
         * @return ($array is list ? list<TValue> : array<TValue>)
135
     */
136
        public static function unsetKeys(&$array, ...$keys): array
1 ✔
137
        {
138
                $out = [];
1 ✔
139
                foreach ($keys as $key) {
1 ✔
140
                        if (array_key_exists($key, $array)) {
1 ✔
141
                                $out[$key] = $array[$key];
1 ✔
142
                                unset($array[$key]);
1 ✔
143
                        }
144
                }
145

146
                return $out;
1 ✔
147
        }
148

149

150
        /**
151
         * @template T
152
         * @param array<T> $values
153
         * @param array<int|string> $keys
154
         * @return array<T>
155
         */
156
        public static function intersectKeys(array $values, array $keys): array
1 ✔
157
        {
158
                return array_intersect_key($values, array_flip($keys));
1 ✔
159
        }
160

161

162
        /**
163
         * @return array<int>
164
         */
165
        public static function generateNumbers(int $from, int $to): array
1 ✔
166
        {
167
                $values = range($from, $to, ($from < $to) ? 1 : -1);
1 ✔
168

169
                return array_combine($values, $values);
1 ✔
170
        }
171

172

173
        /**
174
         * @param array<scalar|Stringable|null> $array1
175
         * @param array<scalar|Stringable|null> $array2
176
         * @param array<scalar|Stringable|null> ...$arrays
177
         * @return list<scalar|Stringable|null>
178
         */
179
        public static function mergeUnique(array $array1, array $array2, array ...$arrays): array
1 ✔
180
        {
181
                return array_values(array_unique(array_merge($array1, $array2, ...$arrays)));
1 ✔
182
        }
183

184

185
        /**
186
         * @return list<string>
187
         */
188
        public static function text2Array(string $text): array
1 ✔
189
        {
190
                $existsNewMethod = method_exists(Strings::class, 'unixNewLines'); // @phpstan-ignore-line
1 ✔
191
                return explode("\n", $existsNewMethod
1 ✔
192
                        ? Strings::unixNewLines($text)
1 ✔
193
                        : Strings::normalizeNewLines($text)
1 ✔
194
                );
195
        }
196

197
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc