Skip to content

Commit

Permalink
Merge pull request #380 from schlessera/fix/trim-php-8.1
Browse files Browse the repository at this point in the history
Avoid using trim() on null
  • Loading branch information
bobthecow committed Dec 13, 2021
2 parents d6340c8 + 10a6c7c commit 6361644
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Mustache/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ class Mustache_Tokenizer
* @throws Mustache_Exception_InvalidArgumentException when $delimiters string is invalid
*
* @param string $text Mustache template source to tokenize
* @param string $delimiters Optionally, pass initial opening and closing delimiters (default: null)
* @param string $delimiters Optionally, pass initial opening and closing delimiters (default: empty string)
*
* @return array Set of Mustache tokens
*/
public function scan($text, $delimiters = null)
public function scan($text, $delimiters = '')
{
// Setting mbstring.func_overload makes things *really* slow.
// Let's do everyone a favor and scan this string as ASCII instead.
Expand All @@ -107,7 +107,7 @@ public function scan($text, $delimiters = null)

$this->reset();

if ($delimiters = trim($delimiters)) {
if (is_string($delimiters) && $delimiters = trim($delimiters)) {
$this->setDelimiters($delimiters);
}

Expand Down
48 changes: 48 additions & 0 deletions test/Mustache/Test/TokenizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,54 @@ public function getTokens()
),
),
),

// Delimiters are trimmed
array(
'<% name %>',
' <% %> ',
array(
array(
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_ESCAPED,
Mustache_Tokenizer::NAME => 'name',
Mustache_Tokenizer::OTAG => '<%',
Mustache_Tokenizer::CTAG => '%>',
Mustache_Tokenizer::LINE => 0,
Mustache_Tokenizer::INDEX => 10,
),
),
),

// An empty string makes delimiters fall back to default
array(
'{{ name }}',
'',
array(
array(
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_ESCAPED,
Mustache_Tokenizer::NAME => 'name',
Mustache_Tokenizer::OTAG => '{{',
Mustache_Tokenizer::CTAG => '}}',
Mustache_Tokenizer::LINE => 0,
Mustache_Tokenizer::INDEX => 10,
),
),
),

// A bad delimiter type makes delimiters fall back to default
array(
'{{ name }}',
42,
array(
array(
Mustache_Tokenizer::TYPE => Mustache_Tokenizer::T_ESCAPED,
Mustache_Tokenizer::NAME => 'name',
Mustache_Tokenizer::OTAG => '{{',
Mustache_Tokenizer::CTAG => '}}',
Mustache_Tokenizer::LINE => 0,
Mustache_Tokenizer::INDEX => 10,
),
),
),
);
}
}

0 comments on commit 6361644

Please sign in to comment.