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 ( f1ee52...9a2238 )
by Baptiste
02:22
created

StringPrimitive::sprintf()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4286
cc 1
eloc 5
nc 1
nop 0
crap 1
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
    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 53
    public function __construct($value)
23
    {
24 53
        if (!is_string($value)) {
25 1
            throw new TypeException('Value must be a string');
26
        }
27
28 52
        $this->value = (string) $value;
29 52
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 2
    public function toPrimitive()
35
    {
36 2
        return $this->value;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 28
    public function __toString()
43
    {
44 28
        return $this->value;
45
    }
46
47
    /**
48
     * Split the string into a collection of ones
49
     *
50
     * @param string $delimiter
51
     *
52
     * @return TypedCollection
53
     */
54 3
    public function split($delimiter = null)
55
    {
56 3
        $delimiter = (string) $delimiter;
57 3
        $parts = empty($delimiter) ?
58 3
                str_split($this->value) : explode($delimiter, $this->value);
59 3
        $strings = [];
60
61 3
        foreach ($parts as $part) {
62 3
            $strings[] = new self($part);
63 3
        }
64
65 3
        return new TypedCollection(
66 3
            self::class,
67 1
            $strings
68 3
        );
69
    }
70
71
    /**
72
     * Returns a collection of the string splitted by the given chunk size
73
     *
74
     * @param int $size
75
     *
76
     * @return TypedCollection
77
     */
78 1
    public function chunk($size = 1)
79
    {
80 1
        return $this->split()->chunk((int) $size);
81
    }
82
83
    /**
84
     * Returns the position of the first occurence of the string
85
     *
86
     * @param string $needle
87
     * @param int $offset
88
     *
89
     * @throws SubstringException If the string is not found
90
     *
91
     * @return int
92
     */
93 3
    public function pos($needle, $offset = 0)
94
    {
95 3
        $position = mb_strpos($this->value, (string) $needle, (int) $offset);
96
97 3
        if ($position === false) {
98 1
            throw new SubstringException(sprintf(
99 1
                'Substring "%s" not found',
100
                $needle
101 1
            ));
102
        }
103
104 2
        return (int) $position;
105
    }
106
107
    /**
108
     * Replace all occurences of the search string with the replacement one
109
     *
110
     * @param string $search
111
     * @param string $replacement
112
     *
113
     * @return StringPrimitive
114
     */
115 1
    public function replace($search, $replacement)
116
    {
117 1
        return new self(str_replace(
118 1
            (string) $search,
119 1
            (string) $replacement,
120 1
            $this->value
121 1
        ));
122
    }
123
124
    /**
125
     * Returns the string following the given delimiter
126
     *
127
     * @param string $delimiter
128
     *
129
     * @throws SubstringException If the string is not found
130
     *
131
     * @return StringInterface
132
     */
133 2
    public function str($delimiter)
134
    {
135 2
        $sub = mb_strstr($this->value, (string) $delimiter);
136
137 2
        if ($sub === false) {
138 1
            throw new SubstringException(sprintf(
139 1
                'Substring "%s" not found',
140
                $delimiter
141 1
            ));
142
        }
143
144 1
        return new self($sub);
145
    }
146
147
    /**
148
     * Return the string in upper case
149
     *
150
     * @return StringPrimitive
151
     */
152 1
    public function toUpper()
153
    {
154 1
        return new self(mb_strtoupper($this->value));
155
    }
156
157
    /**
158
     * Return the string in lower case
159
     *
160
     * @return StringPrimitive
161
     */
162 1
    public function toLower()
163
    {
164 1
        return new self(mb_strtolower($this->value));
165
    }
166
167
    /**
168
     * Return the string length
169
     *
170
     * @return int
171
     */
172 2
    public function length()
173
    {
174 2
        return strlen($this->value);
175
    }
176
177
    /**
178
     * Reverse the string
179
     *
180
     * @return StringPrimitive
181
     */
182 1
    public function reverse()
183
    {
184 1
        return new self(strrev($this->value));
185
    }
186
187
    /**
188
     * Pad the string
189
     *
190
     * @param int $length
191
     * @param string $character
192
     * @param int $direction
193
     *
194
     * @return StringPrimitive
195
     */
196 1
    public function pad($length, $character = ' ', $direction = self::PAD_RIGHT)
197
    {
198 1
        return new self(str_pad(
199 1
            $this->value,
200 1
            (int) $length,
201 1
            (string) $character,
202
            $direction
203 1
        ));
204
    }
205
206
    /**
207
     * Pad to the right
208
     *
209
     * @param int $length
210
     * @param string $character
211
     *
212
     * @return StringPrimitive
213
     */
214 1
    public function rightPad($length, $character = ' ')
215
    {
216 1
        return $this->pad($length, $character, self::PAD_RIGHT);
217
    }
218
219
    /**
220
     * Pad to the left
221
     *
222
     * @param int $length
223
     * @param string $character
224
     *
225
     * @return StringPrimitive
226
     */
227 1
    public function leftPad($length, $character = ' ')
228
    {
229 1
        return $this->pad($length, $character, self::PAD_LEFT);
230
    }
231
232
    /**
233
     * Pad both sides
234
     *
235
     * @param int $length
236
     * @param string $character
237
     *
238
     * @return StringPrimitive
239
     */
240 1
    public function uniPad($length, $character = ' ')
241
    {
242 1
        return $this->pad($length, $character, self::PAD_BOTH);
243
    }
244
245
    /**
246
     * Find length of initial segment not matching mask
247
     *
248
     * @param string $mask
249
     * @param int $start
250
     * @param int $length
251
     *
252
     * @return int
253
     */
254 1
    public function cspn($mask, $start = 0, $length = null)
255
    {
256 1
        $start = (int) $start;
257
258 1
        if ($length === null) {
259 1
            $value = strcspn($this->value, (string) $mask, $start);
260 1
        } else {
261 1
            $value = strcspn(
262 1
                $this->value,
263 1
                (string) $mask,
264 1
                $start,
265
                (int) $length
266 1
            );
267
        }
268
269 1
        return (int) $value;
270
    }
271
272
    /**
273
     * Repeat the string n times
274
     *
275
     * @param int $repeat
276
     *
277
     * @return StringPrimitive
278
     */
279 1
    public function repeat($repeat)
280
    {
281 1
        return new self(str_repeat($this->value, (int) $repeat));
282
    }
283
284
    /**
285
     * Shuffle the string
286
     *
287
     * @return StringPrimitive
288
     */
289 1
    public function shuffle()
290
    {
291 1
        return new self(str_shuffle($this->value));
292
    }
293
294
    /**
295
     * Strip slashes
296
     *
297
     * @return StringPrimitive
298
     */
299 1
    public function stripSlashes()
300
    {
301 1
        return new self(stripslashes($this->value));
302
    }
303
304
    /**
305
     * Strip C-like slashes
306
     *
307
     * @return StringPrimitive
308
     */
309 1
    public function stripCSlashes()
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($charlist = '')
322
    {
323 1
        return (int) str_word_count(
324 1
            $this->value,
325 1
            0,
326
            (string) $charlist
327 1
        );
328
    }
329
330
    /**
331
     * Return the collection of words
332
     *
333
     * @param string $charlist
334
     *
335
     * @return TypedCollection
336
     */
337 1
    public function words($charlist = '')
338
    {
339 1
        $words = str_word_count($this->value, 2, (string) $charlist);
340
341 1
        foreach ($words as &$word) {
342 1
            $word = new self($word);
343 1
        }
344
345 1
        return new TypedCollection(
346 1
            self::class,
347
            $words
348 1
        );
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 TypedCollection
359
     */
360 1
    public function pregSplit($regex, $limit = -1, $flags = self::PREG_NO_FLAGS)
361
    {
362 1
        $strings = preg_split((string) $regex, $this->value, $limit, $flags);
363
364 1
        foreach ($strings as &$string) {
365 1
            $string = new self($string);
366 1
        }
367
368 1
        return new TypedCollection(
369 1
            self::class,
370
            $strings
371 1
        );
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($regex, $offset = 0)
385
    {
386 2
        $matches = [];
387 2
        $value = preg_match((string) $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 TypedCollection
406
     */
407 2
    public function getMatches($regex, $offset = 0, $flags = self::PREG_NO_FLAGS)
408
    {
409 2
        $matches = [];
410 2
        $value = preg_match(
411 2
            (string) $regex,
412 2
            $this->value,
413 2
            $matches,
414 2
            $flags,
415
            $offset
416 2
        );
417
418 2
        foreach ($matches as &$match) {
419 1
            $match = new self($match);
420 2
        }
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 StringPrimitive
439
     */
440 1
    public function pregReplace($regex, $replacement, $limit = -1)
441
    {
442 1
        $value = preg_replace(
443 1
            (string) $regex,
444 1
            (string) $replacement,
445 1
            $this->value,
446
            $limit
447 1
        );
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 StringPrimitive
463
     */
464 1
    public function substring($start, $length = null)
465
    {
466 1
        if ($length === null) {
467 1
            $sub = substr($this->value, (int) $start);
468 1
        } else {
469 1
            $sub = substr($this->value, (int) $start, (int) $length);
470
        }
471
472 1
        return new self($sub);
473
    }
474
475
    /**
476
     * Return a formatted string
477
     *
478
     * @return StringPrimitive
479
     */
480 1
    public function sprintf()
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