Passed
Push — main ( 16ab53...91f55c )
by Andrey
01:26
created

Str::studly()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 2
rs 10
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 2
    public function e(?string $value, bool $double = true): ?string
46
    {
47 2
        if ($escaped = Instance::callOf($this->escaping_methods, $value)) {
48
            return $escaped;
49
        }
50
51 2
        return htmlspecialchars($value, ENT_QUOTES, 'UTF-8', $double);
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);
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 48
    public function startsWith(string $haystack, $needles): bool
159
    {
160 48
        foreach ((array) $needles as $needle) {
161 48
            if ((string) $needle !== '' && strncmp($haystack, $needle, strlen($needle)) === 0) {
162 48
                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 65
    public function lower(?string $value): string
200
    {
201 65
        return mb_strtolower($value, 'UTF-8');
202
    }
203
204
    /**
205
     * Convert a value to studly caps case.
206
     *
207
     * @see https://github.com/illuminate/support/blob/master/Str.php
208
     *
209
     * @param  string|null  $value
210
     *
211
     * @return string|null
212
     */
213 33
    public function studly(?string $value): ?string
214
    {
215 33
        $key = $value;
216
217 33
        if (isset(self::$studlyCache[$key])) {
218 3
            return self::$studlyCache[$key];
219
        }
220
221 30
        $value = ucwords(str_replace(['-', '_'], ' ', $value));
222
223 30
        return self::$studlyCache[$key] = str_replace(' ', '', $value);
224
    }
225
226
    /**
227
     * Convert a value to camel case.
228
     *
229
     * @see https://github.com/illuminate/support/blob/master/Str.php
230
     *
231
     * @param  string|null  $value
232
     *
233
     * @return string|null
234
     */
235 30
    public function camel(?string $value): ?string
236
    {
237 30
        if (isset(self::$camelCache[$value])) {
238 1
            return self::$camelCache[$value];
239
        }
240
241 29
        return self::$camelCache[$value] = lcfirst($this->studly($value));
242
    }
243
244
    /**
245
     * Convert a string to snake case.
246
     *
247
     * @see https://github.com/illuminate/support/blob/master/Str.php
248
     *
249
     * @param  string|null  $value
250
     * @param  string  $delimiter
251
     *
252
     * @return string|null
253
     */
254 2
    public function snake(?string $value, string $delimiter = '_'): ?string
255
    {
256 2
        $key = $value;
257
258 2
        if (isset(self::$snakeCache[$key][$delimiter])) {
259 1
            return self::$snakeCache[$key][$delimiter];
260
        }
261
262 1
        if (! ctype_lower($value)) {
263 1
            $value = preg_replace('/\s+/u', '', ucwords($value));
264
265 1
            $value = $this->lower(preg_replace('/(.)(?=[A-Z])/u', '$1' . $delimiter, $value));
266
        }
267
268 1
        return self::$snakeCache[$key][$delimiter] = $value;
269
    }
270
271
    /**
272
     * Return the length of the given string.
273
     *
274
     * @see https://github.com/illuminate/support/blob/master/Str.php
275
     *
276
     * @param  string|null  $value
277
     * @param  string|null  $encoding
278
     *
279
     * @return int
280
     */
281 2
    public function length(?string $value, string $encoding = null): int
282
    {
283 2
        return $encoding
284 2
            ? mb_strlen($value, $encoding)
285 2
            : mb_strlen($value);
286
    }
287
288
    /**
289
     * Returns the portion of string specified by the start and length parameters.
290
     *
291
     * @see https://github.com/illuminate/support/blob/master/Str.php
292
     *
293
     * @param  string  $string
294
     * @param  int  $start
295
     * @param  int|null  $length
296
     *
297
     * @return string|null
298
     */
299 2
    public function substr(string $string, int $start, int $length = null): ?string
300
    {
301 2
        return mb_substr($string, $start, $length, 'UTF-8');
302
    }
303
304
    /**
305
     * Get the portion of a string before the first occurrence of a given value.
306
     *
307
     * @see https://github.com/illuminate/support/blob/master/Str.php
308
     *
309
     * @param  string  $subject
310
     * @param  string  $search
311
     *
312
     * @return string
313
     */
314
    public function before(string $subject, string $search): ?string
315
    {
316
        return ! empty($search) ? explode($search, $subject)[0] : null;
317
    }
318
319
    /**
320
     * Return the remainder of a string after the first occurrence of a given value.
321
     *
322
     * @see https://github.com/illuminate/support/blob/master/Str.php
323
     *
324
     * @param  string  $subject
325
     * @param  string  $search
326
     *
327
     * @return string
328
     */
329 46
    public function after(string $subject, string $search): ?string
330
    {
331 46
        return ! empty($search) ? array_reverse(explode($search, $subject, 2))[0] : null;
332
    }
333
}
334