Passed
Push — main ( aa46eb...a8156f )
by Andrey
01:23
created

Str::removeSpaces()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Helldar\Support\Helpers;
4
5
use Helldar\Support\Facades\Helpers\Instance as InstanceHelper;
6
use Illuminate\Contracts\Support\DeferringDisplayableValue;
0 ignored issues
show
Bug introduced by
The type Illuminate\Contracts\Sup...ferringDisplayableValue was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Illuminate\Contracts\Support\Htmlable;
0 ignored issues
show
Bug introduced by
The type Illuminate\Contracts\Support\Htmlable was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
final class Str
10
{
11
    /**
12
     * The cache of snake-cased words.
13
     *
14
     * @var array
15
     */
16
    protected static $snakeCache = [];
17
18
    /**
19
     * The cache of camel-cased words.
20
     *
21
     * @var array
22
     */
23
    protected static $camelCache = [];
24
25
    /**
26
     * The cache of studly-cased words.
27
     *
28
     * @var array
29
     */
30
    protected static $studlyCache = [];
31
32
    protected $escaping_methods = [
33
        DeferringDisplayableValue::class => 'resolveDisplayableValue',
34
        Htmlable::class                  => 'toHtml',
35
    ];
36
37
    /**
38
     * Escape HTML special characters in a string.
39
     *
40
     * @param  string|null  $value
41
     * @param  bool  $double
42
     *
43
     * @return string|null
44
     */
45 2
    public function e(?string $value, bool $double = true): ?string
46
    {
47 2
        if ($escaped = InstanceHelper::callOf($this->escaping_methods, $value)) {
48
            return $escaped;
49
        }
50
51 2
        return htmlspecialchars($value, ENT_QUOTES, 'UTF-8', $double);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $string of htmlspecialchars() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
        return htmlspecialchars(/** @scrutinizer ignore-type */ $value, ENT_QUOTES, 'UTF-8', $double);
Loading history...
52
    }
53
54
    /**
55
     * Convert special HTML entities back to characters.
56
     *
57
     * @param  string|null  $value
58
     *
59
     * @return string|null
60
     */
61 2
    public function de(?string $value): ?string
62
    {
63 2
        return htmlspecialchars_decode($value, ENT_QUOTES);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $string of htmlspecialchars_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

63
        return htmlspecialchars_decode(/** @scrutinizer ignore-type */ $value, ENT_QUOTES);
Loading history...
64
    }
65
66
    /**
67
     * Replacing multiple spaces with a single space.
68
     *
69
     * @param  string|null  $value
70
     *
71
     * @return string|null
72
     */
73 2
    public function removeSpaces(?string $value): ?string
74
    {
75 2
        return preg_replace('!\s+!', ' ', $value);
76
    }
77
78
    /**
79
     * Get a string according to an integer value.
80
     *
81
     * @param  float  $number
82
     * @param  array  $choice
83
     * @param  string|null  $extra
84
     *
85
     * @return string
86
     */
87 2
    public function choice(float $number, array $choice = [], string $extra = null): string
88
    {
89 2
        $number = (int) $number;
90 2
        $mod    = $number % 10;
91
92
        switch (true) {
93 2
            case $mod === 0:
94 2
            case $mod >= 5 && $mod <= 9:
95 2
            case ($number % 100 >= 11) && ($number % 100 <= 20):
96 2
                $result = $choice[2] ?? '';
97 2
                break;
98
99 2
            case $mod >= 2 && $mod <= 4:
100
                $result = $choice[1] ?? '';
101
                break;
102
103
            default:
104 2
                $result = $choice[0] ?? '';
105
        }
106
107 2
        if (empty($extra)) {
108 2
            return trim($result);
109
        }
110
111 2
        return implode(' ', [trim($result), trim($extra)]);
112
    }
113
114
    /**
115
     * Begin a string with a single instance of a given value.
116
     *
117
     * @see https://github.com/illuminate/support/blob/master/Str.php
118
     *
119
     * @param  string|null  $value
120
     * @param  string  $prefix
121
     *
122
     * @return string
123
     */
124 2
    public function start(?string $value, string $prefix): string
125
    {
126 2
        $quoted = preg_quote($prefix, '/');
127
128 2
        return $prefix . preg_replace('/^(?:' . $quoted . ')+/u', '', $value);
129
    }
130
131
    /**
132
     * Cap a string with a single instance of a given value.
133
     *
134
     * @see https://github.com/illuminate/support/blob/master/Str.php
135
     *
136
     * @param  string  $value
137
     * @param  string  $cap
138
     *
139
     * @return string
140
     */
141 2
    public function finish(string $value, string $cap = '/'): string
142
    {
143 2
        $quoted = preg_quote($cap, '/');
144
145 2
        return preg_replace('/(?:' . $quoted . ')+$/u', '', $value) . $cap;
146
    }
147
148
    /**
149
     *  Determine if a given string starts with a given substring.
150
     *
151
     * @see https://github.com/illuminate/support/blob/master/Str.php
152
     *
153
     * @param  string  $haystack
154
     * @param  string|string[]  $needles
155
     *
156
     * @return bool
157
     */
158 58
    public function startsWith(string $haystack, $needles): bool
159
    {
160 58
        foreach ((array) $needles as $needle) {
161 58
            if ((string) $needle !== '' && strncmp($haystack, $needle, strlen($needle)) === 0) {
162 58
                return true;
163
            }
164
        }
165
166 28
        return false;
167
    }
168
169
    /**
170
     * Determine if a given string ends with a given substring.
171
     *
172
     * @see https://github.com/illuminate/support/blob/master/Str.php
173
     *
174
     * @param  string  $haystack
175
     * @param  string|string[]  $needles
176
     *
177
     * @return bool
178
     */
179 2
    public function endsWith(string $haystack, $needles): bool
180
    {
181 2
        foreach ((array) $needles as $needle) {
182 2
            if ($needle !== '' && substr($haystack, -strlen($needle)) === (string) $needle) {
183 2
                return true;
184
            }
185
        }
186
187 2
        return false;
188
    }
189
190
    /**
191
     * Convert the given string to lower-case.
192
     *
193
     * @see https://github.com/illuminate/support/blob/master/Str.php
194
     *
195
     * @param  string|null  $value
196
     *
197
     * @return string
198
     */
199 75
    public function lower(?string $value): string
200
    {
201 75
        return mb_strtolower($value, 'UTF-8');
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $string of mb_strtolower() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

201
        return mb_strtolower(/** @scrutinizer ignore-type */ $value, 'UTF-8');
Loading history...
202
    }
203
204
    /**
205
     * Convert the given string to upper-case.
206
     *
207
     * @param  string|null  $value
208
     *
209
     * @return string
210
     */
211 2
    public function upper(?string $value): ?string
212
    {
213 2
        return mb_strtoupper($value, 'UTF-8');
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $string of mb_strtoupper() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

213
        return mb_strtoupper(/** @scrutinizer ignore-type */ $value, 'UTF-8');
Loading history...
214
    }
215
216
    /**
217
     * Convert a value to studly caps case.
218
     *
219
     * @see https://github.com/illuminate/support/blob/master/Str.php
220
     *
221
     * @param  string|null  $value
222
     *
223
     * @return string|null
224
     */
225 33
    public function studly(?string $value): ?string
226
    {
227 33
        $key = $value;
228
229 33
        if (isset(self::$studlyCache[$key])) {
230 3
            return self::$studlyCache[$key];
231
        }
232
233 30
        $value = ucwords(str_replace(['-', '_'], ' ', $value));
234
235 30
        return self::$studlyCache[$key] = str_replace(' ', '', $value);
236
    }
237
238
    /**
239
     * Convert a value to camel case.
240
     *
241
     * @see https://github.com/illuminate/support/blob/master/Str.php
242
     *
243
     * @param  string|null  $value
244
     *
245
     * @return string|null
246
     */
247 30
    public function camel(?string $value): ?string
248
    {
249 30
        if (isset(self::$camelCache[$value])) {
250 1
            return self::$camelCache[$value];
251
        }
252
253 29
        return self::$camelCache[$value] = lcfirst($this->studly($value));
254
    }
255
256
    /**
257
     * Convert a string to snake case.
258
     *
259
     * @see https://github.com/illuminate/support/blob/master/Str.php
260
     *
261
     * @param  string|null  $value
262
     * @param  string  $delimiter
263
     *
264
     * @return string|null
265
     */
266 2
    public function snake(?string $value, string $delimiter = '_'): ?string
267
    {
268 2
        $key = $value;
269
270 2
        if (isset(self::$snakeCache[$key][$delimiter])) {
271 1
            return self::$snakeCache[$key][$delimiter];
272
        }
273
274 1
        if (! ctype_lower($value)) {
275 1
            $value = preg_replace('/\s+/u', '', ucwords($value));
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $string of ucwords() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

275
            $value = preg_replace('/\s+/u', '', ucwords(/** @scrutinizer ignore-type */ $value));
Loading history...
276
277 1
            $value = $this->lower(preg_replace('/(.)(?=[A-Z])/u', '$1' . $delimiter, $value));
278
        }
279
280 1
        return self::$snakeCache[$key][$delimiter] = $value;
281
    }
282
283
    /**
284
     * Return the length of the given string.
285
     *
286
     * @see https://github.com/illuminate/support/blob/master/Str.php
287
     *
288
     * @param  string|null  $value
289
     * @param  string|null  $encoding
290
     *
291
     * @return int
292
     */
293 2
    public function length(?string $value, string $encoding = null): int
294
    {
295 2
        return $encoding
296 2
            ? mb_strlen($value, $encoding)
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $string of mb_strlen() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

296
            ? mb_strlen(/** @scrutinizer ignore-type */ $value, $encoding)
Loading history...
297 2
            : mb_strlen($value);
298
    }
299
300
    /**
301
     * Returns the portion of string specified by the start and length parameters.
302
     *
303
     * @see https://github.com/illuminate/support/blob/master/Str.php
304
     *
305
     * @param  string  $string
306
     * @param  int  $start
307
     * @param  int|null  $length
308
     *
309
     * @return string|null
310
     */
311 2
    public function substr(string $string, int $start, int $length = null): ?string
312
    {
313 2
        return mb_substr($string, $start, $length, 'UTF-8');
314
    }
315
316
    /**
317
     * Get the portion of a string before the first occurrence of a given value.
318
     *
319
     * @see https://github.com/illuminate/support/blob/master/Str.php
320
     *
321
     * @param  string  $subject
322
     * @param  string  $search
323
     *
324
     * @return string
325
     */
326
    public function before(string $subject, string $search): ?string
327
    {
328
        return ! empty($search) ? explode($search, $subject)[0] : null;
329
    }
330
331
    /**
332
     * Return the remainder of a string after the first occurrence of a given value.
333
     *
334
     * @see https://github.com/illuminate/support/blob/master/Str.php
335
     *
336
     * @param  string  $subject
337
     * @param  string  $search
338
     *
339
     * @return string
340
     */
341 56
    public function after(string $subject, string $search): ?string
342
    {
343 56
        return ! empty($search) ? array_reverse(explode($search, $subject, 2))[0] : null;
344
    }
345
}
346