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 ( 7a57b5...f1ee52 )
by Baptiste
02:34
created

StringPrimitive::cspn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

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