What's the Fix for PHP 7.2 Count() Issue?

General discussion of development ideas and the approaches taken in the 3.x branch of phpBB. The current feature release of phpBB 3 is 3.3/Proteus.
Forum rules
Please do not post support questions regarding installing, updating, or upgrading phpBB 3.3.x. If you need support for phpBB 3.3.x please visit the 3.3.x Support Forum on phpbb.com.

If you have questions regarding writing extensions please post in Extension Writers Discussion to receive proper guidance from our staff and community.
Frank Rizzo
Registered User
Posts: 17
Joined: Mon Nov 30, 2015 2:45 pm

What's the Fix for PHP 7.2 Count() Issue?

Post by Frank Rizzo »

PHP 7.2 throws various errors due to the way count() is now handled.

e.g. when browsing the forum index:

My Forum Section
My Forum Description [phpbb Debug] Warning: in file [ROOT} /vendor/twig/twig/lib/Twig/Extension/Core.php on line 1275: count(): Parameter must be an array or an object that implements Countable

The error was first reported here:

https://tracker.phpbb.com/browse/PHPBB3-14972

but that states that it has been fixed for 3.3.0-a1

Can the fix be applied to 3.2? What was the fix exactly?

User avatar
david63
Registered User
Posts: 355
Joined: Mon Feb 07, 2005 7:23 am

Re: What's the Fix for PHP 7.2 Count() Issue?

Post by david63 »

Frank Rizzo wrote: Fri Dec 08, 2017 4:21 pm What was the fix exactly?
This looks like the PR - https://github.com/phpbb/phpbb/commit/7 ... d0c1f7a815
David
Remember: You only know what you know -
and you do not know what you do not know!

Frank Rizzo
Registered User
Posts: 17
Joined: Mon Nov 30, 2015 2:45 pm

Re: What's the Fix for PHP 7.2 Count() Issue?

Post by Frank Rizzo »

Having had a look at this it's obvious the specific error I quoted above is due to twig and not phpbb.

This solves the twig\core.php error:

https://github.com/twigphp/Twig/commit/ ... 0468eef74f

So the solution for this is for the phpBB package to have the version of twig that has the updated files.

Frank Rizzo
Registered User
Posts: 17
Joined: Mon Nov 30, 2015 2:45 pm

Re: What's the Fix for PHP 7.2 Count() Issue?

Post by Frank Rizzo »

I edited the core.php file, changing the section for twig_length_filter from:

Code: Select all

    /**
     * Returns the length of a variable.
     *
     * @param Twig_Environment $env
     * @param mixed            $thing A variable
     *
     * @return int The length of the value
     */
    function twig_length_filter(Twig_Environment $env, $thing)
    {
        if (is_scalar($thing)) {
            return mb_strlen($thing, $env->getCharset());
        }

        if (is_object($thing) && method_exists($thing, '__toString') && !$thing instanceof \Countable) {
            return mb_strlen((string) $thing, $env->getCharset());
        }

        return count($thing);
    }
to:

Code: Select all

/**
     * Returns the length of a variable.
     *
     * @param Twig_Environment $env
     * @param mixed            $thing A variable
     *
     * @return int The length of the value
     */
    function twig_length_filter(Twig_Environment $env, $thing)
    {
        if (null === $thing) {
            return 0;
        }
        if (is_scalar($thing)) {
            return mb_strlen($thing, $env->getCharset());
        }
        if (is_object($thing) && method_exists($thing, '__toString') && !$thing instanceof \Countable) {
            return mb_strlen((string) $thing, $env->getCharset());
        }
        if ($thing instanceof \Countable || is_array($thing)) {
            return count($thing);
        }
        return 1;
    }
That fixed the problem.

rubencm
Development Team
Development Team
Posts: 32
Joined: Sun Mar 12, 2017 8:30 pm
Location: h[b]ell[/b]o

Re: What's the Fix for PHP 7.2 Count() Issue?

Post by rubencm »

I think that phpbb3.2 only supports until 7.1
phpBB3.3 suppots from 7.0 to 7.2

koraldon
Registered User
Posts: 33
Joined: Thu Mar 10, 2005 12:06 pm

Re: What's the Fix for PHP 7.2 Count() Issue?

Post by koraldon »

So phpbb will not support the latest php version ? That is strange considering that 3.3. is not released and not even in alpha.

User avatar
DavidIQ
Customisations Team Leader
Customisations Team Leader
Posts: 1904
Joined: Thu Mar 02, 2006 4:29 pm
Location: Earth
Contact:

Re: What's the Fix for PHP 7.2 Count() Issue?

Post by DavidIQ »

koraldon wrote: Wed Dec 20, 2017 4:19 pm So phpbb will not support the latest php version ? That is strange considering that 3.3. is not released and not even in alpha.
You must have missed or ignored the part about it being a Twig error and not phpbb... The fix would obviously be to update Twig, except that it's not that simple. Along with the fix above there would end up being other core changes needed just to support the version of Twig where they fixed PHP 7.2 compatibility.
Image

koraldon
Registered User
Posts: 33
Joined: Thu Mar 10, 2005 12:06 pm

Re: What's the Fix for PHP 7.2 Count() Issue?

Post by koraldon »

Okay, but the bottom line is the same. It means that in the near future phpbb is not compatible with the latest version of php.
I also see the fix by Frank, can't it be implemented as a stopgap measure?

User avatar
DavidIQ
Customisations Team Leader
Customisations Team Leader
Posts: 1904
Joined: Thu Mar 02, 2006 4:29 pm
Location: Earth
Contact:

Re: What's the Fix for PHP 7.2 Count() Issue?

Post by DavidIQ »

That's in the actual Twig package, a dependency, and not in the phpBB code.
Image

User avatar
rxu
Registered User
Posts: 164
Joined: Tue Apr 04, 2006 4:28 pm
Contact:

Re: What's the Fix for PHP 7.2 Count() Issue?

Post by rxu »

Frank Rizzo wrote: Fri Dec 08, 2017 4:21 pm Can the fix be applied to 3.2?
3.2.x won't support PHP 7.2+ for technical reasons, so the answer is no, it can't.
Image

Post Reply