Merge pull request #380 from schlessera/fix/trim-php-8.1

Avoid using trim() on null
This commit is contained in:
Justin Hileman
2021-12-12 22:13:34 -05:00
committed by GitHub
2 changed files with 51 additions and 3 deletions
+3 -3
View File
@@ -88,11 +88,11 @@ class Mustache_Tokenizer
* @throws Mustache_Exception_InvalidArgumentException when $delimiters string is invalid * @throws Mustache_Exception_InvalidArgumentException when $delimiters string is invalid
* *
* @param string $text Mustache template source to tokenize * @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 * @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. // Setting mbstring.func_overload makes things *really* slow.
// Let's do everyone a favor and scan this string as ASCII instead. // Let's do everyone a favor and scan this string as ASCII instead.
@@ -107,7 +107,7 @@ class Mustache_Tokenizer
$this->reset(); $this->reset();
if ($delimiters = trim($delimiters)) { if (is_string($delimiters) && $delimiters = trim($delimiters)) {
$this->setDelimiters($delimiters); $this->setDelimiters($delimiters);
} }
+48
View File
@@ -301,6 +301,54 @@ class Mustache_Test_TokenizerTest extends PHPUnit_Framework_TestCase
), ),
), ),
), ),
// 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,
),
),
),
); );
} }
} }