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 next feature release of phpBB 3 will be 3.3/Proteus.
Forum rules
Please do not post support questions regarding installing, updating, or upgrading phpBB 3.1. If you need support for phpBB 3.1 please visit the 3.1.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: 15
Joined: Mon Nov 30, 2015 2:45 pm

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

Post by Frank Rizzo » Fri Dec 08, 2017 4:21 pm

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: 245
Joined: Mon Feb 07, 2005 7:23 am
Location: Lancashire, UK

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

Post by david63 » Fri Dec 08, 2017 10:07 pm

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: 15
Joined: Mon Nov 30, 2015 2:45 pm

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

Post by Frank Rizzo » Sat Dec 09, 2017 11:28 am

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: 15
Joined: Mon Nov 30, 2015 2:45 pm

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

Post by Frank Rizzo » Sat Dec 09, 2017 11:33 am

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.

User avatar
rubencm
Registered User
Posts: 21
Joined: Sun Mar 12, 2017 8:30 pm

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

Post by rubencm » Tue Dec 19, 2017 11:14 am

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

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

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

Post by koraldon » 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.

User avatar
DavidIQ
Customisations Team Leader
Customisations Team Leader
Posts: 1777
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 » Wed Dec 20, 2017 5:02 pm

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 Online
Registered User
Posts: 24
Joined: Thu Mar 10, 2005 12:06 pm

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

Post by koraldon » Wed Dec 20, 2017 7:19 pm

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: 1777
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 » Wed Dec 20, 2017 7:33 pm

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

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

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

Post by rxu » Fri Dec 29, 2017 5:46 pm

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