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 ( 580c66...1db9ea )
by Baptiste
03:02 queued 20s
created

StringPrimitive.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Immutable;
5
6
use Innmind\Immutable\Exception\TypeException;
7
use Innmind\Immutable\Exception\RegexException;
8
use Innmind\Immutable\Exception\SubstringException;
9
10
class StringPrimitive implements PrimitiveInterface, StringableInterface
11
{
12
    const PAD_RIGHT = STR_PAD_RIGHT;
13
    const PAD_LEFT = STR_PAD_LEFT;
14
    const PAD_BOTH = STR_PAD_BOTH;
15
    const PREG_NO_FLAGS = 0;
16
    const PREG_SPLIT_NO_EMPTY = PREG_SPLIT_NO_EMPTY;
17
    const PREG_SPLIT_DELIM_CAPTURE = PREG_SPLIT_DELIM_CAPTURE;
18
    const PREG_SPLIT_OFFSET_CAPTURE = PREG_SPLIT_OFFSET_CAPTURE;
19
    const PREG_OFFSET_CAPTURE = PREG_OFFSET_CAPTURE;
20
21
    private $value;
22
23 56
    public function __construct($value)
24
    {
25 56
        if (!is_string($value)) {
26 1
            throw new TypeException('Value must be a string');
27
        }
28
29 55
        $this->value = (string) $value;
30 55
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 1
    public function toPrimitive()
36
    {
37 1
        return $this->value;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 32
    public function __toString(): string
44
    {
45 32
        return $this->value;
46
    }
47
48
    /**
49
     * Split the string into a collection of ones
50
     *
51
     * @param string $delimiter
52
     *
53
     * @return TypedCollectionInterface
54
     */
55 2
    public function split(string $delimiter = null): TypedCollectionInterface
56
    {
57 2
        $parts = empty($delimiter) ?
58 2
                str_split($this->value) : explode($delimiter, $this->value);
59 2
        $strings = [];
60
61 2
        foreach ($parts as $part) {
62 2
            $strings[] = new self($part);
63
        }
64
65 2
        return new TypedCollection(
66 2
            self::class,
67
            $strings
68
        );
69
    }
70
71
    /**
72
     * Returns a collection of the string splitted by the given chunk size
73
     *
74
     * @param int $size
75
     *
76
     * @return TypedCollectionInterface
77
     */
78 1
    public function chunk(int $size = 1): TypedCollectionInterface
79
    {
80 1
        $pieces = str_split($this->value, $size);
81
82 1
        foreach ($pieces as &$piece) {
83 1
            $piece = new self($piece);
84
        }
85
86 1
        return new TypedCollection(self::class, $pieces);
87
    }
88
89
    /**
90
     * Returns the position of the first occurence of the string
91
     *
92
     * @param string $needle
93
     * @param int $offset
94
     *
95
     * @throws SubstringException If the string is not found
96
     *
97
     * @return int
98
     */
99 3
    public function pos(string $needle, int $offset = 0): int
100
    {
101 3
        $position = mb_strpos($this->value, $needle, $offset);
102
103 3
        if ($position === false) {
104 1
            throw new SubstringException(sprintf(
105 1
                'Substring "%s" not found',
106
                $needle
107
            ));
108
        }
109
110 2
        return (int) $position;
111
    }
112
113
    /**
114
     * Replace all occurences of the search string with the replacement one
115
     *
116
     * @param string $search
117
     * @param string $replacement
118
     *
119
     * @return self
120
     */
121 1
    public function replace(string $search, string $replacement): self
122
    {
123 1
        return new self(str_replace(
124 1
            (string) $search,
125 1
            (string) $replacement,
126 1
            $this->value
127
        ));
128
    }
129
130
    /**
131
     * Returns the string following the given delimiter
132
     *
133
     * @param string $delimiter
134
     *
135
     * @throws SubstringException If the string is not found
136
     *
137
     * @return self
138
     */
139 2
    public function str(string $delimiter): self
140
    {
141 2
        $sub = mb_strstr($this->value, $delimiter);
142
143 2
        if ($sub === false) {
144 1
            throw new SubstringException(sprintf(
145 1
                'Substring "%s" not found',
146
                $delimiter
147
            ));
148
        }
149
150 1
        return new self($sub);
151
    }
152
153
    /**
154
     * Return the string in upper case
155
     *
156
     * @return self
157
     */
158 1
    public function toUpper(): self
159
    {
160 1
        return new self(mb_strtoupper($this->value));
161
    }
162
163
    /**
164
     * Return the string in lower case
165
     *
166
     * @return self
167
     */
168 1
    public function toLower(): self
169
    {
170 1
        return new self(mb_strtolower($this->value));
171
    }
172
173
    /**
174
     * Return the string length
175
     *
176
     * @return int
177
     */
178 2
    public function length(): int
179
    {
180 2
        return strlen($this->value);
181
    }
182
183
    /**
184
     * Reverse the string
185
     *
186
     * @return self
187
     */
188 1
    public function reverse(): self
189
    {
190 1
        return new self(strrev($this->value));
191
    }
192
193
    /**
194
     * Pad the string
195
     *
196
     * @param int $length
197
     * @param string $character
198
     * @param int $direction
199
     *
200
     * @return self
201
     */
202 1
    public function pad(int $length, string $character = ' ', int $direction = self::PAD_RIGHT): self
203
    {
204 1
        return new self(str_pad(
205 1
            $this->value,
206
            $length,
207
            $character,
208
            $direction
209
        ));
210
    }
211
212
    /**
213
     * Pad to the right
214
     *
215
     * @param int $length
216
     * @param string $character
217
     *
218
     * @return self
219
     */
220 1
    public function rightPad(int $length, string $character = ' '): self
221
    {
222 1
        return $this->pad($length, $character, self::PAD_RIGHT);
223
    }
224
225
    /**
226
     * Pad to the left
227
     *
228
     * @param int $length
229
     * @param string $character
230
     *
231
     * @return self
232
     */
233 1
    public function leftPad(int $length, string $character = ' '): self
234
    {
235 1
        return $this->pad($length, $character, self::PAD_LEFT);
236
    }
237
238
    /**
239
     * Pad both sides
240
     *
241
     * @param int $length
242
     * @param string $character
243
     *
244
     * @return self
245
     */
246 1
    public function uniPad(int $length, string $character = ' '): self
247
    {
248 1
        return $this->pad($length, $character, self::PAD_BOTH);
249
    }
250
251
    /**
252
     * Find length of initial segment not matching mask
253
     *
254
     * @param string $mask
255
     * @param int $start
256
     * @param int $length
257
     *
258
     * @return int
259
     */
260 1
    public function cspn(string $mask, int $start = 0, int $length = null): int
261
    {
262 1 View Code Duplication
        if ($length === null) {
0 ignored issues
show
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...
263 1
            $value = strcspn($this->value, $mask, $start);
264
        } else {
265 1
            $value = strcspn(
266 1
                $this->value,
267
                $mask,
268
                $start,
269
                $length
270
            );
271
        }
272
273 1
        return (int) $value;
274
    }
275
276
    /**
277
     * Repeat the string n times
278
     *
279
     * @param int $repeat
280
     *
281
     * @return self
282
     */
283 1
    public function repeat(int $repeat): self
284
    {
285 1
        return new self(str_repeat($this->value, $repeat));
286
    }
287
288
    /**
289
     * Shuffle the string
290
     *
291
     * @return self
292
     */
293 1
    public function shuffle(): self
294
    {
295 1
        return new self(str_shuffle($this->value));
296
    }
297
298
    /**
299
     * Strip slashes
300
     *
301
     * @return self
302
     */
303 1
    public function stripSlashes(): self
304
    {
305 1
        return new self(stripslashes($this->value));
306
    }
307
308
    /**
309
     * Strip C-like slashes
310
     *
311
     * @return self
312
     */
313 1
    public function stripCSlashes(): self
314
    {
315 1
        return new self(stripcslashes($this->value));
316
    }
317
318
    /**
319
     * Return the word count
320
     *
321
     * @param string $charlist
322
     *
323
     * @return int
324
     */
325 1
    public function wordCount(string $charlist = ''): int
326
    {
327 1
        return (int) str_word_count(
328 1
            $this->value,
329 1
            0,
330
            $charlist
331
        );
332
    }
333
334
    /**
335
     * Return the collection of words
336
     *
337
     * @param string $charlist
338
     *
339
     * @return TypedCollectionInterface
340
     */
341 1
    public function words(string $charlist = ''): TypedCollectionInterface
342
    {
343 1
        $words = str_word_count($this->value, 2, $charlist);
344
345 1
        foreach ($words as &$word) {
346 1
            $word = new self($word);
347
        }
348
349 1
        return new TypedCollection(
350 1
            self::class,
351
            $words
352
        );
353
    }
354
355
    /**
356
     * Split the string using a regular expression
357
     *
358
     * @param string $regex
359
     * @param int $limit
360
     * @param int $flags
361
     *
362
     * @return TypedCollectionInterface
363
     */
364 2
    public function pregSplit(string $regex, int $limit = -1, int $flags = self::PREG_NO_FLAGS): TypedCollectionInterface
365
    {
366 2
        $strings = preg_split($regex, $this->value, $limit, $flags);
367
368 2
        foreach ($strings as &$string) {
369 2
            $string = new self($string);
370
        }
371
372 2
        return new TypedCollection(
373 2
            self::class,
374
            $strings
375
        );
376
    }
377
378
    /**
379
     * Check if the string match the given regular expression
380
     *
381
     * @param string $regex
382
     * @param int $offset
383
     *
384
     * @throws Exception If the regex failed
385
     *
386
     * @return bool
387
     */
388 2
    public function match(string $regex, int $offset = 0): bool
389
    {
390 2
        $matches = [];
391 2
        $value = preg_match($regex, $this->value, $matches, 0, $offset);
392
393 2
        if ($value === false) {
394 1
            throw new RegexException('', preg_last_error());
395
        }
396
397 1
        return (bool) $value;
398
    }
399
400
    /**
401
     * Return a collection of the elements matching the regex
402
     *
403
     * @param string $regex
404
     * @param int $offset
405
     * @param int $flags
406
     *
407
     * @throws Exception If the regex failed
408
     *
409
     * @return TypedCollectionInterface
410
     */
411 2
    public function getMatches(string $regex, int $offset = 0, int $flags = self::PREG_NO_FLAGS): TypedCollectionInterface
412
    {
413 2
        $matches = [];
414 2
        $value = preg_match(
415
            $regex,
416 2
            $this->value,
417
            $matches,
418
            $flags,
419
            $offset
420
        );
421
422 2
        foreach ($matches as &$match) {
423 1
            $match = new self($match);
424
        }
425
426 2
        if ($value === false) {
427 1
            throw new RegexException('', preg_last_error());
428
        }
429
430 1
        return new TypedCollection(self::class, $matches);
431
    }
432
433
    /**
434
     * Replace part of the string by using a regular expression
435
     *
436
     * @param string $regex
437
     * @param string $replacement
438
     * @param int $limit
439
     *
440
     * @throws Exception If the regex failed
441
     *
442
     * @return self
443
     */
444 1
    public function pregReplace(string $regex, string $replacement, int $limit = -1): self
445
    {
446 1
        $value = preg_replace(
447
            $regex,
448
            $replacement,
449 1
            $this->value,
450
            $limit
451
        );
452
453 1
        if ($value === null) {
454
            throw new RegexException('', preg_last_error());
455
        }
456
457 1
        return new self($value);
458
    }
459
460
    /**
461
     * Return part of the string
462
     *
463
     * @param int $start
464
     * @param int $length
465
     *
466
     * @return self
467
     */
468 1
    public function substring(int $start, int $length = null): self
469
    {
470 1 View Code Duplication
        if ($length === null) {
0 ignored issues
show
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...
471 1
            $sub = substr($this->value, $start);
472
        } else {
473 1
            $sub = substr($this->value, $start, $length);
474
        }
475
476 1
        return new self($sub);
477
    }
478
479
    /**
480
     * Return a formatted string
481
     *
482
     * @return self
483
     */
484 1
    public function sprintf(): self
485
    {
486 1
        $params = func_get_args();
487 1
        array_unshift($params, $this->value);
488 1
        $formatted = call_user_func_array('sprintf', $params);
489
490 1
        return new self($formatted);
491
    }
492
493
    /**
494
     * Return the string with the first letter as uppercase
495
     *
496
     * @return self
497
     */
498 2
    public function ucfirst(): self
499
    {
500 2
        return new self(ucfirst($this->value));
501
    }
502
503
    /**
504
     * Return the string with the first letter as lowercase
505
     *
506
     * @return self
507
     */
508 1
    public function lcfirst(): self
509
    {
510 1
        return new self(lcfirst($this->value));
511
    }
512
513
    /**
514
     * Return a CamelCase representation of the string
515
     *
516
     * @return self
517
     */
518 1
    public function camelize(): self
519
    {
520 1
        return new self(
521
            $this
522 1
                ->pregSplit('/_| /')
523 1
                ->map(function(self $part) {
524 1
                    return $part->ucfirst();
525 1
                })
526 1
                ->join('')
527
        );
528
    }
529
}
530