default modifier uses empty instead of @ error suppression modifier when testing a variable

Fixes #336
This commit is contained in:
Simon Wisselink
2021-03-21 23:39:42 +01:00
parent 4698dd9fb0
commit ae7c263fcb
23 changed files with 80 additions and 8 deletions

1
.gitignore vendored
View File

@@ -11,3 +11,4 @@ utilies/*.php
phpunit* phpunit*
vendor/* vendor/*
composer.lock composer.lock
/composer.phar

View File

@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
### Changed ### Changed
- default modifier uses empty instead of @ error suppression modifier when testing a variable https://github.com/smarty-php/smarty/issues/336
- modifier escape now triggers a E_USER_NOTICE when an unsupported escape type is used https://github.com/smarty-php/smarty/pull/649 - modifier escape now triggers a E_USER_NOTICE when an unsupported escape type is used https://github.com/smarty-php/smarty/pull/649
## [3.1.39] - 2021-02-17 ## [3.1.39] - 2021-02-17

View File

@@ -18,7 +18,7 @@
* *
* @return string with compiled code * @return string with compiled code
*/ */
function smarty_modifiercompiler_default($params) function smarty_modifiercompiler_default($params, Smarty_Internal_TemplateCompilerBase $compiler)
{ {
$output = $params[ 0 ]; $output = $params[ 0 ];
if (!isset($params[ 1 ])) { if (!isset($params[ 1 ])) {
@@ -26,7 +26,13 @@ function smarty_modifiercompiler_default($params)
} }
array_shift($params); array_shift($params);
foreach ($params as $param) { foreach ($params as $param) {
$output = '(($tmp = @' . $output . ')===null||$tmp===\'\' ? ' . $param . ' : $tmp)';
if ($compiler->syntaxMatchesVariable($output)) {
$output = '(empty(' . $output . ') ? ' . $param . ' : ' . $output . ')';
} else {
$output = '(($tmp = ' . $output . ')===null||$tmp===\'\' ? ' . $param . ' : $tmp)';
}
} }
return $output; return $output;
} }

View File

@@ -674,7 +674,7 @@ abstract class Smarty_Internal_TemplateCompilerBase
* @param $string * @param $string
* @return bool * @return bool
*/ */
private function syntaxMatchesVariable($string) { public function syntaxMatchesVariable($string) {
static $regex_pattern = '/^\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*((->)[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*|\[.*]*\])*$/'; static $regex_pattern = '/^\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*((->)[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*|\[.*]*\])*$/';
return 1 === preg_match($regex_pattern, trim($string)); return 1 === preg_match($regex_pattern, trim($string));
} }

View File

@@ -64,10 +64,10 @@ class MuteExpectedErrorsTest extends PHPUnit_Smarty
$this->smarty->clearCompiledTemplate('default.tpl'); $this->smarty->clearCompiledTemplate('default.tpl');
$this->smarty->fetch('default.tpl'); $this->smarty->fetch('default.tpl');
$this->assertEquals(Smarty::$_IS_WINDOWS ? 2 : 2, count($this->_errors)); $this->assertEquals(0, count($this->_errors));
@filemtime('ckxladanwijicajscaslyxck'); @filemtime('ckxladanwijicajscaslyxck');
$this->assertEquals(Smarty::$_IS_WINDOWS ? 3 : 3, count($this->_errors)); $this->assertEquals(1, count($this->_errors));
restore_error_handler(); restore_error_handler();
} }
@@ -112,11 +112,11 @@ class MuteExpectedErrorsTest extends PHPUnit_Smarty
$this->smarty->clearCompiledTemplate('default.tpl'); $this->smarty->clearCompiledTemplate('default.tpl');
$this->smarty->fetch('default.tpl'); $this->smarty->fetch('default.tpl');
$this->assertEquals(Smarty::$_IS_WINDOWS ? 2 : 2, count($this->_errors)); $this->assertEquals(0, count($this->_errors));
@filemtime('ckxladanwijicajscaslyxck'); @filemtime('ckxladanwijicajscaslyxck');
$error = array(__FILE__ . ' line ' . (__LINE__ - 1)); $error = array(__FILE__ . ' line ' . (__LINE__ - 1));
$this->assertEquals(Smarty::$_IS_WINDOWS ? 3 : 3, count($this->_errors)); $this->assertEquals(1, count($this->_errors));
restore_error_handler(); restore_error_handler();
} }

