Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix split filter for php >=8.0 in case of empty separator #213

Merged
merged 5 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Liquid/StandardFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,14 @@ public static function split($input, $pattern)
return [];
}

if ($pattern === '') {
return mb_str_split($input);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Matches the behavior of the upstream project 👍

}

if (empty($pattern)) {
Copy link
Collaborator

@sanmai sanmai Mar 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a problem in this condition. Can you see it?

return [$input];
}

return explode($pattern, $input);
}

Expand Down
15 changes: 14 additions & 1 deletion tests/Liquid/StandardFiltersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -867,19 +867,32 @@ public function testSplit()
array(
'',
array(),
'-'
sanmai marked this conversation as resolved.
Show resolved Hide resolved
),
array(
null,
array(),
'-'
sanmai marked this conversation as resolved.
Show resolved Hide resolved
),
array(
'two-one-three',
array('two', 'one', 'three'),
'-'
sanmai marked this conversation as resolved.
Show resolved Hide resolved
),
array(
sanmai marked this conversation as resolved.
Show resolved Hide resolved
'phrase',
array('p', 'h', 'r', 'a', 's', 'e'),
''
),
array(
'phrase',
array('phrase'),
null
)
);

foreach ($data as $item) {
$this->assertEquals($item[1], StandardFilters::split($item[0], '-'));
$this->assertEquals($item[1], StandardFilters::split($item[0], $item[2]));
}
}

Expand Down
Loading