GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( f17b75...a63301 )
by Freek
05:29
created

Str::replaceFirst()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 2
dl 16
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\String;
4
5
use ArrayAccess;
6
use Spatie\String\Exceptions\ErrorCreatingStringException;
7
use Spatie\String\Exceptions\UnknownFunctionException;
8
use Spatie\String\Exceptions\UnsetOffsetException;
9
use Spatie\String\Integrations\Underscore;
10
11
/**
12
 * Magic methods provided by underscore are documented here.
13
 *
14
 * @see \Underscore\Methods\StringsMethods
15
 *
16
 * @method \Spatie\String\Str accord($count, $many, $one, $zero = null)
17
 * @method \Spatie\String\Str random($length = 16)
18
 * @method \Spatie\String\Str quickRandom($length = 16)
19
 * @method randomStrings($words, $length = 10)
20
 * @method bool endsWith($needles)
21
 * @method bool isIp()
22
 * @method bool isEmail()
23
 * @method bool isUrl()
24
 * @method bool startsWith()
25
 * @method bool find($needle, $caseSensitive = false, $absolute = false)
26
 * @method array slice($slice)
27
 * @method \Spatie\String\Str sliceFrom($slice)
28
 * @method \Spatie\String\Str sliceTo($slice)
29
 * @method \Spatie\String\Str baseClass()
30
 * @method \Spatie\String\Str prepend($with)
31
 * @method \Spatie\String\Str append($with)
32
 * @method \Spatie\String\Str limit($limit = 100, $end = '...')
33
 * @method \Spatie\String\Str remove($remove)
34
 * @method \Spatie\String\Str replace($replace, $with)
35
 * @method \Spatie\String\Str toggle($first, $second, $loose = false)
36
 * @method \Spatie\String\Str slugify($separator = '-')
37
 * @method array explode($with, $limit = null)
38
 * @method \Spatie\String\Str lower()
39
 * @method \Spatie\String\Str plural()
40
 * @method \Spatie\String\Str singular()
41
 * @method \Spatie\String\Str upper()
42
 * @method \Spatie\String\Str title()
43
 * @method \Spatie\String\Str words($words = 100, $end = '...')
44
 * @method \Spatie\String\Str toPascalCase()
45
 * @method \Spatie\String\Str toSnakeCase()
46
 * @method \Spatie\String\Str toCamelCase()
47
 */
48
class Str implements ArrayAccess
49
{
50
    /**
51
     * @var string
52
     */
53
    protected $string;
54
55
    /**
56
     * @param string $string
57
     */
58
    public function __construct($string = '')
59
    {
60
        if (is_array($string)) {
61
            throw new ErrorCreatingStringException('Can\'t create string from an array');
62
        }
63
64
        if (is_object($string) && !method_exists($string, '__toString')) {
65
            throw new ErrorCreatingStringException(
66
                'Can\'t create string from an object that doesn\'t implement __toString'
67
            );
68
        }
69
70
        $this->string = (string) $string;
71
    }
72
73
    /**
74
     * @return string
75
     */
76
    public function __toString()
77
    {
78
        return $this->string;
79
    }
80
81
    /**
82
     * Get the string between the given start and end.
83
     *
84
     * @param $start
85
     * @param $end
86
     *
87
     * @return \Spatie\String\Str
88
     */
89
    public function between($start, $end)
90
    {
91
        if ($start == '' && $end == '') {
92
            return $this;
93
        }
94
95
        if ($start != '' && strpos($this->string, $start) === false) {
96
            return new static();
97
        }
98
99
        if ($end != '' && strpos($this->string, $end) === false) {
100
            return new static();
101
        }
102
103 View Code Duplication
        if ($start == '') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
            return new static(substr($this->string, 0, strpos($this->string, $end)));
105
        }
106
107 View Code Duplication
        if ($end == '') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
            return new static(substr($this->string, strpos($this->string, $start) + strlen($start)));
109
        }
110
111
        $stringWithoutStart = explode($start, $this->string)[1];
112
113
        $middle = explode($end, $stringWithoutStart)[0];
114
115
        return new static($middle);
116
    }
117
118
    /**
119
     * Convert the string to uppercase.
120
     *
121
     * @return \Spatie\String\Str
122
     */
