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

Throw deprecation notice about unregistered functions without changing the compilation flow #985

Merged
merged 2 commits into from Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 12 additions & 11 deletions libs/sysplugins/smarty_internal_templatecompilerbase.php
Expand Up @@ -640,17 +640,18 @@ public function compilePHPFunctionCall($name, $parameter)
return $func_name . '(' . $parameter[ 0 ] . ')';
}
} else {
$first_param = array_shift($parameter);
$modifier = array_merge(array($name), $parameter);
// Now, compile the function call as a modifier
return $this->compileTag(
'private_modifier',
array(),
array(
'modifierlist' => array($modifier),
'value' => $first_param
)
);

if (
!$this->smarty->loadPlugin('smarty_modifiercompiler_' . $name)
&& !isset($this->smarty->registered_plugins[Smarty::PLUGIN_MODIFIER][$name])
&& !in_array($name, ['time', 'join', 'is_array', 'in_array'])
) {
trigger_error('Using unregistered function "' . $name . '" in a template is deprecated and will be ' .
'removed in a future release. Use Smarty::registerPlugin to explicitly register ' .
'a custom modifier.', E_USER_DEPRECATED);
}

return $name . '(' . implode(',', $parameter) . ')';
}
} else {
$this->trigger_template_error("unknown function '{$name}'");
Expand Down
@@ -0,0 +1,67 @@
<?php

class ArgumentMustBePassedByReference961Test extends PHPUnit_Smarty
{

/**
* @group issue961
*/
public function testReset()
{
$smarty = new Smarty();
$smarty->registerPlugin('modifier', 'reset', 'reset');
$templateStr = "string:{reset(\$ar)}";
$smarty->assign('ar', [1,2,3]);
$this->assertEquals(
'1',
$smarty->fetch($templateStr)
);
}

/**
* @group issue961
* @deprecated
*/
public function testResetAsModifier()
{
$smarty = new Smarty();
try {
$templateStr = "string:{\$ar|reset}";
$smarty->assign('ar', [1,2,3]);
$this->assertEquals(
'1',
$smarty->fetch($templateStr)
);
} catch (Exception $e) {
}
}

/**
* @group issue961
*/
public function testResetInExpression()
{
$smarty = new Smarty();
$smarty->registerPlugin('modifier', 'reset', 'reset');
$templateStr = "string:{if reset(\$ar)}ok{/if}";
$smarty->assign('ar', [1,2,3]);
$this->assertEquals(
'ok',
$smarty->fetch($templateStr)
);
}

/**
* @group issue961
*/
public function testMatch()
{
$smarty = new Smarty();
$smarty->registerPlugin('modifier', 'preg_match', 'preg_match');
$templateStr = 'string:{assign var="match" value=null}{if preg_match(\'/([a-z]{4})/\', "a test", $match)}{$match.1}{/if}';
$this->assertEquals(
'test',
$smarty->fetch($templateStr)
);
}
}