smarty-php / smarty-php/smarty

Isset and unassigned variable issue in v5.4.3

Open
#1,112 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
2.3k
Forks
709
PR merge metrics
No merged PRs in 30d

Description

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) . ')';
  }

}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the warning with the two Smarty display examples, then inspect the yacc parser handling of isset and the referenced Data.php and IssetModifierCompiler code. Done means isset($var.item) reports no PHP warning and returns the expected result when the variable or field is unassigned.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.