smarty icon indicating copy to clipboard operation
smarty copied to clipboard

Isset and unassigned variable issue in v5.4.3

Open kaliberx opened this issue 10 months ago • 0 comments

Due to bug https://github.com/smarty-php/smarty/issues/1063 in Smarty v5.4.2 I had a simple custom patch to fix the compatibility problem. Since v5.4.3 the bug is partially fixed and the bad news is that my workaround has stopped working because isset is handled in yacc parser and IssetModifierCompiler is no logner used.

The problem is that a PHP Warning is generated when in isset() an array with some field is used, for instance isset($var.item). Please check following code:

require 'vendor/autoload.php';

error_reporting(E_ALL);

use Smarty\Smarty;
$smarty = new Smarty();
$smarty->error_unassigned = true;

// works fine:
// compiled: if ((true && ($_smarty_tpl->hasVariable('var') && null !== ($_smarty_tpl->getValue('var') ?? null)))) ...
$smarty->display('string:{if isset($var)}yes{else}no{/if}');

// works incorrectly, generates error
// compiled: if ((true && (true && null !== ($_smarty_tpl->getValue('var')['item'] ?? null)))) ...
$smarty->display('string:{if isset($var.item)}yes{else}no{/if}');

Until 5.4.2 I used following workaound with custom IssetModifierCompiler:

diff -urN smarty.orig/src/Data.php smarty/src/Data.php
--- smarty.orig/src/Data.php	2024-10-21 13:57:12.613352696 +0200
+++ smarty/src/Data.php	2024-10-21 13:57:01.132720603 +0200
@@ -301,8 +301,8 @@
 	 *
 	 * @return mixed|null
 	 */
-	public function getValue($varName, $searchParents = true) {
-		$variable = $this->getVariable($varName, $searchParents);
+	public function getValue($varName, $searchParents = true, $errorEnable = true) {
+		$variable = $this->getVariable($varName, $searchParents, $errorEnable);
 		return isset($variable) ? $variable->getValue() : null;
 	}
class IssetModifierCompiler extends Base
{
  public function compile($params, Template $compiler)
  {
    $params = array_filter($params);

    if (count($params) < 1) {
      throw new CompilerException("Invalid number of arguments for isset. isset expects at least one parameter.");
    }

    $TESTS = [];
    foreach ($params as $param) {
      $param = preg_replace('/(\$_smarty_tpl->getValue\(\'(.+?)\')\)/', '\1, true, false)', $param);
      $TESTS[] = 'null !== (' . $param . ' ?? null)';
    }
    return '(' . implode(' && ', $TESTS) . ')';
  }

}

kaliberx avatar Mar 13 '25 08:03 kaliberx