123
    public function toUpper()
124
    {
125
        return new static(strtoupper($this->string));
126
    }
127
128
    /**
129
     * Convert the string to lowercase.
130
     *
131
     * @return \Spatie\String\Str
132
     */
133
    public function toLower()
134
    {
135
        return new static(strtolower($this->string));
136
    }
137
138
    /**
139
     * Shortens a string in a pretty way. It will clean it by trimming
140
     * it, remove all double spaces and html. If the string is then still
141
     * longer than the specified $length it will be shortened. The end
142
     * of the string is always a full word concatinated with the
143
     * specified moreTextIndicator.
144
     *
145
     * @param int    $length
146
     * @param string $moreTextIndicator
147
     *
148
     * @return \Spatie\String\Str
149
     */
150
    public function tease($length = 200, $moreTextIndicator = '...')
151
    {
152
        $sanitizedString = $this->sanitizeForTease($this->string);
153
154
        if (strlen($sanitizedString) == 0) {
155
            return new static();
156
        }
157
158
        if (strlen($sanitizedString) <= $length) {
159
            return new static($sanitizedString);
160
        }
161
162
        $ww = wordwrap($sanitizedString, $length, "\n");
163
        $shortenedString = substr($ww, 0, strpos($ww, "\n")).$moreTextIndicator;
164
165
        return new static($shortenedString);
166
    }
167
168
    /**
169
     * Sanitize the string for teasing.
170
     *
171
     * @param $string
172
     *
173
     * @return string
174
     */
175
    private function sanitizeForTease($string)
176
    {
177
        $string = trim($string);
178
179
        //remove html
180
        $string = strip_tags($string);
181
182
        //replace multiple spaces
183
        $string = preg_replace("/\s+/", ' ', $string);
184
185
        return $string;
186
    }
187
188
    /**
189
     * Replace the first occurrence of a string.
190
     *
191
     * @param $search
192
     * @param $replace
193
     *
194
     * @return \Spatie\String\Str
195
     */
196 View Code Duplication
    public function replaceFirst($search, $replace)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
197
    {
198
        if ($search == '') {
199
            return $this;
200
        }
201
202
        $position = strpos($this->string, $search);
203
204
        if ($position === false) {
205
            return $this;
206
        }
207
208
        $resultString = substr_replace($this->string, $replace, $position, strlen($search));
209
210
        return new static($resultString);
211
    }
212
213
    /**
214
     * Replace the last occurrence of a string.
215
     *
216
     * @param $search
217
     * @param $replace
218
     *
219
     * @return \Spatie\String\Str
220
     */
221 View Code Duplication
    public function replaceLast($search, $replace)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
222
    {
223
        if ($search == '') {
224
            return $this;
225
        }
226
227
        $position = strrpos($this->string, $search);
228
229
        if ($position === false) {
230
            return $this;
231
        }
232
233
        $resultString = substr_replace($this->string, $replace, $position, strlen($search));
234
235
        return new static($resultString);
236
    }
237
238
    /**
239
     * Prefix a string.
240
     *
241
     * @param $string
242
     *
243
     * @return \Spatie\String\Str
244
     */
245
    public function prefix($string)
246
    {
247
        return new static($string.$this->string);
248
    }
249
250
    /**
251
     * Suffix a string.
252
     *
253
     * @param $string
254
     *
255
     * @return \Spatie\String\Str
256
     */
257
    public function suffix($string)
258
    {
259
        return new static($this->string.$string);
260
    }
261
262
    /**
263
     * Concatenate a string.
264
     *
265
     * @param $string
266
     *
267
     * @return \Spatie\String\Str
268
     */
269
    public function concat($string)
270
    {
271
        return $this->suffix($string);
272
    }
273
274
    /**
275
     * Get the possessive version of a string.
276
     *
277
     * @return \Spatie\String\Str
278
     */
279
    public function possessive()
280
    {
281
        return new static($this->string.'\''.($this->string[strlen($this->string) - 1] != 's' ? 's' : ''));
282
    }
283
284
    /**
285
     * Get a segment from a string based on a delimiter.
286
     * Returns an empty string when the offset doesn't exist.
287
     * Use a negative index to start counting from the last element.
288
     *
289
     * @param string $delimiter
290
     * @param int    $index
291
     *
292
     * @return \Spatie\String\Str
293
     */
