Prevent notices on null to string conversion in Template::appendCode

Fixes #996
This commit is contained in:
Simon Wisselink
2024-04-19 10:52:10 +02:00
parent 9a8702d937
commit c2a86bebe3
7 changed files with 12 additions and 10 deletions

1
changelog/996.md Normal file
View File

@ -0,0 +1 @@
- Prevent deprecation notices during compilation in PHP8.3 [#996](https://github.com/smarty-php/smarty/issues/996)

View File

@ -680,7 +680,8 @@ class Template extends BaseCompiler {
* *
* @return string * @return string
*/ */
public function appendCode($left, $right) { public function appendCode(string $left, string $right): string
{
if (preg_match('/\s*\?>\s?$/D', $left) && preg_match('/^<\?php\s+/', $right)) { if (preg_match('/\s*\?>\s?$/D', $left) && preg_match('/^<\?php\s+/', $right)) {
$left = preg_replace('/\s*\?>\s?$/D', "\n", $left); $left = preg_replace('/\s*\?>\s?$/D', "\n", $left);
$left .= preg_replace('/^<\?php\s+/', '', $right); $left .= preg_replace('/^<\?php\s+/', '', $right);
@ -1056,7 +1057,7 @@ class Template extends BaseCompiler {
$prefixArray = array_merge($this->prefix_code, array_pop($this->prefixCodeStack)); $prefixArray = array_merge($this->prefix_code, array_pop($this->prefixCodeStack));
$this->prefixCodeStack[] = []; $this->prefixCodeStack[] = [];
foreach ($prefixArray as $c) { foreach ($prefixArray as $c) {
$code = $this->appendCode($code, $c); $code = $this->appendCode($code, (string) $c);
} }
$this->prefix_code = []; $this->prefix_code = [];
return $code; return $code;

View File

@ -47,18 +47,18 @@ class Dq extends Base
if ($subtree instanceof Code) { if ($subtree instanceof Code) {
$this->subtrees[ $last_subtree ]->data = $this->subtrees[ $last_subtree ]->data =
$parser->compiler->appendCode( $parser->compiler->appendCode(
$this->subtrees[ $last_subtree ]->data, (string) $this->subtrees[ $last_subtree ]->data,
'<?php echo ' . $subtree->data . ';?>' '<?php echo ' . $subtree->data . ';?>'
); );
} elseif ($subtree instanceof DqContent) { } elseif ($subtree instanceof DqContent) {
$this->subtrees[ $last_subtree ]->data = $this->subtrees[ $last_subtree ]->data =
$parser->compiler->appendCode( $parser->compiler->appendCode(
$this->subtrees[ $last_subtree ]->data, (string) $this->subtrees[ $last_subtree ]->data,
'<?php echo "' . $subtree->data . '";?>' '<?php echo "' . $subtree->data . '";?>'
); );
} else { } else {
$this->subtrees[ $last_subtree ]->data = $this->subtrees[ $last_subtree ]->data =
$parser->compiler->appendCode($this->subtrees[ $last_subtree ]->data, $subtree->data); $parser->compiler->appendCode((string) $this->subtrees[ $last_subtree ]->data, (string) $subtree->data);
} }
} else { } else {
$this->subtrees[] = $subtree; $this->subtrees[] = $subtree;

View File

@ -62,9 +62,9 @@ class Tag extends Base
public function assign_to_var(\Smarty\Parser\TemplateParser $parser) public function assign_to_var(\Smarty\Parser\TemplateParser $parser)
{ {
$var = $parser->compiler->getNewPrefixVariable(); $var = $parser->compiler->getNewPrefixVariable();
$tmp = $parser->compiler->appendCode('<?php ob_start();?>', $this->data); $tmp = $parser->compiler->appendCode('<?php ob_start();?>', (string) $this->data);
$tmp = $parser->compiler->appendCode($tmp, "<?php {$var}=ob_get_clean();?>"); $tmp = $parser->compiler->appendCode($tmp, "<?php {$var}=ob_get_clean();?>");
$parser->compiler->appendPrefixCode((string) $tmp); $parser->compiler->appendPrefixCode($tmp);
return $var; return $var;
} }
} }

View File

@ -114,7 +114,7 @@ class Template extends Base
break; break;
case 'tag': case 'tag':
foreach ($chunk['subtrees'] as $subtree) { foreach ($chunk['subtrees'] as $subtree) {
$text = $parser->compiler->appendCode($text, $subtree->to_smarty_php($parser)); $text = $parser->compiler->appendCode($text, (string) $subtree->to_smarty_php($parser));
} }
$code .= $text; $code .= $text;
break; break;

View File

@ -2536,7 +2536,7 @@ public static $yy_action = array(
// line 806 "src/Parser/TemplateParser.y" // line 806 "src/Parser/TemplateParser.y"
public function yy_r101(){ public function yy_r101(){
$prefixVar = $this->compiler->getNewPrefixVariable(); $prefixVar = $this->compiler->getNewPrefixVariable();
$tmp = $this->compiler->appendCode('<?php ob_start();?>', $this->yystack[$this->yyidx + 0]->minor); $tmp = $this->compiler->appendCode('<?php ob_start();?>', (string) $this->yystack[$this->yyidx + 0]->minor);
$this->compiler->appendPrefixCode($this->compiler->appendCode($tmp, "<?php {$prefixVar} = ob_get_clean();?>")); $this->compiler->appendPrefixCode($this->compiler->appendCode($tmp, "<?php {$prefixVar} = ob_get_clean();?>"));
$this->_retvalue = $prefixVar; $this->_retvalue = $prefixVar;
} }

View File

@ -805,7 +805,7 @@ value(res) ::= varindexed(vi) DOUBLECOLON static_class_access(r). {
// Smarty tag // Smarty tag
value(res) ::= smartytag(st). { value(res) ::= smartytag(st). {
$prefixVar = $this->compiler->getNewPrefixVariable(); $prefixVar = $this->compiler->getNewPrefixVariable();
$tmp = $this->compiler->appendCode('<?php ob_start();?>', st); $tmp = $this->compiler->appendCode('<?php ob_start();?>', (string) st);
$this->compiler->appendPrefixCode($this->compiler->appendCode($tmp, "<?php {$prefixVar} = ob_get_clean();?>")); $this->compiler->appendPrefixCode($this->compiler->appendCode($tmp, "<?php {$prefixVar} = ob_get_clean();?>"));
res = $prefixVar; res = $prefixVar;
} }