Skip to content

Commit

Permalink
Str: Also return padded array in case subject is null
Browse files Browse the repository at this point in the history
  • Loading branch information
nilmerg committed Aug 8, 2023
1 parent 8bd5759 commit 0729dcc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static function startsWith($subject, string $start, bool $caseSensitive =
public static function symmetricSplit($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);
Expand Down
5 changes: 5 additions & 0 deletions tests/StrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '));
Expand Down

0 comments on commit 0729dcc

Please sign in to comment.