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

h4kuna / data-type / 36115897674

25 Sep 2026 08:59AM UTC coverage: 96.275% (-0.5%) from 96.774%
36115897674

push

github

h4kuna
build: enable checked exceptions in phpstan

DataTypeException is checked, tests are excluded from the rule.

672 of 698 relevant lines covered (96.28%)

0.96 hits per line

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

98.21
/src/Basic/Strings.php
1
<?php declare(strict_types=1);
2

3
namespace h4kuna\DataType\Basic;
4

5
use h4kuna\DataType\Exceptions\InvalidArgumentsException;
6
use h4kuna\DataType\Exceptions\InvalidTypeException;
7
use h4kuna\DataType\Location;
8
use Nette\StaticClass;
9

10
final class Strings
11
{
12
        use StaticClass;
13

14
        /**
15
         * @throws InvalidTypeException
16
         */
17
        public static function nullable(mixed $value): ?string
1 ✔
18
        {
19
                return $value === null ? null : self::from($value);
1 ✔
20
        }
21

22

23
        public static function strokeToPoint(string $value): string
1 ✔
24
        {
25
                return strtr($value, ',', '.');
1 ✔
26
        }
27

28

29
        public static function key(string|int ...$values): string
30
        {
31
                return self::join($values, "\x00");
×
32
        }
33

34

35
        /**
36
         * @throws InvalidTypeException
37
         */
38
        public static function toFloat(string $value): float
1 ✔
39
        {
40
                return Floats::from($value);
1 ✔
41
        }
42

43
        public static function startWith(string $haystack, string ...$needle): bool
1 ✔
44
        {
45
                foreach ($needle as $str) {
1 ✔
46
                        if (str_starts_with($haystack, $str)) {
1 ✔
47
                                return true;
1 ✔
48
                        }
49
                }
50

51
                return false;
1 ✔
52
        }
53

54

55
        /**
56
         * @throws InvalidTypeException
57
         */
58
        public static function from(mixed $value): string
1 ✔
59
        {
60
                if (is_int($value) || is_float($value) || is_null($value)) {
1 ✔
61
                        return (string) $value;
1 ✔
62
                } elseif (is_string($value) === false) {
1 ✔
63
                        throw InvalidTypeException::createInvalidString($value);
1 ✔
64
                }
65

66
                return $value;
1 ✔
67
        }
68

69

70
        /**
71
         * @throws InvalidTypeException
72
         */
73
        public static function toInt(string $value): int
1 ✔
74
        {
75
                return Integer::from($value);
1 ✔
76
        }
77

78

79
        /**
80
         * @return array{lat: float, long: float}
81
         * @throws InvalidArgumentsException
82
         */
83
        public static function toGps(string $value): array
1 ✔
84
        {
85
                return Location\Gps::fromString($value);
1 ✔
86
        }
87

88

89
        /**
90
         * @return array<string, true>
91
         */
92
        public static function toSet(string $value): array
1 ✔
93
        {
94
                return Set::fromString($value);
1 ✔
95
        }
96

97

98
        /**
99
         * foo_bar => FooBar
100
         */
101
        public static function toPascal(string $string): string
1 ✔
102
        {
103
                return ucfirst(self::toCamel($string));
1 ✔
104
        }
105

106

107
        /**
108
         * foo_bar => fooBar
109
         */
110
        public static function toCamel(string $string): string
1 ✔
111
        {
112
                return (string) preg_replace_callback('/_([a-z])/', static function (array $find): string {
1 ✔
113
                        return strtoupper($find[1]);
1 ✔
114
                }, $string);
1 ✔
115
        }
116

117

118
        /**
119
         * @deprecated use Arrays::explode()
120
         *
121
         * @param non-empty-string $delimiter
122
         * @return array<string>
123
         */
124
        public static function split(string $value, string $delimiter = ', '): array
1 ✔
125
        {
126
                return Arrays::explode($value, $delimiter);
1 ✔
127
        }
128

129

130
        /**
131
         * @deprecated use Arrays::join()
132
         *
133
         * @param array<scalar|null> $array
134
         */
135
        public static function join(array $array, string $delimiter = ', '): string
1 ✔
136
        {
137
                return Arrays::join($array, $delimiter);
1 ✔
138
        }
139

140

141
        /**
142
         * FooBar => foo_bar
143
         */
144
        public static function toUnderscore(string $string): string
1 ✔
145
        {
146
                return strtolower((string) preg_replace_callback('/(.)([A-Z][a-z])|([a-z])([A-Z])/', static function (
1 ✔
147
                        array $find,
148
                ): string {
149
                        if ($find[1] !== '') {
1 ✔
150
                                return $find[1] . '_' . $find[2];
1 ✔
151
                        }
152

153
                        return $find[3] . '_' . $find[4];
1 ✔
154
                }, $string));
1 ✔
155
        }
156

157

158
        public static function replaceStart(
1 ✔
159
                string $subject,
160
                string $search,
161
                string $replacement = '',
162
        ): string
163
        {
164
                return self::strictReplace($subject, $search, $replacement, '^%s', 1);
1 ✔
165
        }
166

167

168
        private static function strictReplace(
1 ✔
169
                string $subject,
170
                string $search,
171
                string $replacement,
172
                string $pattern,
173
                int $limit = -1,
174
        ): string
175
        {
176
                return preg_replace(
1 ✔
177
                        sprintf(self::padIfNeed($pattern, '#', STR_PAD_BOTH), preg_quote($search, '#')),
1 ✔
178
                        $replacement,
179
                        $subject,
180
                        $limit,
181
                ) ?? $search;
1 ✔
182
        }
183

184

185
        public static function padIfNeed(string $string, string $padString = '/', int $padType = STR_PAD_LEFT): string
1 ✔
186
        {
187
                $length = mb_strlen($padString);
1 ✔
188
                $prefix = $suffix = '';
1 ✔
189
                if (($padType === STR_PAD_LEFT || $padType === STR_PAD_BOTH) && mb_substr($string, 0, $length) !== $padString) {
1 ✔
190
                        $prefix = $padString;
1 ✔
191
                }
192

193
                if (($padType === STR_PAD_RIGHT || $padType === STR_PAD_BOTH) && mb_substr($string, -$length) !== $padString) {
1 ✔
194
                        $suffix = $padString;
1 ✔
195
                }
196

197
                return "$prefix$string$suffix";
1 ✔
198
        }
199

200

201
        public static function replaceEnd(
1 ✔
202
                string $subject,
203
                string $search,
204
                string $replacement = '',
205
        ): string
206
        {
207
                return self::strictReplace($subject, $search, $replacement, '%s$', 1);
1 ✔
208
        }
209

210
}
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