smarty icon indicating copy to clipboard operation
smarty copied to clipboard

PHP 8.1: Deprecation notices for several functions in Smarty 3 and 4

Open alexgit2k opened this issue 3 years ago • 4 comments

There are several deprecation-notices with PHP 8.1 in Smarty 3 and 4:

  • explode: Passing null to parameter
  • number_format: Passing null to parameter
  • escape: Passing null to parameter (see #748)
  • date_format: strftime() is deprecated (see #672)
  • replace: Passing null to parameter

Example

docker run -it --rm php:8.1 /bin/bash

apt-get update -yqq && apt-get install -y git
curl https://getcomposer.org/download/latest-stable/composer.phar > /usr/local/bin/composer
chmod a+rx /usr/local/bin/composer
composer require smarty/smarty:^3

cat <<EOT > test.php
<?php
require_once('vendor/autoload.php');
\$smarty = new Smarty();
\$smarty->assign('value',null);

print '- {\$value|escape} -> htmlspecialchars(\$value, ENT_QUOTES, \'UTF-8\', true);';
\$smarty->display('string:{\$value|escape}');

print '- {count(";"|explode:\$value)} -> explode(";",\$value);';
\$smarty->display('string:{count(";"|explode:\$value)}');

print '- {\$value|number_format} -> number_format(\$value);';
\$smarty->display('string:{\$value|number_format}');

print '- {\$smarty.now|date_format} -> strftime(\$format, \$timestamp);';
\$smarty->display('string:{\$smarty.now|date_format}');

print '- {\$value|replace:"":""} -> mb_split(preg_quote(\$search), \$subject);';
\$smarty->display('string:{\$value|replace:"":""}');

?>
EOT

php test.php

Same deprecation notices with Smarty 4: composer require smarty/smarty No deprecation notices with PHP 8.0: docker run -it --rm php:8.0 /bin/bash

Output

  • {$value|escape} → htmlspecialchars($value, ENT_QUOTES, 'UTF-8', true); Deprecated: htmlspecialchars(): Passing null to parameter #1 ($string) of type string is deprecated in compiled template on line 18

  • {count(";"|explode:$value)} → explode(";",$value); Deprecated: explode(): Passing null to parameter #2 ($string) of type string is deprecated in compiled template on line 18

  • {$value|number_format} → number_format($value); Deprecated: number_format(): Passing null to parameter #1 ($num) of type float is deprecated in compiled template on line 18

  • {$smarty.now|date_format} -> strftime($format, $timestamp); Deprecated: Function strftime() is deprecated in /vendor/smarty/smarty/libs/plugins/modifier.date_format.php on line 81

  • {$value|replace:"":""} -> mb_split(preg_quote($search), $subject); Deprecated: mb_split(): Passing null to parameter #2 ($string) of type string is deprecated in /vendor/smarty/smarty/libs/plugins/shared.mb_str_replace.php on line 47

alexgit2k avatar Mar 30 '22 13:03 alexgit2k

@alexgit2k thanks for the detailed report. Could I tempt you to make a PR for Smarty 4?

wisskid avatar Apr 26 '22 20:04 wisskid

For those who needs quick workaround(like me), you can temporarily silence deprecation errors:

     $smarty = new Smarty();
     $smarty->setErrorReporting(E_ALL ^E_DEPRECATED);

This should be enough for now.

olelis avatar Jun 15 '22 08:06 olelis

One more related issue:

cat <<EOT > test2.php
<?php
require_once('vendor/autoload.php');
\$smarty = new Smarty();
\$smarty->assign('value',null);
\$smarty->escape_html = true;

\$smarty->display('string:{\$value}');
EOT

php test2.php

Gives:

Deprecated: htmlspecialchars(): Passing null to parameter #1 ($string) of type string is deprecated in /templates_c/7d841e9760672e7d6790c1a38e11d9e62b27a4bf_2.string.php on line 18

gkreitz avatar Jun 28 '22 10:06 gkreitz

There seems to be a few fixes here awaiting review and merging. Anything we can do to help speed up the process?

pehrsoderman avatar Jul 05 '22 19:07 pehrsoderman

Same for |regex_replace param #3, possible fix is an elseif empty

function smarty_modifier_regex_replace($string, $search, $replace, $limit = -1)
{
    if (is_array($search)) {
        foreach ($search as $idx => $s) {
            $search[$idx] = _smarty_regex_replace_check($s);
        }
    } elseif (empty($string)) {
        return $string;
    } else {
        $search = _smarty_regex_replace_check($search);
    }
    return preg_replace($search, $replace, $string, $limit);
}

dleffler avatar Aug 24 '22 16:08 dleffler

This is now fixed by https://github.com/smarty-php/smarty/pull/755 with only the strftime issue remaining, but we have a seperate issue for strtime (see https://github.com/smarty-php/smarty/issues/672 )

wisskid avatar Sep 09 '22 20:09 wisskid