mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-03 18:04:26 +02:00
Treat undefined vars and array access of a null or false variables equivalent across all supported PHP versions (#830)
* Added test to see what changed exactly * Treat undefined vars and array access of a null or false variables equivalent across all supported PHP versions * Removed 2 tests that produce inconsistent results between PHP7.x versions. * Fix regex matching for slightly different error message for php7.1
This commit is contained in:
@@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
- `$smarty->muteUndefinedOrNullWarnings()` now also mutes PHP7 notices for undefined array indexes [#736](https://github.com/smarty-php/smarty/issues/736)
|
- `$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
|
## [4.3.0] - 2022-11-22
|
||||||
|
|
||||||
|
@@ -66,12 +66,16 @@ class Smarty_Internal_ErrorHandler
|
|||||||
*/
|
*/
|
||||||
public function handleError($errno, $errstr, $errfile, $errline, $errcontext = [])
|
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
|
return; // suppresses this error
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->allowUndefinedArrayKeys && preg_match(
|
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
|
$errstr
|
||||||
)) {
|
)) {
|
||||||
return; // suppresses this error
|
return; // suppresses this error
|
||||||
|
@@ -88,14 +88,12 @@ class UndefinedTemplateVarTest extends PHPUnit_Smarty
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function testUndefinedSimpleVar() {
|
public function testUndefinedSimpleVar() {
|
||||||
$this->smarty->setErrorReporting(E_ALL & ~E_NOTICE);
|
|
||||||
$this->smarty->muteUndefinedOrNullWarnings();
|
$this->smarty->muteUndefinedOrNullWarnings();
|
||||||
$tpl = $this->smarty->createTemplate('string:a{if $undef}def{/if}b');
|
$tpl = $this->smarty->createTemplate('string:a{if $undef}def{/if}b');
|
||||||
$this->assertEquals("ab", $this->smarty->fetch($tpl));
|
$this->assertEquals("ab", $this->smarty->fetch($tpl));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testUndefinedArrayIndex() {
|
public function testUndefinedArrayIndex() {
|
||||||
$this->smarty->setErrorReporting(E_ALL & ~E_NOTICE);
|
|
||||||
$this->smarty->muteUndefinedOrNullWarnings();
|
$this->smarty->muteUndefinedOrNullWarnings();
|
||||||
$tpl = $this->smarty->createTemplate('string:a{if $ar.undef}def{/if}b');
|
$tpl = $this->smarty->createTemplate('string:a{if $ar.undef}def{/if}b');
|
||||||
$tpl->assign('ar', []);
|
$tpl->assign('ar', []);
|
||||||
@@ -103,7 +101,6 @@ class UndefinedTemplateVarTest extends PHPUnit_Smarty
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function testUndefinedArrayIndexDeep() {
|
public function testUndefinedArrayIndexDeep() {
|
||||||
$this->smarty->setErrorReporting(E_ALL & ~E_NOTICE);
|
|
||||||
$this->smarty->muteUndefinedOrNullWarnings();
|
$this->smarty->muteUndefinedOrNullWarnings();
|
||||||
$tpl = $this->smarty->createTemplate('string:a{if $ar.undef.nope.neither}def{/if}b');
|
$tpl = $this->smarty->createTemplate('string:a{if $ar.undef.nope.neither}def{/if}b');
|
||||||
$tpl->assign('ar', []);
|
$tpl->assign('ar', []);
|
||||||
@@ -133,5 +130,19 @@ class UndefinedTemplateVarTest extends PHPUnit_Smarty
|
|||||||
$this->assertTrue($exceptionThrown);
|
$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 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);
|
||||||
|
$this->assertEquals("ab", $this->smarty->fetch($tpl));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user