Passed
Pull Request — main (#22)
by Andrey
28:59 queued 16:23
created

Str::choice()   B

Complexity

Conditions 9
Paths 10

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 9
eloc 16
c 2
b 0
f 0
nc 10
nop 3
dl 0
loc 25
rs 8.0555
1
<?php
2
3
namespace Helldar\Support\Helpers;
4
5
use Helldar\Support\Facades\Helpers\Instance;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Helldar\Support\Helpers\Instance. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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
    public function e(?string $value, bool $double = true): ?string
46
    {
47
        if (empty($value)) {
48
            return null;
49
        }
50
51
        if ($escaped = Instance::callOf($this->escaping_methods, $value)) {
52
            return $escaped;
53
        }
54
55
        return htmlspecialchars($value, ENT_QUOTES, 'UTF-8', $double);
56
    }
57
58
    /**
59
     * Convert special HTML entities back to characters.
60
     *
61
     * @param  string|null  $value
62
     *
63
     * @return string|null
64
     */
65
    public function de(?string $value): ?string
66
    {
67
        if (empty($value)) {
68
            return null;
69
        }
70
71
        return htmlspecialchars_decode($value, ENT_QUOTES);
72
    }
73
74
    /**
75
     * Replacing multiple spaces with a single space.
76
     *
77
     * @param  string|null  $value
78
     *
79
     * @return string|null
80
     */
81
    public function removeSpaces(?string $value): ?string
82
    {
83
        return preg_replace('!\s+!', ' ', $value);
84
    }
85
86
    /**
87
     * Get a string according to an integer value.
88
     *
89
     * @param  float  $number
90
     * @param  array  $choice
91
     * @param  string|null  $extra
92
     *
93
     * @return string
94
     */
95
    public function choice(float $number, array $choice = [], string $extra = null): string
96
    {
97
        $number = (int) $number;
98
        $mod    = $number % 10;
99
100
        switch (true) {
101
            case $mod === 0:
102
            case $mod >= 5 && $mod <= 9:
103
            case ($number % 100 >= 11) && ($number % 100 <= 20):
104
                $result = $choice[2] ?? '';
105
                break;
106
107
            case $mod >= 2 && $mod <= 4:
108
                $result = $choice[1] ?? '';
109
                break;
110
111
            default:
112
                $result = $choice[0] ?? '';
113
        }
114
115
        if (empty($extra)) {
116
            return trim($result);
117
        }
118
119
        return implode(' ', [trim($result), trim($extra)]);
120
    }
121
122
    /**
123
     * Begin a string with a single instance of a given value.
124
     *
125
     * @see https://github.com/illuminate/support/blob/master/Str.php
126
     *
127
     * @param  string|null  $value
128
     * @param  string  $prefix
129
     *
130
     * @return string
131
     */
132
    public function start(?string $value, string $prefix): string
133
    {
134
        $quoted = preg_quote($prefix, '/');
135
136
        return $prefix . preg_replace('/^(?:' . $quoted . ')+/u', '', $value);
137
    }
138
139
    /**
140
     * Cap a string with a single instance of a given value.
141
     *
142
     * @see https://github.com/illuminate/support/blob/master/Str.php
143
     *
144
     * @param  string  $value
145
     * @param  string  $cap
146
     *
147
     * @return string
148
     */
149
    public function finish(string $value, string $cap = '/'): string
150
    {
151
        $quoted = preg_quote($cap, '/');
152
153
        return preg_replace('/(?:' . $quoted . ')+$/u', '', $value) . $cap;
154
    }
155
156
    /**
157
     *  Determine if a given string starts with a given substring.
158
     *
159
     * @see https://github.com/illuminate/support/blob/master/Str.php
160
     *
161
     * @param  string  $haystack
162
     * @param  string|string[]  $needles
163
     *
164
     * @return bool
165
     */
166
    public function startsWith(string $haystack, $needles): bool
167
    {
168
        foreach ((array) $needles as $needle) {
169
            if ((string) $needle !== '' && strncmp($haystack, $needle, strlen($needle)) === 0) {
170
                return true;
171
            }
172
        }
173
174
        return false;
175
    }
176
177
    /**
178
     * Determine if a given string ends with a given substring.
179
     *
180
     * @see https://github.com/illuminate/support/blob/master/Str.php
181
     *
182
     * @param  string  $haystack
183
     * @param  string|string[]  $needles
184
     *
185
     * @return bool
186
     */
187
    public function endsWith(string $haystack, $needles): bool
188
    {
189
        foreach ((array) $needles as $needle) {
190
            if ($needle !== '' && substr($haystack, -strlen($needle)) === (string) $needle) {
191
                return true;
192
            }
193
        }
194
195
        return false;
196
    }
197
198
    /**
199
     * Convert the given string to lower-case.
200
     *
201
     * @see https://github.com/illuminate/support/blob/master/Str.php
202
     *
203
     * @param  string|null  $value
204
     *
205
     * @return string
206
     */
207
    public function lower(?string $value): string
208
    {
209
        return mb_strtolower($value, 'UTF-8');
210
    }
211
212
    /**
213
     * Convert a value to studly caps case.
214
     *
215
     * @see https://github.com/illuminate/support/blob/master/Str.php
216
     *
217
     * @param  string|null  $value
218
     *
219
     * @return string|null
220
     */
221
    public function studly(?string $value): ?string
222
    {
223
        if (empty($value)) {
224
            return null;
225
        }
226
227
        $key = $value;
228
229
        if (isset(self::$studlyCache[$key])) {
230
            return self::$studlyCache[$key];
231
        }
232
233
        $value = ucwords(str_replace(['-', '_'], ' ', $value));
234
235
        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
    public function camel(?string $value): ?string
248
    {
249
        if (empty($value)) {
250
            return null;
251
        }
252
253
        if (isset(self::$camelCache[$value])) {
254
            return self::$camelCache[$value];
255
        }
256
257
        return self::$camelCache[$value] = lcfirst($this->studly($value));
258
    }
259
260
    /**
261
     * Convert a string to snake case.
262
     *
263
     * @see https://github.com/illuminate/support/blob/master/Str.php
264
     *
265
     * @param  string|null  $value
266
     * @param  string  $delimiter
267
     *
268
     * @return string|null
269
     */
270
    public function snake(?string $value, string $delimiter = '_'): ?string
271
    {
272
        if (empty($value)) {
273
            return null;
274
        }
275
276
        $key = $value;
277
278
        if (isset(self::$snakeCache[$key][$delimiter])) {
279
            return self::$snakeCache[$key][$delimiter];
280
        }
281
282
        if (! ctype_lower($value)) {
283
            $value = preg_replace('/\s+/u', '', ucwords($value));
284
285
            $value = $this->lower(preg_replace('/(.)(?=[A-Z])/u', '$1' . $delimiter, $value));
286
        }
287
288
        return self::$snakeCache[$key][$delimiter] = $value;
289
    }
290
291
    /**
292
     * Return the length of the given string.
293
     *
294
     * @see https://github.com/illuminate/support/blob/master/Str.php
295
     *
296
     * @param  string|null  $value
297
     * @param  string|null  $encoding
298
     *
299
     * @return int
300
     */
301
    public function length(?string $value, string $encoding = null): int
302
    {
303
        return $encoding
304
            ? mb_strlen($value, $encoding)
305
            : mb_strlen($value);
306
    }
307
308
    /**
309
     * Returns the portion of string specified by the start and length parameters.
310
     *
311
     * @see https://github.com/illuminate/support/blob/master/Str.php
312
     *
313
     * @param  string  $string
314
     * @param  int  $start
315
     * @param  int|null  $length
316
     *
317
     * @return string|null
318
     */
319
    public function substr(string $string, int $start, int $length = null): ?string
320
    {
321
        return mb_substr($string, $start, $length, 'UTF-8');
322
    }
323
324
    /**
325
     * Get the portion of a string before the first occurrence of a given value.
326
     *
327
     * @see https://github.com/illuminate/support/blob/master/Str.php
328
     *
329
     * @param  string  $subject
330
     * @param  string  $search
331
     *
332
     * @return string
333
     */
334
    public function before(string $subject, string $search): ?string
335
    {
336
        return $search === '' ? $subject : explode($search, $subject)[0];
337
    }
338
339
    /**
340
     * Return the remainder of a string after the first occurrence of a given value.
341
     *
342
     * @see https://github.com/illuminate/support/blob/master/Str.php
343
     *
344
     * @param  string  $subject
345
     * @param  string  $search
346
     *
347
     * @return string
348
     */
349
    public function after(string $subject, string $search): string
350
    {
351
        return $search === '' ? $subject : array_reverse(explode($search, $subject, 2))[0];
352
    }
353
}
354