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

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

Closed
alexgit2k opened this issue Mar 30, 2022 · 6 comments
Closed

Comments

@alexgit2k
Copy link

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

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

@wisskid
Copy link
Contributor

wisskid commented Apr 26, 2022

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

@olelis
Copy link

olelis commented Jun 15, 2022

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.

@gkreitz
Copy link
Contributor

gkreitz commented Jun 28, 2022

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

@pehrsoderman
Copy link

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

@dleffler
Copy link

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);
}

@wisskid
Copy link
Contributor

wisskid commented Sep 9, 2022

This is now fixed by #755 with only the strftime issue remaining, but we have a seperate issue for strtime (see #672 )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants