Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Call to a member function method() on null #972

Closed
wants to merge 10 commits into from
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [5.0.1] - 2024-03-27
- Fix error in Smarty\Smarty::compileAllTemplates() by including missing FilesystemIterator class [#966](https://github.com/smarty-php/smarty/issues/966)


## [5.0.0] - 2024-03-25
- Fixed that scoped variables would overwrite parent scope [#952](https://github.com/smarty-php/smarty/issues/952)
- Removed publicly accessible `$tpl->_var_stack` variable
Expand Down
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)
1 change: 1 addition & 0 deletions changelog/933.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Using stream variables in templates now throws a deprecation notice [#933](https://github.com/smarty-php/smarty/pull/933)
2 changes: 2 additions & 0 deletions changelog/937.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Documented support for `{if $element is in $array}` syntax [#937](https://github.com/smarty-php/smarty/issues/937)
- Added support for `{if $element is not in $array}` syntax [#937](https://github.com/smarty-php/smarty/issues/937)
3 changes: 3 additions & 0 deletions docs/api/variables/streams.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ template.
{$foo:bar}
```

NB. Support for using streams to call variables is deprecated since Smarty v5.1 and will be removed
in a future version.

See also [`Template Resources`](../resources.md)
24 changes: 24 additions & 0 deletions docs/designers/language-basic-syntax/language-syntax-operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,30 @@ Various basic operators can be applied directly to variable values.
> complex, it may be a good idea to move the bits that do not deal
> explicitly with presentation to PHP by way of plugins or modifiers.

## List
The following is a list of recognized operators, which must be
separated from surrounding elements by spaces. Note that items listed in
\[brackets\] are optional. PHP equivalents are shown where applicable.

| Operator | Alternates | Syntax Example | Meaning | PHP Equivalent |
|--------------------|------------|----------------------|--------------------------------|--------------------|
| == | eq | $a eq $b | equals | == |
| != | ne, neq | $a neq $b | not equals | != |
| > | gt | $a gt $b | greater than | > |
| < | lt | $a lt $b | less than | < |
| >= | gte, ge | $a ge $b | greater than or equal | >= |
| <= | lte, le | $a le $b | less than or equal | <= |
| === | | $a === 0 | check for identity | === |
| ! | not | not $a | negation (unary) | ! |
| % | mod | $a mod $b | modulo | % |
| is \[not\] div by | | $a is not div by 4 | divisible by | $a % $b == 0 |
| is \[not\] even | | $a is not even | \[not\] an even number (unary) | $a % 2 == 0 |
| is \[not\] even by | | $a is not even by $b | grouping level \[not\] even | ($a / $b) % 2 == 0 |
| is \[not\] odd | | $a is not odd | \[not\] an odd number (unary) | $a % 2 != 0 |
| is \[not\] odd by | | $a is not odd by $b | \[not\] an odd grouping | ($a / $b) % 2 != 0 |
| is in | | $a is in $b | exists in array | in_array($a, $b) |
| is \[not\] in | | $a is not in $b | does not exist in array | !in_array($a, $b) |

## Ternary
You can use the `?:` (or ternary) operator to test one expression and present the value
of the second or third expression, based on the result of the test.
Expand Down
28 changes: 2 additions & 26 deletions docs/designers/language-builtin-functions/language-function-if.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,8 @@
`{if}` statements in Smarty have much the same flexibility as PHP
[if](https://www.php.net/if) statements, with a few added features for the
template engine. Every `{if}` must be paired with a matching `{/if}`.
`{else}` and `{elseif}` are also permitted. All PHP conditionals and
functions are recognized, such as *\|\|*, *or*, *&&*, *and*,
*is_array()*, etc.

The following is a list of recognized qualifiers, which must be
separated from surrounding elements by spaces. Note that items listed in
\[brackets\] are optional. PHP equivalents are shown where applicable.

## Qualifiers

| Qualifier | Alternates | Syntax Example | Meaning | PHP Equivalent |
|--------------------|------------|----------------------|--------------------------------|--------------------|
| == | eq | $a eq $b | equals | == |
| != | ne, neq | $a neq $b | not equals | != |
| > | gt | $a gt $b | greater than | > |
| < | lt | $a lt $b | less than | < |
| >= | gte, ge | $a ge $b | greater than or equal | >= |
| <= | lte, le | $a le $b | less than or equal | <= |
| === | | $a === 0 | check for identity | === |
| ! | not | not $a | negation (unary) | ! |
| % | mod | $a mod $b | modulo | % |
| is \[not\] div by | | $a is not div by 4 | divisible by | $a % $b == 0 |
| is \[not\] even | | $a is not even | \[not\] an even number (unary) | $a % 2 == 0 |
| is \[not\] even by | | $a is not even by $b | grouping level \[not\] even | ($a / $b) % 2 == 0 |
| is \[not\] odd | | $a is not odd | \[not\] an odd number (unary) | $a % 2 != 0 |
| is \[not\] odd by | | $a is not odd by $b | \[not\] an odd grouping | ($a / $b) % 2 != 0 |
`{else}` and `{elseif}` are also permitted. All [operators](../language-basic-syntax/language-syntax-operators.md) are recognized, such as *==*,
*\|\|*, *or*, *&&*, *and*, etc and you can use modifiers as functions, such as *is_array()*.

## Examples
```smarty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,8 @@
`{while}` loops in Smarty have much the same flexibility as PHP
[while](https://www.php.net/while) statements, with a few added features for
the template engine. Every `{while}` must be paired with a matching
`{/while}`. All PHP conditionals and functions are recognized, such as
*\|\|*, *or*, *&&*, *and*, *is_array()*, etc.

The following is a list of recognized qualifiers, which must be
separated from surrounding elements by spaces. Note that items listed in
\[brackets\] are optional. PHP equivalents are shown where applicable.

## Qualifiers

| Qualifier | Alternates | Syntax Example | Meaning | PHP Equivalent |
|--------------------|------------|----------------------|--------------------------------|--------------------|
| == | eq | $a eq $b | equals | == |
| != | ne, neq | $a neq $b | not equals | != |
| > | gt | $a gt $b | greater than | > |
| < | lt | $a lt $b | less than | < |
| >= | gte, ge | $a ge $b | greater than or equal | >= |
| <= | lte, le | $a le $b | less than or equal | <= |
| === | | $a === 0 | check for identity | === |
| ! | not | not $a | negation (unary) | ! |
| % | mod | $a mod $b | modulo | % |
| is \[not\] div by | | $a is not div by 4 | divisible by | $a % $b == 0 |
| is \[not\] even | | $a is not even | \[not\] an even number (unary) | $a % 2 == 0 |
| is \[not\] even by | | $a is not even by $b | grouping level \[not\] even | ($a / $b) % 2 == 0 |
| is \[not\] odd | | $a is not odd | \[not\] an odd number (unary) | $a % 2 != 0 |
| is \[not\] odd by | | $a is not odd by $b | \[not\] an odd grouping | ($a / $b) % 2 != 0 |
`{/while}`. All [operators](../language-basic-syntax/language-syntax-operators.md) are recognized, such as *==*,
*\|\|*, *or*, *&&*, *and*, etc and you can use modifiers as functions, such as *is_array()*.

## Examples
```smarty
Expand Down
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