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 ( 825633...9c20fb )
by Baptiste
05:21
created

Str   F

Complexity

Total Complexity 67

Size/Duplication

Total Lines 681
Duplicated Lines 0 %

Test Coverage

Coverage 97.27%

Importance

Changes 0
Metric Value
eloc 150
dl 0
loc 681
ccs 178
cts 183
cp 0.9727
rs 3.04
c 0
b 0
f 0
wmc 67

49 Methods

Rating   Name   Duplication   Size   Complexity  
A prepend() 0 3 1
A pregQuote() 0 3 1
A append() 0 3 1
A equals() 0 3 1
A sprintf() 0 3 1
A camelize() 0 9 1
A rightTrim() 0 5 2
A leftTrim() 0 5 2
A ucfirst() 0 6 1
A pad() 0 11 1
A contains() 0 8 2
A trim() 0 5 2
A lcfirst() 0 6 1
A length() 0 3 1
A repeat() 0 3 1
A toEncoding() 0 3 1
A toPrimitive() 0 3 1
A rightPad() 0 3 1
A split() 0 14 4
A cspn() 0 14 2
A substring() 0 9 2
A capture() 0 7 2
A wordCount() 0 6 1
A of() 0 3 1
A matches() 0 7 2
A empty() 0 3 1
A getMatches() 0 6 1
A replace() 0 9 2
A __toString() 0 3 1
A stripSlashes() 0 3 1
A leftPad() 0 3 1
A str() 0 12 2
A chunk() 0 11 2
A pregReplace() 0 17 2
A reverse() 0 6 1
A pregSplit() 0 10 2
A shuffle() 0 6 1
A stripCSlashes() 0 3 1
A uniPad() 0 3 1
A toLower() 0 3 1
A words() 0 10 2
A toUpper() 0 3 1
A position() 0 12 2
A encoding() 0 3 1
A __construct() 0 4 1
A takeEnd() 0 3 1
A dropEnd() 0 3 1
A take() 0 3 1
A drop() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like Str often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Str, and based on these observations, apply Extract Interface, too.

