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 — develop ( cad621...d8b57f )
by Baptiste
04:31
created

Str::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Immutable;
5
6
use Innmind\Immutable\Exception\RegexException;
7
use Innmind\Immutable\Exception\SubstringException;
8
9
class Str implements PrimitiveInterface, StringableInterface
10
{
11
    const PAD_RIGHT = STR_PAD_RIGHT;
12
    const PAD_LEFT = STR_PAD_LEFT;
13
    const PAD_BOTH = STR_PAD_BOTH;
14
    const PREG_NO_FLAGS = 0;
15
    const PREG_SPLIT_NO_EMPTY = PREG_SPLIT_NO_EMPTY;
16
    const PREG_SPLIT_DELIM_CAPTURE = PREG_SPLIT_DELIM_CAPTURE;
17
    const PREG_SPLIT_OFFSET_CAPTURE = PREG_SPLIT_OFFSET_CAPTURE;
18
    const PREG_OFFSET_CAPTURE = PREG_OFFSET_CAPTURE;
19
20
    private $value;
21
22 82
    public function __construct(string $value)
23
    {
24 82
        $this->value = $value;
25 82
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 1
    public function toPrimitive()
31
    {
32 1
        return $this->value;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 53
    public function __toString(): string
39
    {
40 53
        return $this->value;
41
    }
42
43
    /**
44
     * Split the string into a collection of ones
45
     *
46
     * @param string $delimiter
47
     *
48
     * @return StreamInterface<self>
0 ignored issues
show
Documentation introduced by
The doc-type StreamInterface<self> could not be parsed: Expected "|" or "end of type", but got "<" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
49
     */
50 2 View Code Duplication
    public function split(string $delimiter = null): StreamInterface
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...
51
    {
52 2
        $parts = empty($delimiter) ?
53 2
                str_split($this->value) : explode($delimiter, $this->value);
54 2
        $stream = new Stream(self::class);
55
56 2
        foreach ($parts as $part) {
57 2
            $stream = $stream->add(new self($part));
1 ignored issue
show
Documentation introduced by
new self($part) is of type object<Innmind\Immutable\Str>, but the function expects a object<Innmind\Immutable\T>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
58
        }
59
60 2
        return $stream;
61
    }
62
63
    /**
64
     * Returns a collection of the string splitted by the given chunk size
65
     *
66
     * @param int $size
67
     *
68
     * @return StreamInterface<self>
0 ignored issues
show
Documentation introduced by
The doc-type StreamInterface<self> could not be parsed: Expected "|" or "end of type", but got "<" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
69
     */
70 1 View Code Duplication
    public function chunk(int $size = 1): StreamInterface
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...
71
    {
72 1
        $pieces = str_split($this->value, $size);
73 1
        $stream = new Stream(self::class);
74
75 1
        foreach ($pieces as $piece) {
76 1
            $stream = $stream->add(new self($piece));
1 ignored issue
show
Documentation introduced by
new self($piece) is of type object<Innmind\Immutable\Str>, but the function expects a object<Innmind\Immutable\T>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
77
        }
78
79 1
        return $stream;
80
    }
81
82
    /**
83
     * Returns the position of the first occurence of the string
84
     *
85
     * @param string $needle
86
     * @param int $offset
87
     *
88
     * @throws SubstringException If the string is not found
89
     *
90
     * @return int
91
     */
92 3 View Code Duplication
    public function position(string $needle, int $offset = 0): int
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...
93
    {
94 3
        $position = mb_strpos($this->value, $needle, $offset);
95
96 3
        if ($position === false) {
97 1
            throw new SubstringException(sprintf(
98 1
                'Substring "%s" not found',
99
                $needle
100
            ));
101
        }
102
103 2
        return (int) $position;
104
    }
105
106
    /**
107
     * Replace all occurences of the search string with the replacement one
108
     *
109
     * @param string $search
110
     * @param string $replacement
111
     *
112
     * @return self
113
     */
114 1
    public function replace(string $search, string $replacement): self
115
    {
116 1
        return new self(str_replace(
117 1
            (string) $search,
118 1
            (string) $replacement,
119 1
            $this->value
120
        ));
121
    }
122
123
    /**
124
     * Returns the string following the given delimiter
125
     *
126
     * @param string $delimiter
127
     *
128
     * @throws SubstringException If the string is not found
129
     *
130
     * @return self
131
     */
132 2 View Code Duplication
    public function str(string $delimiter): self
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...
133
    {
134 2
        $sub = mb_strstr($this->value, $delimiter);
135
136 2
        if ($sub === false) {
137 1
            throw new SubstringException(sprintf(
138 1
                'Substring "%s" not found',
139
                $delimiter
140
            ));
141
        }
142
143 1
        return new self($sub);
144
    }
145
146
    /**
147
     * Return the string in upper case
148
     *
149
     * @return self
150
     */
151 1
    public function toUpper(): self
152
    {
153 1
        return new self(mb_strtoupper($this->value));
154
    }
155
156
    /**
157
     * Return the string in lower case
158
     *
159
     * @return self
160
     */
161 1
    public function toLower(): self
162
    {
163 1
        return new self(mb_strtolower($this->value));
164
    }
165
166
    /**
167
     * Return the string length
168
     *
169
     * @return int
170
     */
171 2
    public function length(): int
172
    {
173 2
        return strlen($this->value);
174
    }
175
176
    /**
177
     * Reverse the string
178
     *
179
     * @return self
180
     */
181 1
    public function reverse(): self
182
    {
183 1
        return new self(strrev($this->value));
184
    }
185
186
    /**
187
     * Pad to the right
188
     *
189
     * @param int $length
190
     * @param string $character
191
     *
192
     * @return self
193
     */
194 1
    public function rightPad(int $length, string $character = ' '): self
195
    {
196 1
        return $this->pad($length, $character, self::PAD_RIGHT);
197
    }
198
199
    /**
200
     * Pad to the left
201
     *
202
     * @param int $length
203
     * @param string $character
204
     *
205
     * @return self
206
     */
207 1
    public function leftPad(int $length, string $character = ' '): self
208
    {
209 1
        return $this->pad($length, $character, self::PAD_LEFT);
210
    }
211
212
    /**
213
     * Pad both sides
214
     *
215
     * @param int $length
216
     * @param string $character
217
     *
218
     * @return self
219
     */
220 1
    public function uniPad(int $length, string $character = ' '): self
221
    {
222 1
        return $this->pad($length, $character, self::PAD_BOTH);
223
    }
224
225
    /**
226
     * Find length of initial segment not matching mask
227
     *
228
     * @param string $mask
229
     * @param int $start
230
     * @param int $length
231
     *
232
     * @return int
233
     */
234 1 View Code Duplication
    public function cspn(string $mask, int $start = 0, int $length = null): int
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...
235
    {
236 1
        if ($length === null) {
237 1
            $value = strcspn($this->value, $mask, $start);
238
        } else {
239 1
            $value = strcspn(
240 1
                $this->value,
241
                $mask,
242
                $start,
243
                $length
244
            );
245
        }
246
247 1
        return (int) $value;
248
    }
249
250
    /**
251
     * Repeat the string n times
252
     *
253
     * @param int $repeat
254
     *
255
     * @return self
256
     */
257 1
    public function repeat(int $repeat): self
258
    {
259 1
        return new self(str_repeat($this->value, $repeat));
260
    }
261
262
    /**
263
     * Shuffle the string
264
     *
265
     * @return self
266
     */
267 1
    public function shuffle(): self
268
    {
269 1
        return new self(str_shuffle($this->value));
270
    }
271
272
    /**
273
     * Strip slashes
274
     *
275
     * @return self
276
     */
277 1
    public function stripSlashes(): self
278
    {
279 1
        return new self(stripslashes($this->value));
280
    }
281
282
    /**
283
     * Strip C-like slashes
284
     *
285
     * @return self
286
     */
287 1
    public function stripCSlashes(): self
288
    {
289 1
        return new self(stripcslashes($this->value));
290
    }
291
292
    /**
293
     * Return the word count
294
     *
295
     * @param string $charlist
296
     *
297
     * @return int
298
     */
299 1
    public function wordCount(string $charlist = ''): int
300
    {
301 1
        return (int) str_word_count(
302 1
            $this->value,
303 1
            0,
304
            $charlist
305
        );
306
    }
307
308
    /**
309
     * Return the collection of words
310
     *
311
     * @param string $charlist
312
     *
313
     * @return MapInterface<int, self>
0 ignored issues
show
Documentation introduced by
The doc-type MapInterface<int, could not be parsed: Expected "|" or "end of type", but got "<" at position 12. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
314
     */
315 1
    public function words(string $charlist = ''): MapInterface
316
    {
317 1
        $words = str_word_count($this->value, 2, $charlist);
318 1
        $map = new Map('int', self::class);
319
320 1
        foreach ($words as $position => $word) {
321 1
            $map = $map->put($position, new self($word));
322
        }
323
324 1
        return $map;
325
    }
326
327
    /**
328
     * Split the string using a regular expression
329
     *
330
     * @param string $regex
331
     * @param int $limit
332
     * @param int $flags
0 ignored issues
show
Bug introduced by
There is no parameter named $flags. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
333
     *
334
     * @return StreamInterface<self>
0 ignored issues
show
Documentation introduced by
The doc-type StreamInterface<self> could not be parsed: Expected "|" or "end of type", but got "<" at position 15. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
335
     */
336 2 View Code Duplication
    public function pregSplit(string $regex, int $limit = -1): StreamInterface
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...
337
    {
338 2
        $strings = preg_split($regex, $this->value, $limit);
339 2
        $stream = new Stream(self::class);
340
341 2
        foreach ($strings as $string) {
342 2
            $stream = $stream->add(new self($string));
1 ignored issue
show
Documentation introduced by
new self($string) is of type object<Innmind\Immutable\Str>, but the function expects a object<Innmind\Immutable\T>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
343
        }
344
345 2
        return $stream;
346
    }
347
348
    /**
349
     * Check if the string match the given regular expression
350
     *
351
     * @param string $regex
352
     * @param int $offset
353
     *
354
     * @throws Exception If the regex failed
355
     *
356
     * @return bool
357
     */
358 2 View Code Duplication
    public function matches(string $regex, int $offset = 0): bool
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...
359
    {
360 2
        $matches = [];
361 2
        $value = preg_match($regex, $this->value, $matches, 0, $offset);
362
363 2
        if ($value === false) {
364 1
            throw new RegexException('', preg_last_error());
365
        }
366
367 1
        return (bool) $value;
368
    }
369
370
    /**
371
     * Return a collection of the elements matching the regex
372
     *
373
     * @param string $regex
374
     * @param int $offset
375
     * @param int $flags
376
     *
377
     * @throws Exception If the regex failed
378
     *
379
     * @return MapInterface<scalar, self>
0 ignored issues
show
Documentation introduced by
The doc-type MapInterface<scalar, could not be parsed: Expected "|" or "end of type", but got "<" at position 12. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
380
     */
381 2
    public function getMatches(
382
        string $regex,
383
        int $offset = 0,
384
        int $flags = self::PREG_NO_FLAGS
385
    ): MapInterface {
386 2
        $matches = [];
387 2
        $value = preg_match(
388
            $regex,
389 2
            $this->value,
390
            $matches,
391
            $flags,
392
            $offset
393
        );
394 2
        $map = new Map('scalar', self::class);
395
396 2
        foreach ($matches as $key => $match) {
397 1
            $map = $map->put($key, new self($match));
398
        }
399
400 2
        if ($value === false) {
401 1
            throw new RegexException('', preg_last_error());
402
        }
403
404 1
        return $map;
405
    }
406
407
    /**
408
     * Replace part of the string by using a regular expression
409
     *
410
     * @param string $regex
411
     * @param string $replacement
412
     * @param int $limit
413
     *
414
     * @throws Exception If the regex failed
415
     *
416
     * @return self
417
     */
418 1 View Code Duplication
    public function pregReplace(
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...
419
        string $regex,
420
        string $replacement,
421
        int $limit = -1
422
    ): self {
423 1
        $value = preg_replace(
424
            $regex,
425
            $replacement,
426 1
            $this->value,
427
            $limit
428
        );
429
430 1
        if ($value === null) {
431
            throw new RegexException('', preg_last_error());
432
        }
433
434 1
        return new self($value);
435
    }
436
437
    /**
438
     * Return part of the string
439
     *
440
     * @param int $start
441
     * @param int $length
442
     *
443
     * @return self
444
     */
445 1 View Code Duplication
    public function substring(int $start, int $length = null): self
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...
446
    {
447 1
        if ($length === null) {
448 1
            $sub = substr($this->value, $start);
449
        } else {
450 1
            $sub = substr($this->value, $start, $length);
451
        }
452
453 1
        return new self($sub);
454
    }
455
456
    /**
457
     * Return a formatted string
458
     *
459
     * @return self
460
     */
461 1
    public function sprintf(...$values): self
462
    {
463 1
        array_unshift($values, $this->value);
464 1
        $formatted = call_user_func_array('sprintf', $values);
465
466 1
        return new self($formatted);
467
    }
468
469
    /**
470
     * Return the string with the first letter as uppercase
471
     *
472
     * @return self
473
     */
474 2
    public function ucfirst(): self
475
    {
476 2
        return new self(ucfirst($this->value));
477
    }
478
479
    /**
480
     * Return the string with the first letter as lowercase
481
     *
482
     * @return self
483
     */
484 1
    public function lcfirst(): self
485
    {
486 1
        return new self(lcfirst($this->value));
487
    }
488
489
    /**
490
     * Return a CamelCase representation of the string
491
     *
492
     * @return self
493
     */
494 1
    public function camelize(): self
495
    {
496 1
        return new self(
497
            (string) $this
498 1
                ->pregSplit('/_| /')
499 1
                ->map(function(self $part) {
500 1
                    return $part->ucfirst();
501 1
                })
502 1
                ->join('')
503
        );
504
    }
505
506
    /**
507
     * Append a string at the end of the current one
508
     *
509
     * @param string $string
510
     *
511
     * @return self
512
     */
513 1
    public function append(string $string): self
514
    {
515 1
        return new self((string) $this.$string);
516
    }
517
518
    /**
519
     * Prepend a string at the beginning of the current one
520
     *
521
     * @param string $string
522
     *
523
     * @return self
524
     */
525 1
    public function prepend(string $string): self
526
    {
527 1
        return new self($string.(string) $this);
528
    }
529
530
    /**
531
     * Check if the 2 strings are equal
532
     *
533
     * @param self $string
534
     *
535
     * @return bool
536
     */
537 8
    public function equals(self $string): bool
538
    {
539 8
        return (string) $this === (string) $string;
540
    }
541
542
    /**
543
     * Trim the string
544
     *
545
     * @param string $mask
546
     *
547
     * @return self
548
     */
549 1
    public function trim(string $mask = null): self
550
    {
551 1
        return new self(
552 1
            $mask === null ? trim((string) $this) : trim((string) $this, $mask)
553
        );
554
    }
555
556
    /**
557
     * Trim the right side of the string
558
     *
559
     * @param string $mask
560
     *
561
     * @return self
562
     */
563 1
    public function rightTrim(string $mask = null): self
564
    {
565 1
        return new self(
566 1
            $mask === null ? rtrim((string) $this) : rtrim((string) $this, $mask)
567
        );
568
    }
569
570
    /**
571
     * Trim the left side of the string
572
     *
573
     * @param string $mask
574
     *
575
     * @return self
576
     */
577 1
    public function leftTrim(string $mask = null): self
578
    {
579 1
        return new self(
580 1
            $mask === null ? ltrim((string) $this) : ltrim((string) $this, $mask)
581
        );
582
    }
583
584
    /**
585
     * Pad the string
586
     *
587
     * @param int $length
588
     * @param string $character
589
     * @param int $direction
590
     *
591
     * @return self
592
     */
593 1
    private function pad(
594
        int $length,
595
        string $character = ' ',
596
        int $direction = self::PAD_RIGHT
597
    ): self {
598 1
        return new self(str_pad(
599 1
            $this->value,
600
            $length,
601
            $character,
602
            $direction
603
        ));
604
    }
605
}
606