Treat undefined vars and array access of a null or false variables

equivalent across all supported PHP versions
This commit is contained in:
Simon Wisselink
2022-11-24 00:35:06 +01:00
parent dee0c3ffd4
commit dad8005b64
3 changed files with 55 additions and 10 deletions

View File

@@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- `$smarty->muteUndefinedOrNullWarnings()` now also mutes PHP7 notices for undefined array indexes [#736](https://github.com/smarty-php/smarty/issues/736)
- `$smarty->muteUndefinedOrNullWarnings()` now treats undefined vars and array access of a null or false variables
equivalent across all supported PHP versions
## [4.3.0] - 2022-11-22
@@ -17,7 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- Include docs and demo in the releases [#799](https://github.com/smarty-php/smarty/issues/799)
- Using PHP functions as modifiers now triggers a deprecation notice because we will drop support for this in the next major release [#813](https://github.com/smarty-php/smarty/issues/813)
- Dropped remaining references to removed PHP-support in Smarty 4 from docs, lexer and security class. [#816](https://github.com/smarty-php/smarty/issues/816)
- Dropped remaining references to removed PHP-support in Smarty 4 from docs, lexer and security class. [#816](https://github.com/smarty-php/smarty/issues/816)
- Support umask when writing (template) files and set dir permissions to 777 [#548](https://github.com/smarty-php/smarty/issues/548) [#819](https://github.com/smarty-php/smarty/issues/819)
### Fixed

View File

@@ -66,12 +66,16 @@ class Smarty_Internal_ErrorHandler
*/
public function handleError($errno, $errstr, $errfile, $errline, $errcontext = [])
{
if ($this->allowUndefinedVars && $errstr == 'Attempt to read property "value" on null') {
if ($this->allowUndefinedVars && preg_match(
'/^(Attempt to read property "value" on null|Trying to get property \'value\' of non-object)/',
$errstr
)) {
return; // suppresses this error
}
if ($this->allowUndefinedArrayKeys && preg_match(
'/^(Undefined index|Undefined array key|Trying to access array offset on value of type null)/',
'/^(Undefined index|Undefined array key|Trying to access array offset on value of type (null|bool))/',
$errstr
)) {
return; // suppresses this error

View File

@@ -88,14 +88,12 @@ class UndefinedTemplateVarTest extends PHPUnit_Smarty
}
public function testUndefinedSimpleVar() {
$this->smarty->setErrorReporting(E_ALL & ~E_NOTICE);
$this->smarty->muteUndefinedOrNullWarnings();
$tpl = $this->smarty->createTemplate('string:a{if $undef}def{/if}b');
$this->assertEquals("ab", $this->smarty->fetch($tpl));
}
public function testUndefinedArrayIndex() {
$this->smarty->setErrorReporting(E_ALL & ~E_NOTICE);
$this->smarty->muteUndefinedOrNullWarnings();
$tpl = $this->smarty->createTemplate('string:a{if $ar.undef}def{/if}b');
$tpl->assign('ar', []);
@@ -103,7 +101,6 @@ class UndefinedTemplateVarTest extends PHPUnit_Smarty
}
public function testUndefinedArrayIndexDeep() {
$this->smarty->setErrorReporting(E_ALL & ~E_NOTICE);
$this->smarty->muteUndefinedOrNullWarnings();
$tpl = $this->smarty->createTemplate('string:a{if $ar.undef.nope.neither}def{/if}b');
$tpl->assign('ar', []);
@@ -133,15 +130,57 @@ class UndefinedTemplateVarTest extends PHPUnit_Smarty
$this->assertTrue($exceptionThrown);
}
public function testUsingNullAsAnArray() {
$this->smarty->setErrorReporting(E_ALL & ~E_NOTICE);
public function testUsingNullAsAnArrayTriggersError() {
$exceptionThrown = false;
try {
$this->smarty->setErrorReporting(E_ALL);
$tpl = $this->smarty->createTemplate('string:a{if $undef.k}def{/if}b');
$this->smarty->assign('undef', null);
$this->assertEquals("ab", $this->smarty->fetch($tpl));
} catch (Exception $e) {
$exceptionThrown = true;
$this->assertStringStartsWith('Trying to access array offset on value of type null', $e->getMessage());
$this->assertTrue(in_array(
get_class($e),
[
'PHPUnit\Framework\Error\Warning',
'PHPUnit\Framework\Error\Notice',
]
));
}
$this->assertTrue($exceptionThrown);
}
public function testUsingFalseAsAnArrayTriggersError() {
$exceptionThrown = false;
try {
$this->smarty->setErrorReporting(E_ALL);
$tpl = $this->smarty->createTemplate('string:a{if $nottrue.k}def{/if}b');
$this->smarty->assign('nottrue', false);
$this->assertEquals("ab", $this->smarty->fetch($tpl));
} catch (Exception $e) {
$exceptionThrown = true;
$this->assertStringStartsWith('Trying to access array offset on value of type bool', $e->getMessage());
$this->assertTrue(in_array(
get_class($e),
[
'PHPUnit\Framework\Error\Warning',
'PHPUnit\Framework\Error\Notice',
]
));
}
$this->assertTrue($exceptionThrown);
}
public function testUsingNullAsAnArrayIsMuted() {
$this->smarty->setErrorReporting(E_ALL);
$this->smarty->muteUndefinedOrNullWarnings();
$tpl = $this->smarty->createTemplate('string:a{if $undef.k}def{/if}b');
$this->assertEquals("ab", $this->smarty->fetch($tpl));
}
public function testUsingFalseAsAnArray() {
$this->smarty->setErrorReporting(E_ALL & ~E_NOTICE);
public function testUsingFalseAsAnArrayIsMuted() {
$this->smarty->setErrorReporting(E_ALL);
$this->smarty->muteUndefinedOrNullWarnings();
$tpl = $this->smarty->createTemplate('string:a{if $nottrue.k}def{/if}b');
$this->smarty->assign('nottrue', false);