294
    public function segment($delimiter, $index)
295
    {
296
        $segments = explode($delimiter, $this->string);
297
298
        if ($index < 0) {
299
            $segments = array_reverse($segments);
300
            $index = abs($index) - 1;
301
        }
302
303
        $segment = isset($segments[$index]) ? $segments[$index] : '';
304
305
        return new static($segment);
306
    }
307
308
    /**
309
     * Get the first segment from a string based on a delimiter.
310
     *
311
     * @param string $delimiter
312
     *
313
     * @return \Spatie\String\Str
314
     */
315
    public function firstSegment($delimiter)
316
    {
317
        return (new static($this->string))->segment($delimiter, 0);
318
    }
319
320
    /**
321
     * Get the last segment from a string based on a delimiter.
322
     *
323
     * @param string $delimiter
324
     *
325
     * @return \Spatie\String\Str
326
     */
327
    public function lastSegment($delimiter)
328
    {
329
        return (new static($this->string))->segment($delimiter, -1);
330
    }
331
332
    /**
333
     * Pop (remove) the last segment of a string based on a delimiter.
334
     *
335
     * @param string $delimiter
336
     *
337
     * @return \Spatie\String\Str
338
     */
339
    public function pop($delimiter)
340
    {
341
        return (new static($this->string))->replaceLast($delimiter.$this->lastSegment($delimiter), '');
342
    }
343
344
    /**
345
     * Strip whitespace (or other characters) from the beginning and end of a string.
346
     *
347
     * @param string $characterMask
348
     *
349
     * @return \Spatie\String\Str
350
     */
351
    public function trim($characterMask = " \t\n\r\0\x0B")
352
    {
353
        return new static(trim($this->string, $characterMask));
354
    }
355
356
    /**
357
     * Alias for find.
358
     *
359
     * @param array|string $needle
360
     * @param bool         $caseSensitive
361
     * @param bool         $absolute
362
     *
363
     * @return bool
364
     */
365
    public function contains($needle, $caseSensitive = false, $absolute = false)
366
    {
367
        return $this->find($needle, $caseSensitive, $absolute);
368
    }
369
370
    /**
371
     * Unknown methods calls will be handled by various integrations.
372
     *
373
     * @param $method
374
     * @param $args
375
     *
376
     * @throws UnknownFunctionException
377
     *
378
     * @return mixed|\Spatie\String\Str
379
     */
380
    public function __call($method, $args)
381
    {
382
        $underscore = new Underscore();
383
384
        if ($underscore->isSupportedMethod($method)) {
385
            return $underscore->call($this, $method, $args);
386
        }
387
388
        throw new UnknownFunctionException(sprintf('String function %s does not exist', $method));
389
    }
390
391
    /**
392
     * Whether a offset exists.
393
     *
394
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
395
     *
396
     * @param mixed $offset An offset to check for.
397
     *
398
     * @return bool true on success or false on failure.
399
     *              The return value will be casted to boolean if non-boolean was returned.
400
     */
401
    public function offsetExists($offset)
402
    {
403
        return strlen($this->string) >= ($offset + 1);
404
    }
405
406
    /**
407
     * Offset to retrieve.
408
     *
409
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
410
     *
411
     * @param mixed $offset The offset to retrieve.
412
     *
413
     * @return mixed Can return all value types.
414
     */
415
    public function offsetGet($offset)
416
    {
417
        $character = substr($this->string, $offset, 1);
418
419
        return new static($character ?: '');
420
    }
421
422
    /**
423
     * Offset to set.
424
     *
425
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
426
     *
427
     * @param mixed $offset The offset to assign the value to.
428
     * @param mixed $value  The value to set.
429
     */
430
    public function offsetSet($offset, $value)
431
    {
432
        $this->string[$offset] = $value;
433
    }
434
435
    /**
436
     * Offset to unset.
437
     *
438
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
439
     *
440
     * @param mixed $offset The offset to unset.
441
     *
442
     * @throws UnsetOffsetException
443
     */
444
    public function offsetUnset($offset)
445
    {
446
        throw new UnsetOffsetException();
447
    }
448
}
449