timber: strpos() Error in PHP 7.3

Hi Guys. First off, thanks for working on this great project. We’ve incorporated it into our base WordPress theme for many of our new projects, you can see that base theme here: https://github.com/mindgruve/mg-press

I’ve encountered an issue when testing on PHP 7.3. Running a resize filter on an image src property throws a Twig_Error_Runtime exception:

An exception has been thrown during the rendering of a template ("strpos(): Non-string needles will be interpreted as strings in the future. Use an explicit chr() call to preserve the current behavior").

The Twig markup looks like this:

<img src="{{ post.thumbnail.src|resize(1200, 300) }}">

And the stack trace points to line 457 (strpos) in file timber-library/lib/ImageHelper.php:

    protected static function is_in_theme_dir( $path ) {
        $root = realpath(get_stylesheet_directory_uri());
        if ( 0 === strpos($path, $root) ) {
            return true;
        }
    }

get_stylesheet_directory_uri() should return a URI, and I’m pretty sure that realpath() is intended to work with filepaths only, so $root is always null, and now PHP 7.3 is complaining. This looks like a bug. If I switch that to get_stylesheet_directory() instead it works as expected.

Expected behavior

Resized image.

Actual behavior

Twig_Error_Runtime exception thrown.

Steps to reproduce behavior

Add to Twig template:

<img src="{{ post.thumbnail.src|resize(1200, 300) }}">

Upgrade to PHP 7.3.

What version of WordPress, PHP and Timber are you using?

WordPress 5.0.3, PHP 7.3, Timber 1.8.4

How did you install Timber? (for example, from GitHub, Composer/Packagist, WP.org?)

Composer

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 2
  • Comments: 17 (3 by maintainers)

Commits related to this issue

Most upvoted comments

@albertonieto this is already merged to master so you can try to see if it’s correct.

That PR does not look right at all. chr() returns a single character based on an ascii code, but that’s not what you want to strpos search into.

I think one of the reasons strpos gives a warning here, at least for me, is that get_stylesheet_directory_uri() returns an absolute URL (starting with https:// and all) and realpath() returns false because it obviously can’t parse URLs, and so strpos gets a boolean $needle parameter and that causes this warning.