View File

@@ -110,6 +110,9 @@ class FileResourceTest extends PHPUnit_Smarty
); );
} }
/**
* @doesNotPerformAssertions
*/
public function testGetCompiledTimestampPrepare() public function testGetCompiledTimestampPrepare()
{ {
$tpl = $this->smarty->createTemplate('helloworld.tpl'); $tpl = $this->smarty->createTemplate('helloworld.tpl');
@@ -142,6 +145,9 @@ class FileResourceTest extends PHPUnit_Smarty
$this->assertTrue($tpl->mustCompile()); $this->assertTrue($tpl->mustCompile());
} }
/**
* @doesNotPerformAssertions
*/
public function testMustCompileTouchedSourcePrepare() public function testMustCompileTouchedSourcePrepare()
{ {
// touch to prepare next test // touch to prepare next test

View File

@@ -161,7 +161,7 @@ class PhpResourceTest extends PHPUnit_Smarty
* *
* @runInSeparateProcess * @runInSeparateProcess
* @preserveGlobalState disabled * @preserveGlobalState disabled
* * @doesNotPerformAssertions
*/ */
public function testIsCachedTouchedSourcePrepare() public function testIsCachedTouchedSourcePrepare()
{ {
@@ -212,6 +212,7 @@ class PhpResourceTest extends PHPUnit_Smarty
/** /**
* test $smarty->is_cached * test $smarty->is_cached
* @doesNotPerformAssertions
*/ */
public function testSmartyIsCachedPrepare() public function testSmartyIsCachedPrepare()
{ {

View File

@@ -0,0 +1 @@
{* this is a comment *}

View File

@@ -0,0 +1,7 @@
=
{* comment *}
{* comment *}
b
{* comment *}
{* comment *}
=

View File

@@ -0,0 +1,7 @@
=
a
{* comment 1 *}
{* comment 2 *}
{* comment 3 *}
b
=

View File

@@ -0,0 +1,7 @@
=
a
{* comment 1 *}
{* comment 2 *}
{* comment 3 *}
b
=

View File

@@ -0,0 +1,7 @@
=
a
{* comment 1 *}
{* comment 2 *}
{* comment 3 *}
b
=

View File

@@ -0,0 +1,7 @@
=
a
{* comment 1 *}
{* comment 2 *}
{* comment 3 *}
b
=

View File

@@ -0,0 +1 @@
{* another $foo comment *}

View File

@@ -0,0 +1 @@
{* another comment *}some in between{* another comment *}

View File

@@ -0,0 +1,2 @@
{* multi line
comment *}

View File

@@ -0,0 +1 @@
{* /* foo * / *}

View File

@@ -0,0 +1,2 @@
A{* comment *}B
C

View File

@@ -0,0 +1,3 @@
D{* comment *}
{* comment *}E
F

View File

@@ -0,0 +1,2 @@
G{* multi
line *}H

View File

@@ -0,0 +1,3 @@
I{* multi
line *}
J

View File

@@ -20,6 +20,9 @@ class PluginFunctionHtmlImageTest extends PHPUnit_Smarty
$this->setUpSmarty(dirname(__FILE__)); $this->setUpSmarty(dirname(__FILE__));
} }
/**
* @doesNotPerformAssertions
*/
public function testFoo() public function testFoo()
{ {
// TODO: UnitTests for {html_image} // TODO: UnitTests for {html_image}

View File

@@ -291,6 +291,9 @@ class ScopeTest extends PHPUnit_Smarty
'no smarty', $i ++,),); 'no smarty', $i ++,),);
} }
/**
* @doesNotPerformAssertions
*/
public function testFunctionScope() public function testFunctionScope()
{ {
$this->smarty->assign('scope', 'none'); $this->smarty->assign('scope', 'none');