Passed
Push — main ( 6c3117...d6ca5c )
by Andrey
01:47
created

Str::isDoesntEmpty()   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 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
     * @see https://github.com/illuminate/support/blob/master/Str.php
208
     *
209
     * @param  string|null  $value
210
     *
211
     * @return string
212
     */
213 2
    public function upper(?string $value): ?string
214
    {
215 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

215
        return mb_strtoupper(/** @scrutinizer ignore-type */ $value, 'UTF-8');
Loading history...
216
    }
217
218
    /**
219
     * Convert a value to studly caps case.
220
     *
221
     * @see https://github.com/illuminate/support/blob/master/Str.php
222
     *
223
     * @param  string|null  $value
224
     *
225
     * @return string|null
226
     */
227 37
    public function studly(?string $value): ?string
228
    {
229 37
        $key = $value;
230
231 37
        if (isset(self::$studlyCache[$key])) {
232 3
            return self::$studlyCache[$key];
233
        }
234
235 34
        $value = ucwords(str_replace(['-', '_'], ' ', $value));
236
237 34
        return self::$studlyCache[$key] = str_replace(' ', '', $value);
238
    }
239
240
    /**
241
     * Convert a value to camel case.
242
     *
243
     * @see https://github.com/illuminate/support/blob/master/Str.php
244
     *
245
     * @param  string|null  $value
246
     *
247
     * @return string|null
248
     */
249 34
    public function camel(?string $value): ?string
250
    {
251 34
        if (isset(self::$camelCache[$value])) {
252 1
            return self::$camelCache[$value];
253
        }
254
255 33
        return self::$camelCache[$value] = lcfirst($this->studly($value));
256
    }
257
258
    /**
259
     * Convert a string to snake case.
260
     *
261
     * @see https://github.com/illuminate/support/blob/master/Str.php
262
     *
263
     * @param  string|null  $value
264
     * @param  string  $delimiter
265
     *
266
     * @return string|null
267
     */
268 2
    public function snake(?string $value, string $delimiter = '_'): ?string
269
    {
270 2
        $key = $value;
271
272 2
        if (isset(self::$snakeCache[$key][$delimiter])) {
273 1
            return self::$snakeCache[$key][$delimiter];
274
        }
275
276 1
        if (! ctype_lower($value)) {
277 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

277
            $value = preg_replace('/\s+/u', '', ucwords(/** @scrutinizer ignore-type */ $value));
Loading history...
278
279 1
            $value = $this->lower(preg_replace('/(.)(?=[A-Z])/u', '$1' . $delimiter, $value));
280
        }
281
282 1
        return self::$snakeCache[$key][$delimiter] = $value;
283
    }
284
285
    /**
286
     * Return the length of the given string.
287
     *
288
     * @see https://github.com/illuminate/support/blob/master/Str.php
289
     *
290
     * @param  string|null  $value
291
     * @param  string|null  $encoding
292
     *
293
     * @return int
294
     */
295 2
    public function length(?string $value, string $encoding = null): int
296
    {
297 2
        return $encoding
298 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

298
            ? mb_strlen(/** @scrutinizer ignore-type */ $value, $encoding)
Loading history...
299 2
            : mb_strlen($value);
300
    }
301
302
    /**
303
     * Returns the portion of string specified by the start and length parameters.
304
     *
305
     * @see https://github.com/illuminate/support/blob/master/Str.php
306
     *
307
     * @param  string  $string
308
     * @param  int  $start
309
     * @param  int|null  $length
310
     *
311
     * @return string|null
312
     */
313 2
    public function substr(string $string, int $start, int $length = null): ?string
314
    {
315 2
        return mb_substr($string, $start, $length, 'UTF-8');
316
    }
317
318
    /**
319
     * Get the portion of a string before the first occurrence of a given value.
320
     *
321
     * @see https://github.com/illuminate/support/blob/master/Str.php
322
     *
323
     * @param  string  $subject
324
     * @param  string  $search
325
     *
326
     * @return string
327
     */
328
    public function before(string $subject, string $search): ?string
329
    {
330
        return ! empty($search) ? explode($search, $subject)[0] : null;
331
    }
332
333
    /**
334
     * Return the remainder of a string after the first occurrence of a given value.
335
     *
336
     * @see https://github.com/illuminate/support/blob/master/Str.php
337
     *
338
     * @param  string  $subject
339
     * @param  string  $search
340
     *
341
     * @return string
342
     */
343 56
    public function after(string $subject, string $search): ?string
344
    {
345 56
        return ! empty($search) ? array_reverse(explode($search, $subject, 2))[0] : null;
346
    }
347
348
    /**
349
     * Determine if a given string contains a given substring.
350
     *
351
     * @see https://github.com/illuminate/support/blob/master/Str.php
352
     *
353
     * @param  string  $haystack
354
     * @param  string|string[]  $needles
355
     *
356
     * @return bool
357
     */
358 2
    public function contains(string $haystack, $needles): bool
359
    {
360 2
        foreach ((array) $needles as $needle) {
361 2
            if (! empty($needle) && mb_strpos($haystack, $needle) !== false) {
362 2
                return true;
363
            }
364
        }
365
366 2
        return false;
367
    }
368
369
    /**
370
     * Determines if the value is empty.
371
     *
372
     * @param  mixed  $value
373
     *
374
     * @return bool
375
     */
376 8
    public function isEmpty($value): bool
377
    {
378 8
        $value = is_string($value) ? trim($value) : $value;
379
380 8
        return empty($value) && ! is_numeric($value) && (is_string($value) || is_null($value));
381
    }
382
383
    /**
384
     * Determines if the value is doesn't empty.
385
     *
386
     * @param  mixed  $value
387
     *
388
     * @return bool
389
     */
390 2
    public function doesntEmpty($value): bool
391
    {
392 2
        return ! $this->isEmpty($value);
393
    }
394
}
395