@@ -13,7 +13,7 @@ final class Str implements \Stringable
13
13
private string $ value ;
14
14
private Str \Encoding $ encoding ;
15
15
16
- private function __construct (string $ value , Str \Encoding $ encoding = null )
16
+ private function __construct (string $ value , ? Str \Encoding $ encoding = null )
17
17
{
18
18
$ this ->value = $ value ;
19
19
$ this ->encoding = $ encoding ?? Str \Encoding::utf8;
@@ -27,7 +27,7 @@ public function __toString(): string
27
27
/**
28
28
* @psalm-pure
29
29
*/
30
- public static function of (string $ value , Str \Encoding $ encoding = null ): self
30
+ public static function of (string $ value , ? Str \Encoding $ encoding = null ): self
31
31
{
32
32
return new self ($ value , $ encoding );
33
33
}
@@ -75,7 +75,7 @@ public function maybe(callable $filter): Maybe
75
75
*
76
76
* @return Sequence<self>
77
77
*/
78
- public function split (string $ delimiter = null ): Sequence
78
+ public function split (? string $ delimiter = null ): Sequence
79
79
{
80
80
if (\is_null ($ delimiter ) || $ delimiter === '' ) {
81
81
return $ this ->chunk ();
@@ -279,12 +279,20 @@ public function words(string|\Stringable $charlist = ''): Map
279
279
/**
280
280
* Split the string using a regular expression
281
281
*
282
+ * @throws InvalidRegex If the split didn't work
283
+ *
282
284
* @return Sequence<self>
283
285
*/
284
286
public function pregSplit (string |\Stringable $ regex , int $ limit = -1 ): Sequence
285
287
{
286
288
/** @psalm-suppress ArgumentTypeCoercion */
287
289
$ strings = \preg_split ((string ) $ regex , $ this ->value , $ limit );
290
+
291
+ if ($ strings === false ) {
292
+ /** @psalm-suppress ImpureFunctionCall */
293
+ throw new InvalidRegex ('' , \preg_last_error ());
294
+ }
295
+
288
296
/** @var Sequence<self> */
289
297
$ sequence = Sequence::of ();
290
298
@@ -348,7 +356,7 @@ public function pregReplace(
348
356
*
349
357
* @param 0|positive-int $length
350
358
*/
351
- public function substring (int $ start , int $ length = null ): self
359
+ public function substring (int $ start , ? int $ length = null ): self
352
360
{
353
361
if ($ this ->empty ()) {
354
362
return $ this ;
@@ -462,7 +470,7 @@ public function equals(self $string): bool
462
470
/**
463
471
* Trim the string
464
472
*/
465
- public function trim (string $ mask = null ): self
473
+ public function trim (? string $ mask = null ): self
466
474
{
467
475
return new self (
468
476
$ mask === null ? \trim ($ this ->value ) : \trim ($ this ->value , $ mask ),
@@ -473,7 +481,7 @@ public function trim(string $mask = null): self
473
481
/**
474
482
* Trim the right side of the string
475
483
*/
476
- public function rightTrim (string $ mask = null ): self
484
+ public function rightTrim (? string $ mask = null ): self
477
485
{
478
486
return new self (
479
487
$ mask === null ? \rtrim ($ this ->value ) : \rtrim ($ this ->value , $ mask ),
@@ -484,7 +492,7 @@ public function rightTrim(string $mask = null): self
484
492
/**
485
493
* Trim the left side of the string
486
494
*/
487
- public function leftTrim (string $ mask = null ): self
495
+ public function leftTrim (? string $ mask = null ): self
488
496
{
489
497
return new self (
490
498
$ mask === null ? \ltrim ($ this ->value ) : \ltrim ($ this ->value , $ mask ),
0 commit comments