From debf5be7569b403e1cb1887c79a2e4435e0978ba Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Tue, 8 Aug 2023 14:56:24 +0200 Subject: [PATCH] Str: Also return padded array in case subject is null --- src/Str.php | 2 +- tests/StrTest.php | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Str.php b/src/Str.php index 3fec039..9cf1cae 100644 --- a/src/Str.php +++ b/src/Str.php @@ -61,7 +61,7 @@ public static function startsWith(?string $subject, string $start, bool $caseSen public static function symmetricSplit(?string $subject, string $delimiter, int $limit, $default = null) { if ($subject === null) { - return []; + return array_pad([], $limit, $default); } return array_pad(explode($delimiter, $subject, $limit), $limit, $default); diff --git a/tests/StrTest.php b/tests/StrTest.php index e50a2f7..fbe721d 100644 --- a/tests/StrTest.php +++ b/tests/StrTest.php @@ -75,6 +75,11 @@ public function testSymmetricSplitForSymmetricArrayDestructuring() $this->assertNull($password); } + public function testSymmetricSplitWithEmptySubjectStillReturnsAnArrayPaddedToTheDesiredSize() + { + $this->assertSame([null, null], Str::symmetricSplit(null, ',', 2)); + } + public function testTrimSplitTrimsWhitespacesAndSplitsByCommaByDefault() { $this->assertSame(['foo', 'bar', 'baz'], Str::trimSplit(' foo ,bar , baz '));