Total Complexity | 63 |
Total Lines | 661 |
Duplicated Lines | 0 % |
Coverage | 97.14% |
Changes | 0 |
Complex classes like Str often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Str, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Str implements PrimitiveInterface, StringableInterface |
||
13 | { |
||
14 | const PAD_RIGHT = STR_PAD_RIGHT; |
||
15 | const PAD_LEFT = STR_PAD_LEFT; |
||
16 | const PAD_BOTH = STR_PAD_BOTH; |
||
17 | const PREG_NO_FLAGS = 0; |
||
18 | const PREG_SPLIT_NO_EMPTY = PREG_SPLIT_NO_EMPTY; |
||
19 | const PREG_SPLIT_DELIM_CAPTURE = PREG_SPLIT_DELIM_CAPTURE; |
||
20 | const PREG_SPLIT_OFFSET_CAPTURE = PREG_SPLIT_OFFSET_CAPTURE; |
||
21 | const PREG_OFFSET_CAPTURE = PREG_OFFSET_CAPTURE; |
||
22 | |||
23 | private $value; |
||
24 | private $encoding; |
||
25 | |||
26 | 516 | public function __construct(string $value, string $encoding = null) |
|
27 | { |
||
28 | 516 | $this->value = $value; |
|
29 | 516 | $this->encoding = $encoding ?? \mb_internal_encoding(); |
|
30 | 516 | } |
|
31 | |||
32 | 8 | public static function of(string $value, string $encoding = null): self |
|
33 | { |
||
34 | 8 | return new self($value, $encoding); |
|
35 | } |
||
36 | |||
37 | /** |
||
38 | * {@inheritdoc} |
||
39 | */ |
||
40 | 2 | public function toPrimitive() |
|
41 | { |
||
42 | 2 | return $this->value; |
|
43 | } |
||
44 | |||
45 | /** |
||
46 | * {@inheritdoc} |
||
47 | */ |
||
48 | 290 | public function __toString(): string |
|
49 | { |
||
50 | 290 | return $this->value; |
|
51 | } |
||
52 | |||
53 | 12 | public function encoding(): self |
|
54 | { |
||
55 | 12 | return new self($this->encoding); |
|
|
|||
56 | } |
||
57 | |||
58 | 30 | public function toEncoding(string $encoding): self |
|
59 | { |
||
60 | 30 | return new self($this->value, $encoding); |
|
61 | } |
||
62 | |||
63 | /** |
||
64 | * Split the string into a collection of ones |
||
65 | * |
||
66 | * @param string $delimiter |
||
67 | * |
||
68 | * @return StreamInterface<self> |
||
69 | */ |
||
70 | 14 | public function split(string $delimiter = null): StreamInterface |
|
71 | { |
||
72 | 14 | if (\is_null($delimiter) || $delimiter === '') { |
|
73 | 6 | return $this->chunk(); |
|
74 | } |
||
75 | |||
76 | 10 | $parts = \explode($delimiter, $this->value); |
|
77 | 10 | $stream = new Stream(self::class); |
|
78 | |||
79 | 10 | foreach ($parts as $part) { |
|
80 | 10 | $stream = $stream->add(new self($part, $this->encoding)); |
|
81 | } |
||
82 | |||
83 | 10 | return $stream; |
|
84 | } |
||
85 | |||
86 | /** |
||
87 | * Returns a collection of the string splitted by the given chunk size |
||
88 | * |
||
89 | * @param int $size |
||
90 | * |
||
91 | * @return StreamInterface<self> |
||
92 | */ |
||
93 | 14 | public function chunk(int $size = 1): StreamInterface |
|
94 | { |
||
95 | 14 | $stream = new Stream(self::class); |
|
96 | 14 | $string = $this; |
|
97 | |||
98 | 14 | while ($string->length() > 0) { |
|
99 | 14 | $stream = $stream->add($string->substring(0, $size)); |
|
100 | 14 | $string = $string->substring($size); |
|
101 | } |
||
102 | |||
103 | 14 | return $stream; |
|
104 | } |
||
105 | |||
106 | /** |
||
107 | * Returns the position of the first occurence of the string |
||
108 | * |
||
109 | * @param string $needle |
||
110 | * @param int $offset |
||
111 | * |
||
112 | * @throws SubstringException If the string is not found |
||
113 | * |
||
114 | * @return int |
||
115 | */ |
||
116 | 12 | public function position(string $needle, int $offset = 0): int |
|
117 | { |
||
118 | 12 | $position = \mb_strpos($this->value, $needle, $offset, $this->encoding); |
|
119 | |||
120 | 12 | if ($position === false) { |
|
121 | 6 | throw new SubstringException(\sprintf( |
|
122 | 6 | 'Substring "%s" not found', |
|
123 | 6 | $needle |
|
124 | )); |
||
125 | } |
||
126 | |||
127 | 10 | return (int) $position; |
|
128 | } |
||
129 | |||
130 | /** |
||
131 | * Replace all occurences of the search string with the replacement one |
||
132 | * |
||
133 | * @param string $search |
||
134 | * @param string $replacement |
||
135 | * |
||
136 | * @return self |
||
137 | */ |
||
138 | 4 | public function replace(string $search, string $replacement): self |
|
139 | { |
||
140 | 4 | if (!$this->contains($search)) { |
|
141 | 2 | return $this; |
|
142 | } |
||
143 | |||
144 | return $this |
||
145 | 4 | ->split($search) |
|
146 | 4 | ->join($replacement); |
|
147 | } |
||
148 | |||
149 | /** |
||
150 | * Returns the string following the given delimiter |
||
151 | * |
||
152 | * @param string $delimiter |
||
153 | * |
||
154 | * @throws SubstringException If the string is not found |
||
155 | * |
||
156 | * @return self |
||
157 | */ |
||
158 | 6 | public function str(string $delimiter): self |
|
159 | { |
||
160 | 6 | $sub = \mb_strstr($this->value, $delimiter, false, $this->encoding); |
|
161 | |||
162 | 6 | if ($sub === false) { |
|
163 | 2 | throw new SubstringException(\sprintf( |
|
164 | 2 | 'Substring "%s" not found', |
|
165 | 2 | $delimiter |
|
166 | )); |
||
167 | } |
||
168 | |||
169 | 4 | return new self($sub, $this->encoding); |
|
170 | } |
||
171 | |||
172 | /** |
||
173 | * Return the string in upper case |
||
174 | * |
||
175 | * @return self |
||
176 | */ |
||
177 | 6 | public function toUpper(): self |
|
178 | { |
||
179 | 6 | return new self(\mb_strtoupper($this->value), $this->encoding); |
|
180 | } |
||
181 | |||
182 | /** |
||
183 | * Return the string in lower case |
||
184 | * |
||
185 | * @return self |
||
186 | */ |
||
187 | 4 | public function toLower(): self |
|
188 | { |
||
189 | 4 | return new self(\mb_strtolower($this->value), $this->encoding); |
|
190 | } |
||
191 | |||
192 | /** |
||
193 | * Return the string length |
||
194 | * |
||
195 | * @return int |
||
196 | */ |
||
197 | 30 | public function length(): int |
|
198 | { |
||
199 | 30 | return \mb_strlen($this->value, $this->encoding); |
|
200 | } |
||
201 | |||
202 | 2 | public function empty(): bool |
|
203 | { |
||
204 | 2 | return $this->value === ''; |
|
205 | } |
||
206 | |||
207 | /** |
||
208 | * Reverse the string |
||
209 | * |
||
210 | * @return self |
||
211 | */ |
||
212 | 2 | public function reverse(): self |
|
213 | { |
||
214 | return $this |
||
215 | 2 | ->chunk() |
|
216 | 2 | ->reverse() |
|
217 | 2 | ->join(''); |
|
218 | } |
||
219 | |||
220 | /** |
||
221 | * Pad to the right |
||
222 | * |
||
223 | * @param int $length |
||
224 | * @param string $character |
||
225 | * |
||
226 | * @return self |
||
227 | */ |
||
228 | 2 | public function rightPad(int $length, string $character = ' '): self |
|
229 | { |
||
230 | 2 | return $this->pad($length, $character, self::PAD_RIGHT); |
|
231 | } |
||
232 | |||
233 | /** |
||
234 | * Pad to the left |
||
235 | * |
||
236 | * @param int $length |
||
237 | * @param string $character |
||
238 | * |
||
239 | * @return self |
||
240 | */ |
||
241 | 2 | public function leftPad(int $length, string $character = ' '): self |
|
242 | { |
||
243 | 2 | return $this->pad($length, $character, self::PAD_LEFT); |
|
244 | } |
||
245 | |||
246 | /** |
||
247 | * Pad both sides |
||
248 | * |
||
249 | * @param int $length |
||
250 | * @param string $character |
||
251 | * |
||
252 | * @return self |
||
253 | */ |
||
254 | 2 | public function uniPad(int $length, string $character = ' '): self |
|
255 | { |
||
256 | 2 | return $this->pad($length, $character, self::PAD_BOTH); |
|
257 | } |
||
258 | |||
259 | /** |
||
260 | * Find length of initial segment not matching mask |
||
261 | * |
||
262 | * @param string $mask |
||
263 | * @param int $start |
||
264 | * @param int $length |
||
265 | * |
||
266 | * @return int |
||
267 | */ |
||
268 | 2 | public function cspn(string $mask, int $start = 0, int $length = null): int |
|
269 | { |
||
270 | 2 | if ($length === null) { |
|
271 | 2 | $value = \strcspn($this->value, $mask, $start); |
|
272 | } else { |
||
273 | 2 | $value = \strcspn( |
|
274 | 2 | $this->value, |
|
275 | 2 | $mask, |
|
276 | 2 | $start, |
|
277 | 2 | $length |
|
278 | ); |
||
279 | } |
||
280 | |||
281 | 2 | return (int) $value; |
|
282 | } |
||
283 | |||
284 | /** |
||
285 | * Repeat the string n times |
||
286 | * |
||
287 | * @param int $repeat |
||
288 | * |
||
289 | * @return self |
||
290 | */ |
||
291 | 2 | public function repeat(int $repeat): self |
|
292 | { |
||
293 | 2 | return new self(\str_repeat($this->value, $repeat), $this->encoding); |
|
294 | } |
||
295 | |||
296 | /** |
||
297 | * Shuffle the string |
||
298 | * |
||
299 | * @return self |
||
300 | */ |
||
301 | 4 | public function shuffle(): self |
|
302 | { |
||
303 | 4 | $parts = $this->chunk()->toPrimitive(); |
|
304 | 4 | \shuffle($parts); |
|
305 | |||
306 | 4 | return new self(\implode('', $parts), $this->encoding); |
|
307 | } |
||
308 | |||
309 | /** |
||
310 | * Strip slashes |
||
311 | * |
||
312 | * @return self |
||
313 | */ |
||
314 | 2 | public function stripSlashes(): self |
|
315 | { |
||
316 | 2 | return new self(\stripslashes($this->value), $this->encoding); |
|
317 | } |
||
318 | |||
319 | /** |
||
320 | * Strip C-like slashes |
||
321 | * |
||
322 | * @return self |
||
323 | */ |
||
324 | 2 | public function stripCSlashes(): self |
|
325 | { |
||
326 | 2 | return new self(\stripcslashes($this->value), $this->encoding); |
|
327 | } |
||
328 | |||
329 | /** |
||
330 | * Return the word count |
||
331 | * |
||
332 | * @param string $charlist |
||
333 | * |
||
334 | * @return int |
||
335 | */ |
||
336 | 2 | public function wordCount(string $charlist = ''): int |
|
337 | { |
||
338 | 2 | return (int) \str_word_count( |
|
339 | 2 | $this->value, |
|
340 | 2 | 0, |
|
341 | 2 | $charlist |
|
342 | ); |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * Return the collection of words |
||
347 | * |
||
348 | * @param string $charlist |
||
349 | * |
||
350 | * @return MapInterface<int, self> |
||
351 | */ |
||
352 | 2 | public function words(string $charlist = ''): MapInterface |
|
353 | { |
||
354 | 2 | $words = \str_word_count($this->value, 2, $charlist); |
|
355 | 2 | $map = new Map('int', self::class); |
|
356 | |||
357 | 2 | foreach ($words as $position => $word) { |
|
358 | 2 | $map = $map->put($position, new self($word, $this->encoding)); |
|
359 | } |
||
360 | |||
361 | 2 | return $map; |
|
362 | } |
||
363 | |||
364 | /** |
||
365 | * Split the string using a regular expression |
||
366 | * |
||
367 | * @param string $regex |
||
368 | * @param int $limit |
||
369 | * |
||
370 | * @return StreamInterface<self> |
||
371 | */ |
||
372 | 4 | public function pregSplit(string $regex, int $limit = -1): StreamInterface |
|
373 | { |
||
374 | 4 | $strings = \preg_split($regex, $this->value, $limit); |
|
375 | 4 | $stream = new Stream(self::class); |
|
376 | |||
377 | 4 | foreach ($strings as $string) { |
|
378 | 4 | $stream = $stream->add(new self($string, $this->encoding)); |
|
379 | } |
||
380 | |||
381 | 4 | return $stream; |
|
382 | } |
||
383 | |||
384 | /** |
||
385 | * Check if the string match the given regular expression |
||
386 | * |
||
387 | * @param string $regex |
||
388 | * |
||
389 | * @throws Exception If the regex failed |
||
390 | * |
||
391 | * @return bool |
||
392 | */ |
||
393 | 4 | public function matches(string $regex): bool |
|
394 | { |
||
395 | 4 | if (\func_num_args() !== 1) { |
|
396 | throw new LogicException('Offset is no longer supported'); |
||
397 | } |
||
398 | |||
399 | 4 | return RegExp::of($regex)->matches($this); |
|
400 | } |
||
401 | |||
402 | /** |
||
403 | * Return a collection of the elements matching the regex |
||
404 | * |
||
405 | * @deprecated replaced by self::capture, to be removed in 3.0 |
||
406 | * |
||
407 | * @param string $regex |
||
408 | * @param int $offset |
||
409 | * @param int $flags |
||
410 | * |
||
411 | * @throws Exception If the regex failed |
||
412 | * |
||
413 | * @return MapInterface<scalar, self> |
||
414 | */ |
||
415 | public function getMatches( |
||
416 | string $regex, |
||
417 | int $offset = 0, |
||
418 | int $flags = self::PREG_NO_FLAGS |
||
419 | ): MapInterface { |
||
420 | return $this->capture($regex, $offset, $flags); |
||
1 ignored issue
–
show
|
|||
421 | } |
||
422 | |||
423 | /** |
||
424 | * Return a collection of the elements matching the regex |
||
425 | * |
||
426 | * @param string $regex |
||
427 | * |
||
428 | * @throws Exception If the regex failed |
||
429 | * |
||
430 | * @return MapInterface<scalar, self> |
||
431 | */ |
||
432 | 6 | public function capture(string $regex): MapInterface |
|
433 | { |
||
434 | 6 | if (\func_num_args() !== 1) { |
|
435 | throw new LogicException('Offset and flags are no longer supported'); |
||
436 | } |
||
437 | |||
438 | 6 | return RegExp::of($regex)->capture($this); |
|
439 | } |
||
440 | |||
441 | /** |
||
442 | * Replace part of the string by using a regular expression |
||
443 | * |
||
444 | * @param string $regex |
||
445 | * @param string $replacement |
||
446 | * @param int $limit |
||
447 | * |
||
448 | * @throws Exception If the regex failed |
||
449 | * |
||
450 | * @return self |
||
451 | */ |
||
452 | 2 | public function pregReplace( |
|
453 | string $regex, |
||
454 | string $replacement, |
||
455 | int $limit = -1 |
||
456 | ): self { |
||
457 | 2 | $value = \preg_replace( |
|
458 | 2 | $regex, |
|
459 | 2 | $replacement, |
|
460 | 2 | $this->value, |
|
461 | 2 | $limit |
|
462 | ); |
||
463 | |||
464 | 2 | if ($value === null) { |
|
465 | throw new RegexException('', \preg_last_error()); |
||
466 | } |
||
467 | |||
468 | 2 | return new self($value, $this->encoding); |
|
469 | } |
||
470 | |||
471 | /** |
||
472 | * Return part of the string |
||
473 | * |
||
474 | * @param int $start |
||
475 | * @param int $length |
||
476 | * |
||
477 | * @return self |
||
478 | */ |
||
479 | 26 | public function substring(int $start, int $length = null): self |
|
480 | { |
||
481 | 26 | if ($this->length() === 0) { |
|
482 | 2 | return $this; |
|
483 | } |
||
484 | |||
485 | 26 | $sub = \mb_substr($this->value, $start, $length, $this->encoding); |
|
486 | |||
487 | 26 | return new self($sub, $this->encoding); |
|
488 | } |
||
489 | |||
490 | /** |
||
491 | * Return a formatted string |
||
492 | * |
||
493 | * @return self |
||
494 | */ |
||
495 | 2 | public function sprintf(...$values): self |
|
496 | { |
||
497 | 2 | return new self(\sprintf($this->value, ...$values), $this->encoding); |
|
498 | } |
||
499 | |||
500 | /** |
||
501 | * Return the string with the first letter as uppercase |
||
502 | * |
||
503 | * @return self |
||
504 | */ |
||
505 | 4 | public function ucfirst(): self |
|
506 | { |
||
507 | return $this |
||
508 | 4 | ->substring(0, 1) |
|
509 | 4 | ->toUpper() |
|
510 | 4 | ->append((string) $this->substring(1)); |
|
511 | } |
||
512 | |||
513 | /** |
||
514 | * Return the string with the first letter as lowercase |
||
515 | * |
||
516 | * @return self |
||
517 | */ |
||
518 | 2 | public function lcfirst(): self |
|
519 | { |
||
520 | return $this |
||
521 | 2 | ->substring(0, 1) |
|
522 | 2 | ->toLower() |
|
523 | 2 | ->append((string) $this->substring(1)); |
|
524 | } |
||
525 | |||
526 | /** |
||
527 | * Return a CamelCase representation of the string |
||
528 | * |
||
529 | * @return self |
||
530 | */ |
||
531 | 2 | public function camelize(): self |
|
540 | } |
||
541 | |||
542 | /** |
||
543 | * Append a string at the end of the current one |
||
544 | * |
||
545 | * @param string $string |
||
546 | * |
||
547 | * @return self |
||
548 | */ |
||
549 | 8 | public function append(string $string): self |
|
550 | { |
||
551 | 8 | return new self((string) $this.$string, $this->encoding); |
|
552 | } |
||
553 | |||
554 | /** |
||
555 | * Prepend a string at the beginning of the current one |
||
556 | * |
||
557 | * @param string $string |
||
558 | * |
||
559 | * @return self |
||
560 | */ |
||
561 | 2 | public function prepend(string $string): self |
|
564 | } |
||
565 | |||
566 | /** |
||
567 | * Check if the 2 strings are equal |
||
568 | * |
||
569 | * @param self $string |
||
570 | * |
||
571 | * @return bool |
||
572 | */ |
||
573 | 76 | public function equals(self $string): bool |
|
574 | { |
||
575 | 76 | return (string) $this === (string) $string; |
|
576 | } |
||
577 | |||
578 | /** |
||
579 | * Trim the string |
||
580 | * |
||
581 | * @param string $mask |
||
582 | * |
||
583 | * @return self |
||
584 | */ |
||
585 | 2 | public function trim(string $mask = null): self |
|
586 | { |
||
587 | 2 | return new self( |
|
588 | 2 | $mask === null ? \trim((string) $this) : \trim((string) $this, $mask), |
|
589 | 2 | $this->encoding |
|
590 | ); |
||
591 | } |
||
592 | |||
593 | /** |
||
594 | * Trim the right side of the string |
||
595 | * |
||
596 | * @param string $mask |
||
597 | * |
||
598 | * @return self |
||
599 | */ |
||
600 | 2 | public function rightTrim(string $mask = null): self |
|
601 | { |
||
602 | 2 | return new self( |
|
603 | 2 | $mask === null ? \rtrim((string) $this) : \rtrim((string) $this, $mask), |
|
604 | 2 | $this->encoding |
|
605 | ); |
||
606 | } |
||
607 | |||
608 | /** |
||
609 | * Trim the left side of the string |
||
610 | * |
||
611 | * @param string $mask |
||
612 | * |
||
613 | * @return self |
||
614 | */ |
||
615 | 2 | public function leftTrim(string $mask = null): self |
|
616 | { |
||
617 | 2 | return new self( |
|
618 | 2 | $mask === null ? \ltrim((string) $this) : \ltrim((string) $this, $mask), |
|
619 | 2 | $this->encoding |
|
620 | ); |
||
621 | } |
||
622 | |||
623 | /** |
||
624 | * Check if the given string is present in the current one |
||
625 | * |
||
626 | * @param string $value |
||
627 | * |
||
628 | * @return bool |
||
629 | */ |
||
630 | 6 | public function contains(string $value): bool |
|
631 | { |
||
632 | try { |
||
633 | 6 | $this->position($value); |
|
634 | |||
635 | 6 | return true; |
|
636 | 4 | } catch (SubstringException $e) { |
|
637 | 4 | return false; |
|
638 | } |
||
639 | } |
||
640 | |||
641 | /** |
||
642 | * Quote regular expression characters |
||
643 | * |
||
644 | * @param string $delimiter |
||
645 | * |
||
646 | * @return self |
||
647 | */ |
||
648 | 2 | public function pregQuote(string $delimiter = ''): self |
|
651 | } |
||
652 | |||
653 | /** |
||
654 | * Pad the string |
||
655 | * |
||
656 | * @param int $length |
||
657 | * @param string $character |
||
658 | * @param int $direction |
||
659 | * |
||
660 | * @return self |
||
661 | */ |
||
662 | 2 | private function pad( |
|
663 | int $length, |
||
664 | string $character = ' ', |
||
665 | int $direction = self::PAD_RIGHT |
||
666 | ): self { |
||
667 | 2 | return new self(\str_pad( |
|
668 | 2 | $this->value, |
|
669 | 2 | $length, |
|
670 | 2 | $character, |
|
671 | 2 | $direction |
|
673 | } |
||
674 | } |
||
675 |