1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Immutable;
5
6
use Innmind\Immutable\{
7
    Exception\RegexException,
8
    Exception\SubstringException,
9
    Exception\LogicException
10
};
11
12
class Str implements PrimitiveInterface, StringableInterface
13
{
14
    const PAD_RIGHT = STR_PAD_RIGHT;
15
    const PAD_LEFT = STR_PAD_LEFT;
16
    const PAD_BOTH = STR_PAD_BOTH;
17
    const PREG_NO_FLAGS = 0;
18
    const PREG_SPLIT_NO_EMPTY = PREG_SPLIT_NO_EMPTY;
19
    const PREG_SPLIT_DELIM_CAPTURE = PREG_SPLIT_DELIM_CAPTURE;
20
    const PREG_SPLIT_OFFSET_CAPTURE = PREG_SPLIT_OFFSET_CAPTURE;
21
    const PREG_OFFSET_CAPTURE = PREG_OFFSET_CAPTURE;
22
23
    private $value;
24
    private $encoding;
25
26 524
    public function __construct(string $value, string $encoding = null)
27
    {
28 524
        $this->value = $value;
29 524
        $this->encoding = $encoding ?? \mb_internal_encoding();
30 524
    }
31
32 8
    public static function of(string $value, string $encoding = null): self
33
    {
34 8
        return new self($value, $encoding);
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 2
    public function toPrimitive()
41
    {
42 2
        return $this->value;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 298
    public function __toString(): string
49
    {
50 298
        return $this->value;
51
    }
52
53 12
    public function encoding(): self
54
    {
55 12
        return new self($this->encoding);
0 ignored issues
show
Bug introduced by
It seems like $this->encoding can also be of type boolean; however, parameter $value of Innmind\Immutable\Str::__construct() 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

55
        return new self(/** @scrutinizer ignore-type */ $this->encoding);
Loading history...
56
    }
57
58 30
    public function toEncoding(string $encoding): self
59
    {
60 30
        return new self($this->value, $encoding);
61
    }
62
63
    /**
64
     * Split the string into a collection of ones
65
     *
66
     * @param string $delimiter
67
     *
68
     * @return StreamInterface<self>
69
     */
70 14
    public function split(string $delimiter = null): StreamInterface
71
    {
72 14
        if (\is_null($delimiter) || $delimiter === '') {
73 6
            return $this->chunk();
74
        }
75
76 10
        $parts = \explode($delimiter, $this->value);
77 10
        $stream = new Stream(self::class);
78
79 10
        foreach ($parts as $part) {
80 10
            $stream = $stream->add(new self($part, $this->encoding));
0 ignored issues
show
Bug introduced by
It seems like $this->encoding can also be of type boolean; however, parameter $encoding of Innmind\Immutable\Str::__construct() does only seem to accept null|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

80
            $stream = $stream->add(new self($part, /** @scrutinizer ignore-type */ $this->encoding));
Loading history...
81
        }
82
83 10
        return $stream;
84
    }
85
86
    /**
87
     * Returns a collection of the string splitted by the given chunk size
88
     *
89
     * @param int $size
90
     *
91
     * @return StreamInterface<self>
92
     */
93 14
    public function chunk(int $size = 1): StreamInterface
94
    {
95 14
        $stream = new Stream(self::class);
96 14
        $string = $this;
97
98 14
        while ($string->length() > 0) {
99 14
            $stream = $stream->add($string->substring(0, $size));
100 14
            $string = $string->substring($size);
101
        }
102
103 14
        return $stream;
104
    }
105
106
    /**
107
     * Returns the position of the first occurence of the string
108
     *
109
     * @param string $needle
110
     * @param int $offset
111
     *
112
     * @throws SubstringException If the string is not found
113
     *
114
     * @return int
115
     */
116 12
    public function position(string $needle, int $offset = 0): int
117
    {
118 12
        $position = \mb_strpos($this->value, $needle, $offset, $this->encoding);
119
120 12
        if ($position === false) {
121 6
            throw new SubstringException(\sprintf(
122 6
                'Substring "%s" not found',
123 6
                $needle
124
            ));
125
        }
126
127 10
        return (int) $position;
128
    }
129
130
    /**
131
     * Replace all occurences of the search string with the replacement one
132
     *
133
     * @param string $search
134
     * @param string $replacement
135
     *
136
     * @return self
137
     */
138 4
    public function replace(string $search, string $replacement): self
139
    {
140 4
        if (!$this->contains($search)) {
141 2
            return $this;
142
        }
143
144
        return $this
145 4
            ->split($search)
146 4
            ->join($replacement);
147
    }
148
149
    /**
150
     * Returns the string following the given delimiter
151
     *
152
     * @param string $delimiter
153
     *
154
     * @throws SubstringException If the string is not found
155
     *
156
     * @return self
157
     */
158 6
    public function str(string $delimiter): self
159
    {
160 6
        $sub = \mb_strstr($this->value, $delimiter, false, $this->encoding);
161
162 6
        if ($sub === false) {
163 2
            throw new SubstringException(\sprintf(
164 2
                'Substring "%s" not found',
165 2
                $delimiter
166
            ));
167
        }
168
169 4
        return new self($sub, $this->encoding);
0 ignored issues
show
Bug introduced by
It seems like $this->encoding can also be of type boolean; however, parameter $encoding of Innmind\Immutable\Str::__construct() does only seem to accept null|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

169
        return new self($sub, /** @scrutinizer ignore-type */ $this->encoding);
Loading history...
170
    }
171
172
    /**
173
     * Return the string in upper case
174
     *
175
     * @return self
176
     */
177 6
    public function toUpper(): self
178
    {
179 6
        return new self(\mb_strtoupper($this->value), $this->encoding);
0 ignored issues
show
Bug introduced by
It seems like $this->encoding can also be of type boolean; however, parameter $encoding of Innmind\Immutable\Str::__construct() does only seem to accept null|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

179
        return new self(\mb_strtoupper($this->value), /** @scrutinizer ignore-type */ $this->encoding);
Loading history...
180
    }
181
182
    /**
183
     * Return the string in lower case
184
     *
185
     * @return self
186
     */
187 4
    public function toLower(): self
188
    {
189 4
        return new self(\mb_strtolower($this->value), $this->encoding);
0 ignored issues
show
Bug introduced by
It seems like $this->encoding can also be of type boolean; however, parameter $encoding of Innmind\Immutable\Str::__construct() does only seem to accept null|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

189
        return new self(\mb_strtolower($this->value), /** @scrutinizer ignore-type */ $this->encoding);
Loading history...
190
    }
191
192
    /**
193
     * Return the string length
194
     *
195
     * @return int
196
     */
197 38
    public function length(): int
198
    {
199 38
        return \mb_strlen($this->value, $this->encoding);
200
    }
201
202 2
    public function empty(): bool
203
    {
204 2
        return $this->value === '';
205
    }
206
207
    /**
208
     * Reverse the string
209
     *
210
     * @return self
211
     */
212 2
    public function reverse(): self
213
    {
214
        return $this
215 2
            ->chunk()
216 2
            ->reverse()
217 2
            ->join('');
218
    }
219
220
    /**
221
     * Pad to the right
222
     *
223
     * @param int $length
224
     * @param string $character
225
     *
226
     * @return self
227
     */
228 2
    public function rightPad(int $length, string $character = ' '): self
229
    {
230 2
        return $this->pad($length, $character, self::PAD_RIGHT);
231
    }
232
233
    /**
234
     * Pad to the left
235
     *
236
     * @param int $length
237
     * @param string $character
238
     *
239
     * @return self
240
     */
241 2
    public function leftPad(int $length, string $character = ' '): self
242
    {
243 2
        return $this->pad($length, $character, self::PAD_LEFT);
244
    }
245
246
    /**
247
     * Pad both sides
248
     *
249
     * @param int $length
250
     * @param string $character
251
     *
252
     * @return self
253
     */
254 2
    public function uniPad(int $length, string $character = ' '): self
255
    {
256 2
        return $this->pad($length, $character, self::PAD_BOTH);
257
    }
258
259
    /**
260
     * Find length of initial segment not matching mask
261
     *
262
     * @param string $mask
263
     * @param int $start
264
     * @param int $length
265
     *
266
     * @return int
267
     */
268 2
    public function cspn(string $mask, int $start = 0, int $length = null): int
269
    {
270 2
        if ($length === null) {
271 2
            $value = \strcspn($this->value, $mask, $start);
272
        } else {
273 2
            $value = \strcspn(
274 2
                $this->value,
275 2
                $mask,
276 2
                $start,
277 2
                $length
278
            );
279
        }
280
281 2
        return (int) $value;
282
    }
283
284
    /**
285
     * Repeat the string n times
286
     *
287
     * @param int $repeat
288
     *
289
     * @return self
290
     */
291 2
    public function repeat(int $repeat): self
292
    {
293 2
        return new self(\str_repeat($this->value, $repeat), $this->encoding);
0 ignored issues
show
Bug introduced by
It seems like $this->encoding can also be of type boolean; however, parameter $encoding of Innmind\Immutable\Str::__construct() does only seem to accept null|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

293
        return new self(\str_repeat($this->value, $repeat), /** @scrutinizer ignore-type */ $this->encoding);
Loading history...
294
    }
295
296
    /**
297
     * Shuffle the string
298
     *
299
     * @return self
300
     */
301 4
    public function shuffle(): self
302
    {
303 4
        $parts = $this->chunk()->toPrimitive();
304 4
        \shuffle($parts);
305
306 4
        return new self(\implode('', $parts), $this->encoding);
0 ignored issues
show
Bug introduced by
It seems like $this->encoding can also be of type boolean; however, parameter $encoding of Innmind\Immutable\Str::__construct() does only seem to accept null|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

306
        return new self(\implode('', $parts), /** @scrutinizer ignore-type */ $this->encoding);
Loading history...
307
    }
308
309
    /**
310
     * Strip slashes
311
     *
312
     * @return self
313
     */
314 2
    public function stripSlashes(): self
315
    {
316 2
        return new self(\stripslashes($this->value), $this->encoding);
0 ignored issues
show
Bug introduced by
It seems like $this->encoding can also be of type boolean; however, parameter $encoding of Innmind\Immutable\Str::__construct() does only seem to accept null|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

316
        return new self(\stripslashes($this->value), /** @scrutinizer ignore-type */ $this->encoding);
Loading history...
317
    }
318
319
    /**
320
     * Strip C-like slashes
321
     *
322
     * @return self
323
     */
324 2
    public function stripCSlashes(): self
325
    {
326 2
        return new self(\stripcslashes($this->value), $this->encoding);
0 ignored issues
show
Bug introduced by
It seems like $this->encoding can also be of type boolean; however, parameter $encoding of Innmind\Immutable\Str::__construct() does only seem to accept null|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

326
        return new self(\stripcslashes($this->value), /** @scrutinizer ignore-type */ $this->encoding);
Loading history...
327
    }
328
329
    /**
330
     * Return the word count
331
     *
332
     * @param string $charlist
333
     *
334
     * @return int
335
     */
336 2
    public function wordCount(string $charlist = ''): int
337
    {
338 2
        return (int) \str_word_count(
339 2
            $this->value,
340 2
            0,
341 2
            $charlist
342
        );
343
    }
344
345
    /**
346
     * Return the collection of words
347
     *
348
     * @param string $charlist
349
     *
350
     * @return MapInterface<int, self>
351
     */
352 2
    public function words(string $charlist = ''): MapInterface
353
    {
354 2
        $words = \str_word_count($this->value, 2, $charlist);
355 2
        $map = new Map('int', self::class);
356
357 2
        foreach ($words as $position => $word) {
358 2
            $map = $map->put($position, new self($word, $this->encoding));
0 ignored issues
show
Bug introduced by
It seems like $this->encoding can also be of type boolean; however, parameter $encoding of Innmind\Immutable\Str::__construct() does only seem to accept null|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

358
            $map = $map->put($position, new self($word, /** @scrutinizer ignore-type */ $this->encoding));
Loading history...
359
        }
360
361 2
        return $map;
362
    }
363
364
    /**
365
     * Split the string using a regular expression
366
     *
367
     * @param string $regex
368
     * @param int $limit
369
     *
370
     * @return StreamInterface<self>
371
     */
372 4
    public function pregSplit(string $regex, int $limit = -1): StreamInterface
373
    {
374 4
        $strings = \preg_split($regex, $this->value, $limit);
375 4
        $stream = new Stream(self::class);
376
377 4
        foreach ($strings as $string) {
378 4
            $stream = $stream->add(new self($string, $this->encoding));
0 ignored issues
show
Bug introduced by
It seems like $this->encoding can also be of type boolean; however, parameter $encoding of Innmind\Immutable\Str::__construct() does only seem to accept null|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

378
            $stream = $stream->add(new self($string, /** @scrutinizer ignore-type */ $this->encoding));
Loading history...
379
        }
380
381 4
        return $stream;
382
    }
383
384
    /**
385
     * Check if the string match the given regular expression
386
     *
387
     * @param string $regex
388
     *
389
     * @throws Exception If the regex failed
390
     *
391
     * @return bool
392
     */
393 4
    public function matches(string $regex): bool
394
    {
395 4
        if (\func_num_args() !== 1) {
396
            throw new LogicException('Offset is no longer supported');
397
        }
398
399 4
        return RegExp::of($regex)->matches($this);
400
    }
401
402
    /**
403
     * Return a collection of the elements matching the regex
404
     *
405
     * @deprecated replaced by self::capture, to be removed in 3.0
406
     *
407
     * @param string $regex
408
     * @param int $offset
409
     * @param int $flags
410
     *
411
     * @throws Exception If the regex failed
412
     *
413
     * @return MapInterface<scalar, self>
414
     */
415
    public function getMatches(
416
        string $regex,
417
        int $offset = 0,
418
        int $flags = self::PREG_NO_FLAGS
419
    ): MapInterface {
420
        return $this->capture($regex, $offset, $flags);
421
    }
422
423
    /**
424
     * Return a collection of the elements matching the regex
425
     *
426
     * @param string $regex
427
     *
428
     * @throws Exception If the regex failed
429
     *
430
     * @return MapInterface<scalar, self>
431
     */
432 6
    public function capture(string $regex): MapInterface
433
    {
434 6
        if (\func_num_args() !== 1) {
435
            throw new LogicException('Offset and flags are no longer supported');
436
        }
437
438 6
        return RegExp::of($regex)->capture($this);
439
    }
440
441
    /**
442
     * Replace part of the string by using a regular expression
443
     *
444
     * @param string $regex
445
     * @param string $replacement
446
     * @param int $limit
447
     *
448
     * @throws Exception If the regex failed
449
     *
450
     * @return self
451
     */
452 2
    public function pregReplace(
453
        string $regex,
454
        string $replacement,
455
        int $limit = -1
456
    ): self {
457 2
        $value = \preg_replace(
458 2
            $regex,
459 2
            $replacement,
460 2
            $this->value,
461 2
            $limit
462
        );
463
464 2
        if ($value === null) {
465
            throw new RegexException('', \preg_last_error());
466
        }
467
468 2
        return new self($value, $this->encoding);
0 ignored issues
show
Bug introduced by
It seems like $this->encoding can also be of type boolean; however, parameter $encoding of Innmind\Immutable\Str::__construct() does only seem to accept null|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

468
        return new self($value, /** @scrutinizer ignore-type */ $this->encoding);
Loading history...
469
    }
470
471
    /**
472
     * Return part of the string
473
     *
474
     * @param int $start
475
     * @param int $length
476
     *
477
     * @return self
478
     */
479 34
    public function substring(int $start, int $length = null): self
480
    {
481 34
        if ($this->length() === 0) {
482 2
            return $this;
483
        }
484
485 34
        $sub = \mb_substr($this->value, $start, $length, $this->encoding);
486
487 34
        return new self($sub, $this->encoding);
0 ignored issues
show
Bug introduced by
It seems like $this->encoding can also be of type boolean; however, parameter $encoding of Innmind\Immutable\Str::__construct() does only seem to accept null|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

487
        return new self($sub, /** @scrutinizer ignore-type */ $this->encoding);
Loading history...
488
    }
489
490 2
    public function take(int $size): self
491
    {
492 2
        return $this->substring(0, $size);
493
    }
494
495 2
    public function takeEnd(int $size): self
496
    {
497 2
        return $this->substring(-$size);
498
    }
499
500 2
    public function drop(int $size): self
501
    {
502 2
        return $this->substring($size);
503
    }
504
505 2
    public function dropEnd(int $size): self
506
    {
507 2
        return $this->substring(0, $this->length() - $size);
508
    }
509
510
    /**
511
     * Return a formatted string
512
     *
513
     * @return self
514
     */
515 2
    public function sprintf(...$values): self
516
    {
517 2
        return new self(\sprintf($this->value, ...$values), $this->encoding);
0 ignored issues
show
Bug introduced by
It seems like $this->encoding can also be of type boolean; however, parameter $encoding of Innmind\Immutable\Str::__construct() does only seem to accept null|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

517
        return new self(\sprintf($this->value, ...$values), /** @scrutinizer ignore-type */ $this->encoding);
Loading history...
518
    }
519
520
    /**
521
     * Return the string with the first letter as uppercase
522
     *
523
     * @return self
524
     */
525 4
    public function ucfirst(): self
526
    {
527
        return $this
528 4
            ->substring(0, 1)
529 4
            ->toUpper()
530 4
            ->append((string) $this->substring(1));
531
    }
532
533
    /**
534
     * Return the string with the first letter as lowercase
535
     *
536
     * @return self
537
     */
538 2
    public function lcfirst(): self
539
    {
540
        return $this
541 2
            ->substring(0, 1)
542 2
            ->toLower()
543 2
            ->append((string) $this->substring(1));
544
    }
545
546
    /**
547
     * Return a CamelCase representation of the string
548
     *
549
     * @return self
550
     */
551 2
    public function camelize(): self
552
    {
553
        return $this
554 2
            ->pregSplit('/_| /')
555 2
            ->map(function(self $part) {
556 2
                return $part->ucfirst();
557 2
            })
558 2
            ->join('')
559 2
            ->toEncoding($this->encoding);
0 ignored issues
show
Bug introduced by
It seems like $this->encoding can also be of type boolean; however, parameter $encoding of Innmind\Immutable\Str::toEncoding() 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

559
            ->toEncoding(/** @scrutinizer ignore-type */ $this->encoding);
Loading history...
560
    }
561
562
    /**
563
     * Append a string at the end of the current one
564
     *
565
     * @param string $string
566
     *
567
     * @return self
568
     */
569 8
    public function append(string $string): self
570
    {
571 8
        return new self((string) $this.$string, $this->encoding);
0 ignored issues
show
Bug introduced by
It seems like $this->encoding can also be of type boolean; however, parameter $encoding of Innmind\Immutable\Str::__construct() does only seem to accept null|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

571
        return new self((string) $this.$string, /** @scrutinizer ignore-type */ $this->encoding);
Loading history...
572
    }
573
574
    /**
575
     * Prepend a string at the beginning of the current one
576
     *
577
     * @param string $string
578
     *
579
     * @return self
580
     */
581 2
    public function prepend(string $string): self
582
    {
583 2
        return new self($string.(string) $this, $this->encoding);
0 ignored issues
show
Bug introduced by
It seems like $this->encoding can also be of type boolean; however, parameter $encoding of Innmind\Immutable\Str::__construct() does only seem to accept null|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

583
        return new self($string.(string) $this, /** @scrutinizer ignore-type */ $this->encoding);
Loading history...
584
    }
585
586
    /**
587
     * Check if the 2 strings are equal
588
     *
589
     * @param self $string
590
     *
591
     * @return bool
592
     */
593 76
    public function equals(self $string): bool
594
    {
595 76
        return (string) $this === (string) $string;
596
    }
597
598
    /**
599
     * Trim the string
600
     *
601
     * @param string $mask
602
     *
603
     * @return self
604
     */
605 2
    public function trim(string $mask = null): self
606
    {
607 2
        return new self(
608 2
            $mask === null ? \trim((string) $this) : \trim((string) $this, $mask),
609 2
            $this->encoding
0 ignored issues
show
Bug introduced by
It seems like $this->encoding can also be of type boolean; however, parameter $encoding of Innmind\Immutable\Str::__construct() does only seem to accept null|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

609
            /** @scrutinizer ignore-type */ $this->encoding
Loading history...
610
        );
611
    }
612
613
    /**
614
     * Trim the right side of the string
615
     *
616
     * @param string $mask
617
     *
618
     * @return self
619
     */
620 2
    public function rightTrim(string $mask = null): self
621
    {
622 2
        return new self(
623 2
            $mask === null ? \rtrim((string) $this) : \rtrim((string) $this, $mask),
624 2
            $this->encoding
0 ignored issues
show
Bug introduced by
It seems like $this->encoding can also be of type boolean; however, parameter $encoding of Innmind\Immutable\Str::__construct() does only seem to accept null|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

624
            /** @scrutinizer ignore-type */ $this->encoding
Loading history...
625
        );
626
    }
627
628
    /**
629
     * Trim the left side of the string
630
     *
631
     * @param string $mask
632
     *
633
     * @return self
634
     */
635 2
    public function leftTrim(string $mask = null): self
636
    {
637 2
        return new self(
638 2
            $mask === null ? \ltrim((string) $this) : \ltrim((string) $this, $mask),
639 2
            $this->encoding
0 ignored issues
show
Bug introduced by
It seems like $this->encoding can also be of type boolean; however, parameter $encoding of Innmind\Immutable\Str::__construct() does only seem to accept null|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

639
            /** @scrutinizer ignore-type */ $this->encoding
Loading history...
640
        );
641
    }
642
643
    /**
644
     * Check if the given string is present in the current one
645
     *
646
     * @param string $value
647
     *
648
     * @return bool
649
     */
650 6
    public function contains(string $value): bool
651
    {
652
        try {
653 6
            $this->position($value);
654
655 6
            return true;
656 4
        } catch (SubstringException $e) {
657 4
            return false;
658
        }
659
    }
660
661
    /**
662
     * Quote regular expression characters
663
     *
664
     * @param string $delimiter
665
     *
666
     * @return self
667
     */
668 2
    public function pregQuote(string $delimiter = ''): self
669
    {
670 2
        return new self(\preg_quote((string) $this, $delimiter), $this->encoding);
0 ignored issues
show
Bug introduced by
It seems like $this->encoding can also be of type boolean; however, parameter $encoding of Innmind\Immutable\Str::__construct() does only seem to accept null|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

670
        return new self(\preg_quote((string) $this, $delimiter), /** @scrutinizer ignore-type */ $this->encoding);
Loading history...
671
    }
672
673
    /**
674
     * Pad the string
675
     *
676
     * @param int $length
677
     * @param string $character
678
     * @param int $direction
679
     *
680
     * @return self
681
     */
682 2
    private function pad(
683
        int $length,
684
        string $character = ' ',
685
        int $direction = self::PAD_RIGHT
686
    ): self {
687 2
        return new self(\str_pad(
688 2
            $this->value,
689 2
            $length,
690 2
            $character,
691 2
            $direction
692 2
        ), $this->encoding);
0 ignored issues
show
Bug introduced by
It seems like $this->encoding can also be of type boolean; however, parameter $encoding of Innmind\Immutable\Str::__construct() does only seem to accept null|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

692
        ), /** @scrutinizer ignore-type */ $this->encoding);
Loading history...
693
    }
694
}
695