diff --git a/doc/internals.rst b/doc/internals.rst index 97661411d..07ff85534 100644 --- a/doc/internals.rst +++ b/doc/internals.rst @@ -124,7 +124,7 @@ using):: /* Hello {{ name }} */ class __TwigTemplate_1121b6f109fe93ebe8c6e22e3712bceb extends Template { - protected function doDisplay(array $context, array $blocks = []) + protected function doDisplay(array $context, array $blocks = []): iterable { $macros = $this->macros; // line 1 diff --git a/src/Node/BlockNode.php b/src/Node/BlockNode.php index d2cfc3bd8..2ee74a8d2 100644 --- a/src/Node/BlockNode.php +++ b/src/Node/BlockNode.php @@ -32,7 +32,10 @@ class BlockNode extends Node { $compiler ->addDebugInfo($this) - ->write(\sprintf("public function block_%s(\$context, array \$blocks = [])\n", $this->getAttribute('name')), "{\n") + ->write("/**\n") + ->write(" * @return iterable\n") + ->write(" */\n") + ->write(\sprintf("public function block_%s(array \$context, array \$blocks = []): iterable\n", $this->getAttribute('name')), "{\n") ->indent() ->write("\$macros = \$this->macros;\n") ; diff --git a/src/Node/ModuleNode.php b/src/Node/ModuleNode.php index deb05a16f..264a0e67f 100644 --- a/src/Node/ModuleNode.php +++ b/src/Node/ModuleNode.php @@ -318,7 +318,7 @@ final class ModuleNode extends Node protected function compileDisplay(Compiler $compiler) { $compiler - ->write("protected function doDisplay(array \$context, array \$blocks = [])\n", "{\n") + ->write("protected function doDisplay(array \$context, array \$blocks = []): iterable\n", "{\n") ->indent() ->write("\$macros = \$this->macros;\n") ->subcompile($this->getNode('display_start')) diff --git a/src/Template.php b/src/Template.php index 653462db6..ec9db9bf6 100644 --- a/src/Template.php +++ b/src/Template.php @@ -74,7 +74,7 @@ abstract class Template * * @return self|TemplateWrapper|false The parent template or false if there is no parent */ - public function getParent(array $context) + public function getParent(array $context): self|TemplateWrapper|false { if (null !== $this->parent) { return $this->parent; @@ -122,7 +122,7 @@ abstract class Template * @param array $context The context * @param array $blocks The current set of blocks */ - public function displayParentBlock($name, array $context, array $blocks = []) + public function displayParentBlock($name, array $context, array $blocks = []): void { foreach ($this->yieldParentBlock($name, $context, $blocks) as $data) { echo $data; @@ -140,7 +140,7 @@ abstract class Template * @param array $blocks The current set of blocks * @param bool $useBlocks Whether to use the current set of blocks */ - public function displayBlock($name, array $context, array $blocks = [], $useBlocks = true, ?self $templateContext = null) + public function displayBlock($name, array $context, array $blocks = [], $useBlocks = true, ?self $templateContext = null): void { foreach ($this->yieldBlock($name, $context, $blocks, $useBlocks, $templateContext) as $data) { echo $data; @@ -159,7 +159,7 @@ abstract class Template * * @return string The rendered block */ - public function renderParentBlock($name, array $context, array $blocks = []) + public function renderParentBlock($name, array $context, array $blocks = []): string { if (!$this->useYield) { if ($this->env->isDebug()) { @@ -193,7 +193,7 @@ abstract class Template * * @return string The rendered block */ - public function renderBlock($name, array $context, array $blocks = [], $useBlocks = true) + public function renderBlock($name, array $context, array $blocks = [], $useBlocks = true): string { if (!$this->useYield) { $level = ob_get_level(); @@ -235,7 +235,7 @@ abstract class Template * * @return bool true if the block exists, false otherwise */ - public function hasBlock($name, array $context, array $blocks = []) + public function hasBlock($name, array $context, array $blocks = []): bool { if (isset($blocks[$name])) { return $blocks[$name][0] instanceof self; @@ -261,9 +261,9 @@ abstract class Template * @param array $context The context * @param array $blocks The current set of blocks * - * @return array An array of block names + * @return array An array of block names */ - public function getBlockNames(array $context, array $blocks = []) + public function getBlockNames(array $context, array $blocks = []): array { $names = array_merge(array_keys($blocks), array_keys($this->blocks)); @@ -276,10 +276,8 @@ abstract class Template /** * @param string|TemplateWrapper|array $template - * - * @return self|TemplateWrapper */ - protected function loadTemplate($template, $templateName = null, $line = null, $index = null) + protected function loadTemplate($template, $templateName = null, $line = null, $index = null): self|TemplateWrapper { try { if (\is_array($template)) { @@ -327,10 +325,8 @@ abstract class Template /** * @internal - * - * @return self */ - public function unwrap() + public function unwrap(): self { return $this; } @@ -343,7 +339,7 @@ abstract class Template * * @return array An array of blocks */ - public function getBlocks() + public function getBlocks(): array { return $this->blocks; } @@ -418,7 +414,7 @@ abstract class Template /** * @return iterable */ - public function yieldBlock($name, array $context, array $blocks = [], $useBlocks = true, ?self $templateContext = null) + public function yieldBlock($name, array $context, array $blocks = [], $useBlocks = true, ?self $templateContext = null): iterable { if ($useBlocks && isset($blocks[$name])) { $template = $blocks[$name][0]; @@ -478,7 +474,7 @@ abstract class Template * * @return iterable */ - public function yieldParentBlock($name, array $context, array $blocks = []) + public function yieldParentBlock($name, array $context, array $blocks = []): iterable { if (isset($this->traits[$name])) { yield from $this->traits[$name][0]->yieldBlock($name, $context, $blocks, false); @@ -494,6 +490,8 @@ abstract class Template * * @param array $context An array of parameters to pass to the template * @param array $blocks An array of blocks to pass to the template + + * @return iterable */ - abstract protected function doDisplay(array $context, array $blocks = []); + abstract protected function doDisplay(array $context, array $blocks = []): iterable; } diff --git a/tests/Node/BlockTest.php b/tests/Node/BlockTest.php index 0938e74a1..02de54b4a 100644 --- a/tests/Node/BlockTest.php +++ b/tests/Node/BlockTest.php @@ -33,7 +33,10 @@ class BlockTest extends NodeTestCase $tests = []; $tests[] = [new BlockNode('foo', new TextNode('foo', 1), 1), << + */ +public function block_foo(array \$context, array \$blocks = []): iterable { \$macros = \$this->macros; yield "foo"; diff --git a/tests/Node/ModuleTest.php b/tests/Node/ModuleTest.php index f3081dff9..0b7dd5ddb 100644 --- a/tests/Node/ModuleTest.php +++ b/tests/Node/ModuleTest.php @@ -98,7 +98,7 @@ class __TwigTemplate_%x extends Template ]; } - protected function doDisplay(array \$context, array \$blocks = []) + protected function doDisplay(array \$context, array \$blocks = []): iterable { \$macros = \$this->macros; // line 1 @@ -178,7 +178,7 @@ class __TwigTemplate_%x extends Template return "layout.twig"; } - protected function doDisplay(array \$context, array \$blocks = []) + protected function doDisplay(array \$context, array \$blocks = []): iterable { \$macros = \$this->macros; // line 2 @@ -273,7 +273,7 @@ class __TwigTemplate_%x extends Template return \$this->loadTemplate(((true) ? ("foo") : ("foo")), "foo.twig", 2); } - protected function doDisplay(array \$context, array \$blocks = []) + protected function doDisplay(array \$context, array \$blocks = []): iterable { \$macros = \$this->macros; // line 4 diff --git a/tests/TemplateTest.php b/tests/TemplateTest.php index 884226e0a..2756c5774 100644 --- a/tests/TemplateTest.php +++ b/tests/TemplateTest.php @@ -464,7 +464,7 @@ class TemplateForTest extends Template return false; } - protected function doDisplay(array $context, array $blocks = []) + protected function doDisplay(array $context, array $blocks = []): iterable { }