Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

php72 compat: Countable

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Bugs
View previous topic :: View next topic  
Author Message
oschonrock
Smarty Rookie


Joined: 08 Aug 2003
Posts: 13

PostPosted: Fri Jan 19, 2018 1:28 am    Post subject: php72 compat: Countable Reply with quote

Firstly a huge thank you. Both for smarty in general and for bothering to maintain even smarty 2 to be php72 compat. This saved me huge amounts of time when making some legacy smarty 2 code compat with php72.

There is a tiny link issue here:
https://www.smarty.net/

In that your announcement for smarty 2.6.31 actually links to 2.6.30.

Then I found another tiny issue due to php72's "count() must be countable" deprecation.

Smarty 2.6.31 seems to compile the following tpl code:
Code:
{foreach from=$categories item="category"}

into
Code:
<?php $_from = $this->_tpl_vars['categories']; if (!is_array($_from) && !is_object($_from)) { settype($_from, 'array'); }if (count($_from)):
    foreach ($_from as $this->_tpl_vars['category']):


if $categories is not countable php72 complains with:

Code:

PHP Warning:  count(): Parameter must be an array or an object that implements Countable


Below are the lines in the compiler class which appear to be responsible. While coping with php72 compat changes I wrote this workaround function:

Code:

function qcount($v)
{
  if (is_array($v) || $v instanceof Countable)
  {
    return count($v);
  }
  return 0;
}


It's not great, and just a workaround, but it worked for me. I suspect any changes to the compiler class would want to be more clever.

Anyway, given that function, I found that the following patch to the compiler class, gets rid of the php72 warnings, and makes the code work as before:

Code:

diff -u libs/Smarty_Compiler.class.php.orig libs/Smarty_Compiler.class.php
--- libs/Smarty_Compiler.class.php.orig 2018-01-19 01:08:58.909041000 +0000
+++ libs/Smarty_Compiler.class.php      2018-01-19 01:09:15.721388000 +0000
@@ -1195,12 +1195,12 @@
         $output .= "\$_from = $from; if (!is_array(\$_from) && !is_object(\$_from)) { settype(\$_from, 'array'); }";
         if (isset($name)) {
             $foreach_props = "\$this->_foreach[$name]";
-            $output .= "{$foreach_props} = array('total' => count(\$_from), 'iteration' => 0);\n";
+            $output .= "{$foreach_props} = array('total' => qcount(\$_from), 'iteration' => 0);\n";
             $output .= "if ({$foreach_props}['total'] > 0):\n";
             $output .= "    foreach (\$_from as $key_part\$this->_tpl_vars['$item']):\n";
             $output .= "        {$foreach_props}['iteration']++;\n";
         } else {
-            $output .= "if (count(\$_from)):\n";
+            $output .= "if (qcount(\$_from)):\n";
             $output .= "    foreach (\$_from as $key_part\$this->_tpl_vars['$item']):\n";
         }
         $output .= '?>';


Hope that's helpful.

Oliver
Back to top
View user's profile Send private message
rabe85
Smarty n00b


Joined: 16 Jun 2018
Posts: 1

PostPosted: Sat Jun 16, 2018 9:13 pm    Post subject: Reply with quote

I have the same PHP 7.2 Warning with smarty 3.1.32.

Code:
count(): Parameter must be an array or an object that implements Countable in ...


My code:
Code:
{if count($enable_autoselect) eq true}
 <div>test</div>
{/if}

I tried to make something like this, but smarty gives me a syntax error (Unexpected "?"):
Code:
{if (isset($enable_autoselect)?count($enable_autoselect):0) eq true}
 <div>test</div>
{/if}



Any idea how to fix it or where i can add your qcount function?
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sun Jun 17, 2018 12:49 am    Post subject: Reply with quote

1. This is merely a warning. Don't sweat it.
2. This topic is about Smarty2.
3. If you are sure it's 3.1.32, please file an issue and may be provide a test case.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Bugs All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP