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