From e4f6bf8e8a60622a877f7ea5b401befc23b7f893 Mon Sep 17 00:00:00 2001 From: Ruud Kamphuis Date: Thu, 17 Oct 2024 12:28:35 +0200 Subject: [PATCH] Call For recurseFunc inline After https://github.com/phpstan/phpstan-src/pull/3554 this now makes it easier for PHPStan to understands what's going on. This will allow TwigStan to handle for loops in v4. --- src/Node/ForNode.php | 5 ++--- tests/Node/ForTest.php | 25 ++++++++++--------------- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/src/Node/ForNode.php b/src/Node/ForNode.php index c9383761f..8aa5b5407 100644 --- a/src/Node/ForNode.php +++ b/src/Node/ForNode.php @@ -49,7 +49,7 @@ class ForNode extends Node ->write("\$$iteratorVar = new \Twig\Runtime\LoopIterator(") ->subcompile($this->getNode('seq')) ->raw(");\n") - ->write("\$$functionVar = function (\$iterator, &\$context, \$blocks, \$recurseFunc, \$depth) {\n") + ->write("yield from (\$$functionVar = function (\$iterator, &\$context, \$blocks, \$recurseFunc, \$depth) {\n") ->indent() ->write("\$macros = \$this->macros;\n") ->write("\$parent = \$context;\n") @@ -94,8 +94,7 @@ class ForNode extends Node $compiler ->write("yield from [];\n") ->outdent() - ->write("};\n") - ->write("yield from \$$functionVar(\$$iteratorVar, \$context, \$blocks, \$$functionVar, 0);\n") + ->write("})(\$$iteratorVar, \$context, \$blocks, \$$functionVar, 0);\n") ; } } diff --git a/tests/Node/ForTest.php b/tests/Node/ForTest.php index 407d51b73..b1d2b87ee 100644 --- a/tests/Node/ForTest.php +++ b/tests/Node/ForTest.php @@ -68,7 +68,7 @@ class ForTest extends NodeTestCase $tests[] = [$node, <<macros; \$parent = \$context; foreach (\$iterator as \$context["key"] => \$context["item"]) { @@ -77,8 +77,7 @@ class ForTest extends NodeTestCase unset(\$context['key'], \$context['item']); \$context = array_intersect_key(\$context, \$parent) + \$parent; yield from []; -}; -yield from \$__internal_compile_1(\$__internal_compile_0, \$context, \$blocks, \$__internal_compile_1, 0); +})(\$__internal_compile_0, \$context, \$blocks, \$__internal_compile_1, 0); EOF ]; @@ -93,7 +92,7 @@ EOF $tests[] = [$node, <<macros; \$parent = \$context; \$context['loop'] = new \Twig\Runtime\LoopContext(\$iterator, \$parent, \$blocks, \$recurseFunc, \$depth); @@ -103,8 +102,7 @@ EOF unset(\$context['k'], \$context['v'], \$context['loop']); \$context = array_intersect_key(\$context, \$parent) + \$parent; yield from []; -}; -yield from \$__internal_compile_1(\$__internal_compile_0, \$context, \$blocks, \$__internal_compile_1, 0); +})(\$__internal_compile_0, \$context, \$blocks, \$__internal_compile_1, 0); EOF ]; @@ -119,7 +117,7 @@ EOF $tests[] = [$node, <<macros; \$parent = \$context; \$context['loop'] = new \Twig\Runtime\LoopContext(\$iterator, \$parent, \$blocks, \$recurseFunc, \$depth); @@ -129,8 +127,7 @@ EOF unset(\$context['k'], \$context['v'], \$context['loop']); \$context = array_intersect_key(\$context, \$parent) + \$parent; yield from []; -}; -yield from \$__internal_compile_1(\$__internal_compile_0, \$context, \$blocks, \$__internal_compile_1, 0); +})(\$__internal_compile_0, \$context, \$blocks, \$__internal_compile_1, 0); EOF ]; @@ -146,7 +143,7 @@ EOF $tests[] = [$node, <<macros; \$parent = \$context; \$context['loop'] = new \Twig\Runtime\LoopContext(\$iterator, \$parent, \$blocks, \$recurseFunc, \$depth); @@ -161,8 +158,7 @@ EOF unset(\$context['k'], \$context['v'], \$context['loop']); \$context = array_intersect_key(\$context, \$parent) + \$parent; yield from []; -}; -yield from \$__internal_compile_1(\$__internal_compile_0, \$context, \$blocks, \$__internal_compile_1, 0); +})(\$__internal_compile_0, \$context, \$blocks, \$__internal_compile_1, 0); EOF ]; @@ -173,7 +169,7 @@ EOF $tests[] = [$node, <<macros; \$parent = \$context; \$context['loop'] = new \Twig\Runtime\LoopContext(\$iterator, \$parent, \$blocks, \$recurseFunc, \$depth); @@ -183,8 +179,7 @@ EOF unset(\$context['_key'], \$context['item'], \$context['loop']); \$context = array_intersect_key(\$context, \$parent) + \$parent; yield from []; -}; -yield from \$__internal_compile_1(\$__internal_compile_0, \$context, \$blocks, \$__internal_compile_1, 0); +})(\$__internal_compile_0, \$context, \$blocks, \$__internal_compile_1, 0); EOF , $env];