From 09a43a9f6fff2fc703e62e3555d10df0e5beffae Mon Sep 17 00:00:00 2001 From: Ruud Kamphuis Date: Tue, 3 Sep 2024 09:54:27 +0200 Subject: [PATCH] Replace `return; yield` with `yield from []` This has the same effect, but looks less hacky and makes PHPStan happy. https://phpstan.org/r/df7fcc88-1df8-428e-b675-5dc6965c34d6 Previously this was needed to work properly with output capturing. --- src/Node/BlockNode.php | 2 +- src/Node/CaptureNode.php | 2 +- src/Node/ModuleNode.php | 2 +- tests/Node/BlockTest.php | 2 +- tests/Node/MacroTest.php | 4 ++-- tests/Node/ModuleTest.php | 2 +- tests/Node/SetTest.php | 4 ++-- 7 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Node/BlockNode.php b/src/Node/BlockNode.php index 2ee74a8d2..3c06f155b 100644 --- a/src/Node/BlockNode.php +++ b/src/Node/BlockNode.php @@ -42,7 +42,7 @@ class BlockNode extends Node $compiler ->subcompile($this->getNode('body')) - ->write("return; yield '';\n") // needed when body doesn't yield anything + ->write("yield from [];\n") ->outdent() ->write("}\n\n") ; diff --git a/src/Node/CaptureNode.php b/src/Node/CaptureNode.php index 0162113c1..3b7f0b6d8 100644 --- a/src/Node/CaptureNode.php +++ b/src/Node/CaptureNode.php @@ -39,7 +39,7 @@ class CaptureNode extends Node ->raw("(function () use (&\$context, \$macros, \$blocks) {\n") ->indent() ->subcompile($this->getNode('body')) - ->write("return; yield '';\n") + ->write("yield from [];\n") ->outdent() ->write('})()') ; diff --git a/src/Node/ModuleNode.php b/src/Node/ModuleNode.php index 264a0e67f..b57e643e8 100644 --- a/src/Node/ModuleNode.php +++ b/src/Node/ModuleNode.php @@ -353,7 +353,7 @@ final class ModuleNode extends Node $compiler->subcompile($this->getNode('display_end')); if (!$this->hasNode('parent')) { - $compiler->write("return; yield '';\n"); // ensure at least one yield call even for templates with no output + $compiler->write("yield from [];\n"); } $compiler diff --git a/tests/Node/BlockTest.php b/tests/Node/BlockTest.php index 02de54b4a..06405d1c3 100644 --- a/tests/Node/BlockTest.php +++ b/tests/Node/BlockTest.php @@ -40,7 +40,7 @@ public function block_foo(array \$context, array \$blocks = []): iterable { \$macros = \$this->macros; yield "foo"; - return; yield ''; + yield from []; } EOF , new Environment(new ArrayLoader()), diff --git a/tests/Node/MacroTest.php b/tests/Node/MacroTest.php index f05adf636..405046b37 100644 --- a/tests/Node/MacroTest.php +++ b/tests/Node/MacroTest.php @@ -59,7 +59,7 @@ public function macro_foo(\$__foo__ = null, \$__bar__ = "Foo", ...\$__varargs__) return ('' === \$tmp = implode('', iterator_to_array((function () use (&\$context, \$macros, \$blocks) { yield "foo"; - return; yield ''; + yield from []; })(), false))) ? '' : new Markup(\$tmp, \$this->env->getCharset()); } EOF @@ -81,7 +81,7 @@ public function macro_foo(\$__foo__ = null, \$__bar__ = "Foo", ...\$__varargs__) return ('' === \$tmp = \\Twig\\Extension\\CoreExtension::captureOutput((function () use (&\$context, \$macros, \$blocks) { yield "foo"; - return; yield ''; + yield from []; })())) ? '' : new Markup(\$tmp, \$this->env->getCharset()); } EOF diff --git a/tests/Node/ModuleTest.php b/tests/Node/ModuleTest.php index 0b7dd5ddb..10c4d7fa5 100644 --- a/tests/Node/ModuleTest.php +++ b/tests/Node/ModuleTest.php @@ -103,7 +103,7 @@ class __TwigTemplate_%x extends Template \$macros = \$this->macros; // line 1 yield "foo"; - return; yield ''; + yield from []; } /** diff --git a/tests/Node/SetTest.php b/tests/Node/SetTest.php index f250b80ee..9bb93a367 100644 --- a/tests/Node/SetTest.php +++ b/tests/Node/SetTest.php @@ -57,7 +57,7 @@ EOF // line 1 \$context["foo"] = ('' === \$tmp = implode('', iterator_to_array((function () use (&\$context, \$macros, \$blocks) { yield "foo"; - return; yield ''; + yield from []; })(), false))) ? '' : new Markup(\$tmp, \$this->env->getCharset()); EOF , new Environment(new ArrayLoader()), @@ -67,7 +67,7 @@ EOF // line 1 $context["foo"] = ('' === $tmp = \Twig\Extension\CoreExtension::captureOutput((function () use (&$context, $macros, $blocks) { yield "foo"; - return; yield ''; + yield from []; })())) ? '' : new Markup($tmp, $this->env->getCharset()); EOF , new Environment(new ArrayLoader()),