Passed
Pull Request — main (#22)
by Andrey
20:42 queued 05:40
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
    public function choice(float $number, array $choice = [], string $extra = null): string
87
    {
88
        $number = (int) $number;
89
        $mod    = $number % 10;
90
91
        switch (true) {
92
            case $mod === 0:
93
            case $mod >= 5 && $mod <= 9:
94
            case ($number % 100 >= 11) && ($number % 100 <= 20):
95
                $result = $choice[2] ?? '';
96
                break;
97
98
            case $mod >= 2 && $mod <= 4:
99
                $result = $choice[1] ?? '';
100
                break;
101
102
            default:
103
                $result = $choice[0] ?? '';
104
        }
105
106
        if (empty($extra)) {
107
            return trim($result);
108
        }
109
110
        return implode(' ', [trim($result), trim($extra)]);
111
    }
112
113
    /**
114
     * Begin a string with a single instance of a given value.
115
     *
116
     * @see https://github.com/illuminate/support/blob/master/Str.php
117
     *
118
     * @param  string|null  $value
119
     * @param  string  $prefix
120
     *
121
     * @return string
122
     */
123
    public function start(?string $value, string $prefix): string
124
    {
125
        $quoted = preg_quote($prefix, '/');
126
127
        return $prefix . preg_replace('/^(?:' . $quoted . ')+/u', '', $value);
128
    }
129
130
    /**
131
     * Cap a string with a single instance of a given value.
132
     *
133
     * @see https://github.com/illuminate/support/blob/master/Str.php
134
     *
135
     * @param  string  $value
136
     * @param  string  $cap
137
     *
138
     * @return string
139
     */
140
    public function finish(string $value, string $cap = '/'): string
141
    {
142
        $quoted = preg_quote($cap, '/');
143
144
        return preg_replace('/(?:' . $quoted . ')+$/u', '', $value) . $cap;
145
    }
146
147
    /**
148
     *  Determine if a given string starts with a given substring.
149
     *
150
     * @see https://github.com/illuminate/support/blob/master/Str.php
151
     *
152
     * @param  string  $haystack
153
     * @param  string|string[]  $needles
154
     *
155
     * @return bool
156
     */
157
    public function startsWith(string $haystack, $needles): bool
158
    {
159
        foreach ((array) $needles as $needle) {
160
            if ((string) $needle !== '' && strncmp($haystack, $needle, strlen($needle)) === 0) {
161
                return true;
162
            }
163
        }
164
165
        return false;
166
    }
167
168
    /**
169
     * Determine if a given string ends with a given substring.
170
     *
171
     * @see https://github.com/illuminate/support/blob/master/Str.php
172
     *
173
     * @param  string  $haystack
174
     * @param  string|string[]  $needles
175
     *
176
     * @return bool
177
     */
178
    public function endsWith(string $haystack, $needles): bool
179
    {
180
        foreach ((array) $needles as $needle) {
181
            if ($needle !== '' && substr($haystack, -strlen($needle)) === (string) $needle) {
182
                return true;
183
            }
184
        }
185
186
        return false;
187
    }
188
189
    /**
190
     * Convert the given string to lower-case.
191
     *
192
     * @see https://github.com/illuminate/support/blob/master/Str.php
193
     *
194
     * @param  string|null  $value
195
     *
196
     * @return string
197
     */
198
    public function lower(?string $value): string
199
    {
200
        return mb_strtolower($value, 'UTF-8');
201
    }
202
203
    /**
204
     * Convert a value to studly caps case.
205
     *
206
     * @see https://github.com/illuminate/support/blob/master/Str.php
207
     *
208
     * @param  string|null  $value
209
     *
210
     * @return string|null
211
     */
212
    public function studly(?string $value): ?string
213
    {
214
        if (empty($value)) {
215
            return null;
216
        }
217
218
        $key = $value;
219
220
        if (isset(self::$studlyCache[$key])) {
221
            return self::$studlyCache[$key];
222
        }
223
224
        $value = ucwords(str_replace(['-', '_'], ' ', $value));
225
226
        return self::$studlyCache[$key] = str_replace(' ', '', $value);
227
    }
228
229
    /**
230
     * Convert a value to camel case.
231
     *
232
     * @see https://github.com/illuminate/support/blob/master/Str.php
233
     *
234
     * @param  string|null  $value
235
     *
236
     * @return string|null
237
     */
238
    public function camel(?string $value): ?string
239
    {
240
        if (empty($value)) {
241
            return null;
242
        }
243
244
        if (isset(self::$camelCache[$value])) {
245
            return self::$camelCache[$value];
246
        }
247
248
        return self::$camelCache[$value] = lcfirst($this->studly($value));
249
    }
250
251
    /**
252
     * Convert a string to snake case.
253
     *
254
     * @see https://github.com/illuminate/support/blob/master/Str.php
255
     *
256
     * @param  string|null  $value
257
     * @param  string  $delimiter
258
     *
259
     * @return string|null
260
     */
261
    public function snake(?string $value, string $delimiter = '_'): ?string
262
    {
263
        if (empty($value)) {
264
            return null;
265
        }
266
267
        $key = $value;
268
269
        if (isset(self::$snakeCache[$key][$delimiter])) {
270
            return self::$snakeCache[$key][$delimiter];
271
        }
272
273
        if (! ctype_lower($value)) {
274
            $value = preg_replace('/\s+/u', '', ucwords($value));
275
276
            $value = $this->lower(preg_replace('/(.)(?=[A-Z])/u', '$1' . $delimiter, $value));
277
        }
278
279
        return self::$snakeCache[$key][$delimiter] = $value;
280
    }
281
282
    /**
283
     * Return the length of the given string.
284
     *
285
     * @see https://github.com/illuminate/support/blob/master/Str.php
286
     *
287
     * @param  string|null  $value
288
     * @param  string|null  $encoding
289
     *
290
     * @return int
291
     */
292
    public function length(?string $value, string $encoding = null): int
293
    {
294
        return $encoding
295
            ? mb_strlen($value, $encoding)
296
            : mb_strlen($value);
297
    }
298
299
    /**
300
     * Returns the portion of string specified by the start and length parameters.
301
     *
302
     * @see https://github.com/illuminate/support/blob/master/Str.php
303
     *
304
     * @param  string  $string
305
     * @param  int  $start
306
     * @param  int|null  $length
307
     *
308
     * @return string|null
309
     */
310
    public function substr(string $string, int $start, int $length = null): ?string
311
    {
312
        return mb_substr($string, $start, $length, 'UTF-8');
313
    }
314
315
    /**
316
     * Get the portion of a string before the first occurrence of a given value.
317
     *
318
     * @see https://github.com/illuminate/support/blob/master/Str.php
319
     *
320
     * @param  string  $subject
321
     * @param  string  $search
322
     *
323
     * @return string
324
     */
325
    public function before(string $subject, string $search): ?string
326
    {
327
        return $search === '' ? $subject : explode($search, $subject)[0];
328
    }
329
330
    /**
331
     * Return the remainder of a string after the first occurrence of a given value.
332
     *
333
     * @see https://github.com/illuminate/support/blob/master/Str.php
334
     *
335
     * @param  string  $subject
336
     * @param  string  $search
337
     *
338
     * @return string
339
     */
340
    public function after(string $subject, string $search): string
341
    {
342
        return $search === '' ? $subject : array_reverse(explode($search, $subject, 2))[0];
343
    }
344
}
345