Skip to content

Commit

Permalink
Prevent notices on null to string conversion in Template::appendCode (#…
Browse files Browse the repository at this point in the history
…1002)

Fixes #996
  • Loading branch information
wisskid committed Apr 19, 2024
1 parent 9a8702d commit f411247
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 10 deletions.
1 change: 1 addition & 0 deletions changelog/996.md
@@ -0,0 +1 @@
- Prevent deprecation notices during compilation in PHP8.3 [#996](https://github.com/smarty-php/smarty/issues/996)
5 changes: 3 additions & 2 deletions src/Compiler/Template.php
Expand Up @@ -680,7 +680,8 @@ public function getPluginFromDefaultHandler($tag, $plugin_type) {
*
* @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)) {
$left = preg_replace('/\s*\?>\s?$/D', "\n", $left);
$left .= preg_replace('/^<\?php\s+/', '', $right);
Expand Down Expand Up @@ -1056,7 +1057,7 @@ public function getPrefixCode() {
$prefixArray = array_merge($this->prefix_code, array_pop($this->prefixCodeStack));
$this->prefixCodeStack[] = [];
foreach ($prefixArray as $c) {
$code = $this->appendCode($code, $c);
$code = $this->appendCode($code, (string) $c);
}
$this->prefix_code = [];
return $code;
Expand Down
6 changes: 3 additions & 3 deletions src/ParseTree/Dq.php
Expand Up @@ -47,18 +47,18 @@ public function append_subtree(\Smarty\Parser\TemplateParser $parser, Base $subt
if ($subtree instanceof Code) {
$this->subtrees[ $last_subtree ]->data =
$parser->compiler->appendCode(
$this->subtrees[ $last_subtree ]->data,
(string) $this->subtrees[ $last_subtree ]->data,
'<?php echo ' . $subtree->data . ';?>'
);
} elseif ($subtree instanceof DqContent) {
$this->subtrees[ $last_subtree ]->data =
$parser->compiler->appendCode(
$this->subtrees[ $last_subtree ]->data,
(string) $this->subtrees[ $last_subtree ]->data,
'<?php echo "' . $subtree->data . '";?>'
);
} else {
$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 {
$this->subtrees[] = $subtree;
Expand Down
4 changes: 2 additions & 2 deletions src/ParseTree/Tag.php
Expand Up @@ -62,9 +62,9 @@ public function to_smarty_php(\Smarty\Parser\TemplateParser $parser)
public function assign_to_var(\Smarty\Parser\TemplateParser $parser)
{
$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();?>");
$parser->compiler->appendPrefixCode((string) $tmp);
$parser->compiler->appendPrefixCode($tmp);
return $var;
}
}
2 changes: 1 addition & 1 deletion src/ParseTree/Template.php
Expand Up @@ -114,7 +114,7 @@ public function to_smarty_php(\Smarty\Parser\TemplateParser $parser)
break;
case 'tag':
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;
break;
Expand Down
2 changes: 1 addition & 1 deletion src/Parser/TemplateParser.php
Expand Up @@ -2536,7 +2536,7 @@ public function yy_r100(){
// line 806 "src/Parser/TemplateParser.y"
public function yy_r101(){
$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->_retvalue = $prefixVar;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Parser/TemplateParser.y
Expand Up @@ -805,7 +805,7 @@ value(res) ::= varindexed(vi) DOUBLECOLON static_class_access(r). {
// Smarty tag
value(res) ::= smartytag(st). {
$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();?>"));
res = $prefixVar;
}
Expand Down

0 comments on commit f411247

Please sign in to comment.