Twig icon indicating copy to clipboard operation
Twig copied to clipboard

fix: add missing initializer for sourceOffset

Open shyim opened this issue 2 years ago • 2 comments

When I create the compiler by own (test case) and call as first addDebugInfo it throws a php deprecation

$compiler = new Compiler(new Environment(new ArrayLoader()));

$compiler
            ->addDebugInfo($myNode);

PHP Deprecated: substr_count(): Passing null to parameter #3 ($offset) of type int is deprecated in /Users/shyim/Code/sw6/vendor/twig/twig/src/Compiler.php on line 166

That variable is only set by compile, but already used by addDebugInfo 🤔

shyim avatar Aug 16 '22 07:08 shyim

Why are you calling addDebugInfo outside of compiling nodes?

fabpot avatar Aug 16 '22 08:08 fabpot

It was inside just to show it easier I removed the code around 😅

The problem is only the compile of the Compiler class sets that variables

$token = new FeatureCallSilentToken(...);

$compiler = new Compiler(new Environment(new ArrayLoader()));

$token->compile($compiler);

but my actual compile in the Node does not use compile from the Compiler it just does

public function compile(Compiler $compiler): void
    {
        $compiler
            ->addDebugInfo($this)
            ->raw(...)
            ->subcompile($this->getNode('body'))
            ->raw(...);
    }

shyim avatar Aug 16 '22 08:08 shyim

What about https://github.com/twigphp/Twig/pull/3788 as an alternative? That way, you will be able to call reset whenever you see fit and never use compiile() directly (which is now just a shortcut).

fabpot avatar Dec 26 '22 16:12 fabpot