Skip to content

Commit

Permalink
Internal compiler classes always return a string (#918)
Browse files Browse the repository at this point in the history
* Internal compiler classes always return a string (the internal has_code flag has been removed for simplicity)
* Add :string method signature to compile method everywhere.
  • Loading branch information
wisskid authored Mar 25, 2024
1 parent e161bab commit 7255b4d
Show file tree
Hide file tree
Showing 58 changed files with 253 additions and 246 deletions.
1 change: 0 additions & 1 deletion TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,4 @@

## Unrelated / other
- review (and avoid) use of 'clone' keyword
- compiler->has_code seems silly. Why not have proper return values?
- what is 'user literal support', why are unit tests skipped?
1 change: 1 addition & 0 deletions changelog/918.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Internal compiler classes always return a string (the internal has_code flag has been removed for simplicity) [#918](https://github.com/smarty-php/smarty/pull/918)
4 changes: 2 additions & 2 deletions src/Compile/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ protected function convertScope($scope): int {
* @param Template $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return bool|string compiled code or true if no code has been compiled
* @return string compiled code as a string
* @throws \Smarty\CompilerException
*/
abstract public function compile($args, Template $compiler, $parameter = array(), $tag = null, $function = null);
abstract public function compile($args, Template $compiler, $parameter = array(), $tag = null, $function = null): string;
}
5 changes: 2 additions & 3 deletions src/Compile/BlockCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class BlockCompiler extends Base {
* @throws CompilerException
* @throws Exception
*/
public function compile($args, Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, Template $compiler, $parameter = [], $tag = null, $function = null): string
{

if (!isset($tag[5]) || substr($tag, -5) !== 'close') {
$output = $this->compileOpeningTag($compiler, $args, $tag, $function);
Expand All @@ -77,7 +78,6 @@ public function compileChild(\Smarty\Compiler\Template $compiler) {
);
}
$compiler->_cache['blockParams'][$compiler->_cache['blockNesting']]['callsChild'] = true;
$compiler->has_code = true;
$compiler->suppressNocacheProcessing = true;

$output = "<?php \n";
Expand All @@ -102,7 +102,6 @@ public function compileParent(\Smarty\Compiler\Template $compiler) {
$compiler->getParser()->lex->taglineno
);
}
$compiler->has_code = true;
$compiler->suppressNocacheProcessing = true;

$output = "<?php \n";
Expand Down
4 changes: 2 additions & 2 deletions src/Compile/CompilerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ interface CompilerInterface {
* @param \Smarty\Compiler\Template $compiler compiler object
* @param array $parameter array with compilation parameter
*
* @return bool|string compiled code or true if no code has been compiled
* @return string compiled code as a string
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null);
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string;

public function isCacheable(): bool;
}
3 changes: 2 additions & 1 deletion src/Compile/DefaultHandlerFunctionCallCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class DefaultHandlerFunctionCallCompiler extends Base {
* @throws \Smarty\CompilerException
* @throws \Smarty\Exception
*/
public function compile($args, Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
unset($_attr['nocache']);
Expand Down
3 changes: 2 additions & 1 deletion src/Compile/FunctionCallCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ class FunctionCallCompiler extends Base {
* @throws \Smarty\CompilerException
* @throws \Smarty\Exception
*/
public function compile($args, Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, Template $compiler, $parameter = [], $tag = null, $function = null): string
{

// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
Expand Down
5 changes: 2 additions & 3 deletions src/Compile/ModifierCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ class ModifierCompiler extends Base {
* @throws \Smarty\CompilerException
* @throws \Smarty\Exception
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {

$compiler->has_code = true;
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{

$output = $parameter['value'];

Expand Down
3 changes: 2 additions & 1 deletion src/Compile/ObjectMethodCallCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ class ObjectMethodCallCompiler extends Base {
* @throws \Smarty\CompilerException
* @throws \Smarty\Exception
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
unset($_attr['nocache']);
Expand Down
5 changes: 2 additions & 3 deletions src/Compile/PrintExpressionCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ class PrintExpressionCompiler extends Base {
* @return string
* @throws \Smarty\Exception
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {

$compiler->has_code = true;
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{

// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
Expand Down
7 changes: 4 additions & 3 deletions src/Compile/SpecialVariableCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ class SpecialVariableCompiler extends Base {
* @return string compiled code
* @throws CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {

$compiler->has_code = true;
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{

$_index = preg_split("/\]\[/", substr($parameter, 1, strlen($parameter) - 2));
$variable = smarty_strtolower_ascii($compiler->getId($_index[0]));
Expand Down Expand Up @@ -129,5 +128,7 @@ public function compile($args, \Smarty\Compiler\Template $compiler, $parameter =
}
return $compiled_ref;
}

return '';
}
}
4 changes: 2 additions & 2 deletions src/Compile/Tag/Append.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class Append extends Assign
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = array(), $tag = null, $function = null)
{
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = array(), $tag = null, $function = null): string
{

// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
Expand Down
4 changes: 2 additions & 2 deletions src/Compile/Tag/Assign.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ class Assign extends Base
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = array(), $tag = null, $function = null)
{
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = array(), $tag = null, $function = null): string
{

$_nocache = false;
// check and get attributes
Expand Down
3 changes: 2 additions & 1 deletion src/Compile/Tag/BCPluginWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public function __construct($callback, bool $cacheable = true) {
/**
* @inheritDoc
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
return call_user_func($this->callback, $this->getAttributes($compiler, $args), $compiler->getSmarty());
}
}
3 changes: 2 additions & 1 deletion src/Compile/Tag/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Block extends Inheritance {
* @param \Smarty\Compiler\Template $compiler compiler object
* @param array $parameter array with compilation parameter
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = array(), $tag = null, $function = null)
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = array(), $tag = null, $function = null): string
{
if (!isset($compiler->_cache['blockNesting'])) {
$compiler->_cache['blockNesting'] = 0;
Expand Down Expand Up @@ -87,5 +87,6 @@ public function compile($args, \Smarty\Compiler\Template $compiler, $parameter =
$compiler->getParser()->current_buffer = new Template();
$compiler->getTemplate()->getCompiled()->setNocacheCode(false);
$compiler->suppressNocacheProcessing = true;
return '';
}
}
3 changes: 1 addition & 2 deletions src/Compile/Tag/BlockClose.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class BlockClose extends Inheritance {
*
* @return bool true
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = array(), $tag = null, $function = null)
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = array(), $tag = null, $function = null): string
{
[$_attr, $_nocache, $_buffer, $_has_nocache_code, $_className] = $this->closeTag($compiler, ['block']);

Expand Down Expand Up @@ -103,7 +103,6 @@ public function compile($args, \Smarty\Compiler\Template $compiler, $parameter =
if ($compiler->_cache['blockNesting'] === 0) {
unset($compiler->_cache['blockNesting']);
}
$compiler->has_code = true;
$compiler->suppressNocacheProcessing = true;
return $output;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Compile/Tag/BreakTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class BreakTag extends Base {
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = array(), $tag = null, $function = null)
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = array(), $tag = null, $function = null): string
{
[$levels, $foreachLevels] = $this->checkLevels($args, $compiler);
$output = "<?php ";
Expand Down
3 changes: 2 additions & 1 deletion src/Compile/Tag/Call.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ class Call extends Base {
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
// save possible attributes
Expand Down
6 changes: 3 additions & 3 deletions src/Compile/Tag/Capture.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public static function compileSpecialVariable(
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
$buffer = $_attr['name'] ?? "'default'";
Expand All @@ -66,7 +67,6 @@ public function compile($args, \Smarty\Compiler\Template $compiler, $parameter =
$compiler->openTag('nocache');
}

$_output = "<?php \$_smarty_tpl->getSmarty()->getRuntime('Capture')->open(\$_smarty_tpl, $buffer, $assign, $append);?>";
return $_output;
return "<?php \$_smarty_tpl->getSmarty()->getRuntime('Capture')->open(\$_smarty_tpl, $buffer, $assign, $append);?>";
}
}
3 changes: 2 additions & 1 deletion src/Compile/Tag/CaptureClose.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class CaptureClose extends Base {
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{

if (array_pop($compiler->_cache['capture_stack'])) {
// pop the virtual {nocache} tag from the stack.
Expand Down
3 changes: 2 additions & 1 deletion src/Compile/Tag/ConfigLoad.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ class ConfigLoad extends Base {
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
if ($_attr['nocache'] === true) {
Expand Down
3 changes: 2 additions & 1 deletion src/Compile/Tag/Debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class Debug extends Base {
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes, may trigger errors
$this->getAttributes($compiler, $args);

Expand Down
3 changes: 2 additions & 1 deletion src/Compile/Tag/ElseIfTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class ElseIfTag extends Base {
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{

[$nesting, $nocache_pushed] = $this->closeTag($compiler, ['if', 'elseif']);

Expand Down
3 changes: 2 additions & 1 deletion src/Compile/Tag/ElseTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class ElseTag extends Base {
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
[$nesting, $compiler->tag_nocache] = $this->closeTag($compiler, ['if', 'elseif']);
$this->openTag($compiler, 'else', [$nesting, $compiler->tag_nocache]);
return '<?php } else { ?>';
Expand Down
3 changes: 2 additions & 1 deletion src/Compile/Tag/EvalTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class EvalTag extends Base {
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
if (isset($_attr['assign'])) {
Expand Down
4 changes: 2 additions & 2 deletions src/Compile/Tag/ExtendsTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class ExtendsTag extends Inheritance {
* @throws \Smarty\CompilerException
* @throws \Smarty\Exception
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
// check and get attributes
$_attr = $this->getAttributes($compiler, $args);
if ($_attr['nocache'] === true) {
Expand Down Expand Up @@ -86,7 +87,6 @@ public function compile($args, \Smarty\Compiler\Template $compiler, $parameter =
} else {
$this->compileEndChild($compiler, $_attr['file']);
}
$compiler->has_code = false;
return '';
}

Expand Down
3 changes: 2 additions & 1 deletion src/Compile/Tag/ForClose.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class ForClose extends Base {
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$compiler->loopNesting--;

[$openTag, $nocache_pushed] = $this->closeTag($compiler, ['for', 'forelse']);
Expand Down
3 changes: 2 additions & 1 deletion src/Compile/Tag/ForElse.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class ForElse extends Base {
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
[$tagName, $nocache_pushed] = $this->closeTag($compiler, ['for']);
$this->openTag($compiler, 'forelse', ['forelse', $nocache_pushed]);
return "<?php }} else { ?>";
Expand Down
3 changes: 2 additions & 1 deletion src/Compile/Tag/ForTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class ForTag extends Base {
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$compiler->loopNesting++;
if ($parameter === 0) {
$this->required_attributes = ['start', 'to'];
Expand Down
3 changes: 2 additions & 1 deletion src/Compile/Tag/ForeachClose.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class ForeachClose extends Base {
* @return string compiled code
* @throws \Smarty\CompilerException
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$compiler->loopNesting--;

[$openTag, $nocache_pushed, $localVariablePrefix, $item, $restore] = $this->closeTag($compiler, ['foreach', 'foreachelse']);
Expand Down
3 changes: 2 additions & 1 deletion src/Compile/Tag/ForeachElse.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class ForeachElse extends Base {
*
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{

[$openTag, $nocache_pushed, $localVariablePrefix, $item, $restore] = $this->closeTag($compiler, ['foreach']);
$this->openTag($compiler, 'foreachelse', ['foreachelse', $nocache_pushed, $localVariablePrefix, $item, false]);
Expand Down
3 changes: 2 additions & 1 deletion src/Compile/Tag/ForeachTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ class ForeachTag extends ForeachSection {
* @throws \Smarty\CompilerException
* @throws \Smarty\Exception
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$compiler->loopNesting++;
// init
$this->isNamed = false;
Expand Down
7 changes: 4 additions & 3 deletions src/Compile/Tag/FunctionClose.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ class FunctionClose extends Base {
* @param array $args array with attributes from parser
* @param object|\Smarty\Compiler\Template $compiler compiler object
*
* @return bool true
* @return string compiled code
*/
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null) {
public function compile($args, \Smarty\Compiler\Template $compiler, $parameter = [], $tag = null, $function = null): string
{
$this->compiler = $compiler;
$saved_data = $this->closeTag($compiler, ['function']);
$_attr = $saved_data[0];
Expand Down Expand Up @@ -140,7 +141,7 @@ public function compile($args, \Smarty\Compiler\Template $compiler, $parameter =
// restore old status
$compiler->getTemplate()->getCompiled()->setNocacheCode($saved_data[2]);
$compiler->getTemplate()->caching = $saved_data[3];
return true;
return '';
}

/**
Expand Down
Loading

0 comments on commit 7255b4d

Please sign in to comment.