From 30c15112485b9fb838bbe90a6e45246143357819 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 20 Dec 2022 00:14:30 +0100 Subject: [PATCH] yield can retun null --- src/Latte/Compiler/Node.php | 1 - src/Latte/Compiler/NodeTraverser.php | 3 +++ src/Latte/Compiler/Nodes/AuxiliaryNode.php | 2 +- src/Latte/Compiler/Nodes/Html/AttributeNode.php | 4 +--- src/Latte/Compiler/Nodes/Html/ElementNode.php | 12 +++--------- src/Latte/Compiler/Nodes/NopNode.php | 2 +- src/Latte/Compiler/Nodes/Php/ArgumentNode.php | 4 +--- .../Nodes/Php/Expression/ArrayAccessNode.php | 4 +--- .../Compiler/Nodes/Php/Expression/ArrayItemNode.php | 4 +--- .../Compiler/Nodes/Php/Expression/ArrayNode.php | 4 +--- .../Compiler/Nodes/Php/Expression/ClosureNode.php | 8 ++------ .../Compiler/Nodes/Php/Expression/TernaryNode.php | 8 ++------ src/Latte/Compiler/Nodes/Php/IdentifierNode.php | 2 +- src/Latte/Compiler/Nodes/Php/NameNode.php | 2 +- src/Latte/Compiler/Nodes/Php/ParameterNode.php | 8 ++------ src/Latte/Compiler/Nodes/Php/ScalarNode.php | 2 +- src/Latte/Compiler/Nodes/Php/SuperiorTypeNode.php | 2 +- src/Latte/Compiler/Nodes/TextNode.php | 2 +- src/Latte/Essential/Nodes/ContentTypeNode.php | 2 +- src/Latte/Essential/Nodes/DebugbreakNode.php | 4 +--- src/Latte/Essential/Nodes/DumpNode.php | 4 +--- src/Latte/Essential/Nodes/FirstLastSepNode.php | 8 ++------ src/Latte/Essential/Nodes/ForNode.php | 4 +--- src/Latte/Essential/Nodes/ForeachNode.php | 8 ++------ src/Latte/Essential/Nodes/IfChangedNode.php | 4 +--- src/Latte/Essential/Nodes/IfNode.php | 4 +--- src/Latte/Essential/Nodes/IncludeBlockNode.php | 4 +--- src/Latte/Essential/Nodes/RawPhpNode.php | 2 +- src/Latte/Essential/Nodes/RollbackNode.php | 2 +- src/Latte/Essential/Nodes/SwitchNode.php | 4 +--- src/Latte/Essential/Nodes/TemplatePrintNode.php | 2 +- src/Latte/Essential/Nodes/TemplateTypeNode.php | 2 +- src/Latte/Essential/Nodes/TraceNode.php | 2 +- src/Latte/Essential/Nodes/TryNode.php | 4 +--- src/Latte/Essential/Nodes/VarPrintNode.php | 2 +- src/Latte/Essential/Nodes/VarTypeNode.php | 2 +- tests/helpers.php | 4 +++- 37 files changed, 47 insertions(+), 95 deletions(-) diff --git a/src/Latte/Compiler/Node.php b/src/Latte/Compiler/Node.php index a9be3be23..c6fd18d94 100644 --- a/src/Latte/Compiler/Node.php +++ b/src/Latte/Compiler/Node.php @@ -22,7 +22,6 @@ abstract public function print(PrintContext $context): string; public function &getIterator(): \Generator { - return; yield; } } diff --git a/src/Latte/Compiler/NodeTraverser.php b/src/Latte/Compiler/NodeTraverser.php index 492bb15b8..a3ad1dc1c 100644 --- a/src/Latte/Compiler/NodeTraverser.php +++ b/src/Latte/Compiler/NodeTraverser.php @@ -56,6 +56,9 @@ private function traverseNode(Node $node): Node if ($children) { foreach ($node as &$subnode) { + if ($subnode === null) { + continue; + } $subnode = $this->traverseNode($subnode); if ($this->stop) { break; diff --git a/src/Latte/Compiler/Nodes/AuxiliaryNode.php b/src/Latte/Compiler/Nodes/AuxiliaryNode.php index 50af4f813..06561c8e0 100644 --- a/src/Latte/Compiler/Nodes/AuxiliaryNode.php +++ b/src/Latte/Compiler/Nodes/AuxiliaryNode.php @@ -28,6 +28,6 @@ public function print(PrintContext $context): string public function &getIterator(): \Generator { - false && yield; + yield; } } diff --git a/src/Latte/Compiler/Nodes/Html/AttributeNode.php b/src/Latte/Compiler/Nodes/Html/AttributeNode.php index e8cf920b8..c578492ba 100644 --- a/src/Latte/Compiler/Nodes/Html/AttributeNode.php +++ b/src/Latte/Compiler/Nodes/Html/AttributeNode.php @@ -41,8 +41,6 @@ public function print(PrintContext $context): string public function &getIterator(): \Generator { yield $this->name; - if ($this->value) { - yield $this->value; - } + yield $this->value; } } diff --git a/src/Latte/Compiler/Nodes/Html/ElementNode.php b/src/Latte/Compiler/Nodes/Html/ElementNode.php index 848de60bf..035df35f4 100644 --- a/src/Latte/Compiler/Nodes/Html/ElementNode.php +++ b/src/Latte/Compiler/Nodes/Html/ElementNode.php @@ -116,14 +116,8 @@ private function printStartTag(PrintContext $context): string public function &getIterator(): \Generator { yield $this->tagNode; - if ($this->customName) { - yield $this->customName; - } - if ($this->attributes) { - yield $this->attributes; - } - if ($this->content) { - yield $this->content; - } + yield $this->customName; + yield $this->attributes; + yield $this->content; } } diff --git a/src/Latte/Compiler/Nodes/NopNode.php b/src/Latte/Compiler/Nodes/NopNode.php index b4b4dc1a7..9a2815345 100644 --- a/src/Latte/Compiler/Nodes/NopNode.php +++ b/src/Latte/Compiler/Nodes/NopNode.php @@ -22,6 +22,6 @@ public function print(PrintContext $context): string public function &getIterator(): \Generator { - false && yield; + yield; } } diff --git a/src/Latte/Compiler/Nodes/Php/ArgumentNode.php b/src/Latte/Compiler/Nodes/Php/ArgumentNode.php index b284a2189..4dd1b7edd 100644 --- a/src/Latte/Compiler/Nodes/Php/ArgumentNode.php +++ b/src/Latte/Compiler/Nodes/Php/ArgumentNode.php @@ -37,9 +37,7 @@ public function print(PrintContext $context): string public function &getIterator(): \Generator { - if ($this->name) { - yield $this->name; - } + yield $this->name; yield $this->value; } } diff --git a/src/Latte/Compiler/Nodes/Php/Expression/ArrayAccessNode.php b/src/Latte/Compiler/Nodes/Php/Expression/ArrayAccessNode.php index 01fdce271..fbc3e29f1 100644 --- a/src/Latte/Compiler/Nodes/Php/Expression/ArrayAccessNode.php +++ b/src/Latte/Compiler/Nodes/Php/Expression/ArrayAccessNode.php @@ -34,8 +34,6 @@ public function print(PrintContext $context): string public function &getIterator(): \Generator { yield $this->expr; - if ($this->index) { - yield $this->index; - } + yield $this->index; } } diff --git a/src/Latte/Compiler/Nodes/Php/Expression/ArrayItemNode.php b/src/Latte/Compiler/Nodes/Php/Expression/ArrayItemNode.php index d4f3c9672..a20035622 100644 --- a/src/Latte/Compiler/Nodes/Php/Expression/ArrayItemNode.php +++ b/src/Latte/Compiler/Nodes/Php/Expression/ArrayItemNode.php @@ -44,9 +44,7 @@ public function print(PrintContext $context): string public function &getIterator(): \Generator { - if ($this->key) { - yield $this->key; - } + yield $this->key; yield $this->value; } } diff --git a/src/Latte/Compiler/Nodes/Php/Expression/ArrayNode.php b/src/Latte/Compiler/Nodes/Php/Expression/ArrayNode.php index 06ecea3ac..e6f4986d2 100644 --- a/src/Latte/Compiler/Nodes/Php/Expression/ArrayNode.php +++ b/src/Latte/Compiler/Nodes/Php/Expression/ArrayNode.php @@ -86,9 +86,7 @@ public function print(PrintContext $context): string public function &getIterator(): \Generator { foreach ($this->items as &$item) { - if ($item) { - yield $item; - } + yield $item; } } } diff --git a/src/Latte/Compiler/Nodes/Php/Expression/ClosureNode.php b/src/Latte/Compiler/Nodes/Php/Expression/ClosureNode.php index 95a09f738..6afd3b269 100644 --- a/src/Latte/Compiler/Nodes/Php/Expression/ClosureNode.php +++ b/src/Latte/Compiler/Nodes/Php/Expression/ClosureNode.php @@ -63,11 +63,7 @@ public function &getIterator(): \Generator yield $item; } - if ($this->returnType) { - yield $this->returnType; - } - if ($this->expr) { - yield $this->expr; - } + yield $this->returnType; + yield $this->expr; } } diff --git a/src/Latte/Compiler/Nodes/Php/Expression/TernaryNode.php b/src/Latte/Compiler/Nodes/Php/Expression/TernaryNode.php index c8fd44987..3660f154c 100644 --- a/src/Latte/Compiler/Nodes/Php/Expression/TernaryNode.php +++ b/src/Latte/Compiler/Nodes/Php/Expression/TernaryNode.php @@ -40,11 +40,7 @@ public function print(PrintContext $context): string public function &getIterator(): \Generator { yield $this->cond; - if ($this->if) { - yield $this->if; - } - if ($this->else) { - yield $this->else; - } + yield $this->if; + yield $this->else; } } diff --git a/src/Latte/Compiler/Nodes/Php/IdentifierNode.php b/src/Latte/Compiler/Nodes/Php/IdentifierNode.php index 539fed319..75a9b924a 100644 --- a/src/Latte/Compiler/Nodes/Php/IdentifierNode.php +++ b/src/Latte/Compiler/Nodes/Php/IdentifierNode.php @@ -37,6 +37,6 @@ public function print(PrintContext $context): string public function &getIterator(): \Generator { - false && yield; + yield; } } diff --git a/src/Latte/Compiler/Nodes/Php/NameNode.php b/src/Latte/Compiler/Nodes/Php/NameNode.php index d590efb66..e81d6150b 100644 --- a/src/Latte/Compiler/Nodes/Php/NameNode.php +++ b/src/Latte/Compiler/Nodes/Php/NameNode.php @@ -90,6 +90,6 @@ public function toCodeString(): string public function &getIterator(): \Generator { - false && yield; + yield; } } diff --git a/src/Latte/Compiler/Nodes/Php/ParameterNode.php b/src/Latte/Compiler/Nodes/Php/ParameterNode.php index 54d02d440..27001f461 100644 --- a/src/Latte/Compiler/Nodes/Php/ParameterNode.php +++ b/src/Latte/Compiler/Nodes/Php/ParameterNode.php @@ -43,12 +43,8 @@ public function print(PrintContext $context): string public function &getIterator(): \Generator { - if ($this->type) { - yield $this->type; - } + yield $this->type; yield $this->var; - if ($this->default) { - yield $this->default; - } + yield $this->default; } } diff --git a/src/Latte/Compiler/Nodes/Php/ScalarNode.php b/src/Latte/Compiler/Nodes/Php/ScalarNode.php index 764cbbb0f..481423d03 100644 --- a/src/Latte/Compiler/Nodes/Php/ScalarNode.php +++ b/src/Latte/Compiler/Nodes/Php/ScalarNode.php @@ -14,6 +14,6 @@ abstract class ScalarNode extends ExpressionNode { public function &getIterator(): \Generator { - false && yield; + yield; } } diff --git a/src/Latte/Compiler/Nodes/Php/SuperiorTypeNode.php b/src/Latte/Compiler/Nodes/Php/SuperiorTypeNode.php index 61a57f50d..ddae5d008 100644 --- a/src/Latte/Compiler/Nodes/Php/SuperiorTypeNode.php +++ b/src/Latte/Compiler/Nodes/Php/SuperiorTypeNode.php @@ -30,6 +30,6 @@ public function print(PrintContext $context): string public function &getIterator(): \Generator { - false && yield; + yield; } } diff --git a/src/Latte/Compiler/Nodes/TextNode.php b/src/Latte/Compiler/Nodes/TextNode.php index 2b6f4f202..63b254842 100644 --- a/src/Latte/Compiler/Nodes/TextNode.php +++ b/src/Latte/Compiler/Nodes/TextNode.php @@ -38,6 +38,6 @@ public function isWhitespace(): bool public function &getIterator(): \Generator { - false && yield; + yield; } } diff --git a/src/Latte/Essential/Nodes/ContentTypeNode.php b/src/Latte/Essential/Nodes/ContentTypeNode.php index 594214852..9e8306ee5 100644 --- a/src/Latte/Essential/Nodes/ContentTypeNode.php +++ b/src/Latte/Essential/Nodes/ContentTypeNode.php @@ -75,6 +75,6 @@ public function print(PrintContext $context): string public function &getIterator(): \Generator { - false && yield; + yield; } } diff --git a/src/Latte/Essential/Nodes/DebugbreakNode.php b/src/Latte/Essential/Nodes/DebugbreakNode.php index 4a1ca9925..4c7499cfb 100644 --- a/src/Latte/Essential/Nodes/DebugbreakNode.php +++ b/src/Latte/Essential/Nodes/DebugbreakNode.php @@ -46,8 +46,6 @@ public function print(PrintContext $context): string public function &getIterator(): \Generator { - if ($this->condition) { - yield $this->condition; - } + yield $this->condition; } } diff --git a/src/Latte/Essential/Nodes/DumpNode.php b/src/Latte/Essential/Nodes/DumpNode.php index 049915423..c6056ce48 100644 --- a/src/Latte/Essential/Nodes/DumpNode.php +++ b/src/Latte/Essential/Nodes/DumpNode.php @@ -51,8 +51,6 @@ public function print(PrintContext $context): string public function &getIterator(): \Generator { - if ($this->expression) { - yield $this->expression; - } + yield $this->expression; } } diff --git a/src/Latte/Essential/Nodes/FirstLastSepNode.php b/src/Latte/Essential/Nodes/FirstLastSepNode.php index e2161a7a7..5be554d23 100644 --- a/src/Latte/Essential/Nodes/FirstLastSepNode.php +++ b/src/Latte/Essential/Nodes/FirstLastSepNode.php @@ -70,12 +70,8 @@ public function print(PrintContext $context): string public function &getIterator(): \Generator { - if ($this->width) { - yield $this->width; - } + yield $this->width; yield $this->then; - if ($this->else) { - yield $this->else; - } + yield $this->else; } } diff --git a/src/Latte/Essential/Nodes/ForNode.php b/src/Latte/Essential/Nodes/ForNode.php index f070704f7..8422b1896 100644 --- a/src/Latte/Essential/Nodes/ForNode.php +++ b/src/Latte/Essential/Nodes/ForNode.php @@ -78,9 +78,7 @@ public function &getIterator(): \Generator yield $item; } - if ($this->condition) { - yield $this->condition; - } + yield $this->condition; foreach ($this->next as &$item) { yield $item; diff --git a/src/Latte/Essential/Nodes/ForeachNode.php b/src/Latte/Essential/Nodes/ForeachNode.php index 338a89646..23e9403ea 100644 --- a/src/Latte/Essential/Nodes/ForeachNode.php +++ b/src/Latte/Essential/Nodes/ForeachNode.php @@ -143,13 +143,9 @@ private function printArgs(PrintContext $context): string public function &getIterator(): \Generator { yield $this->expression; - if ($this->key) { - yield $this->key; - } + yield $this->key; yield $this->value; yield $this->content; - if ($this->else) { - yield $this->else; - } + yield $this->else; } } diff --git a/src/Latte/Essential/Nodes/IfChangedNode.php b/src/Latte/Essential/Nodes/IfChangedNode.php index a5e9e94f5..b45ab67ae 100644 --- a/src/Latte/Essential/Nodes/IfChangedNode.php +++ b/src/Latte/Essential/Nodes/IfChangedNode.php @@ -134,8 +134,6 @@ public function &getIterator(): \Generator { yield $this->conditions; yield $this->then; - if ($this->else) { - yield $this->else; - } + yield $this->else; } } diff --git a/src/Latte/Essential/Nodes/IfNode.php b/src/Latte/Essential/Nodes/IfNode.php index e6b2ed022..2748080fc 100644 --- a/src/Latte/Essential/Nodes/IfNode.php +++ b/src/Latte/Essential/Nodes/IfNode.php @@ -173,8 +173,6 @@ public function &getIterator(): \Generator { yield $this->condition; yield $this->then; - if ($this->else) { - yield $this->else; - } + yield $this->else; } } diff --git a/src/Latte/Essential/Nodes/IncludeBlockNode.php b/src/Latte/Essential/Nodes/IncludeBlockNode.php index 0381688ec..06c6ffc03 100644 --- a/src/Latte/Essential/Nodes/IncludeBlockNode.php +++ b/src/Latte/Essential/Nodes/IncludeBlockNode.php @@ -131,9 +131,7 @@ private function printBlockFrom(PrintContext $context, string $modArg): string public function &getIterator(): \Generator { yield $this->name; - if ($this->from) { - yield $this->from; - } + yield $this->from; yield $this->args; yield $this->modifier; } diff --git a/src/Latte/Essential/Nodes/RawPhpNode.php b/src/Latte/Essential/Nodes/RawPhpNode.php index 4c5807b9a..181011d89 100644 --- a/src/Latte/Essential/Nodes/RawPhpNode.php +++ b/src/Latte/Essential/Nodes/RawPhpNode.php @@ -47,6 +47,6 @@ public function print(PrintContext $context): string public function &getIterator(): \Generator { - false && yield; + yield; } } diff --git a/src/Latte/Essential/Nodes/RollbackNode.php b/src/Latte/Essential/Nodes/RollbackNode.php index 36c92802f..e7aff43b0 100644 --- a/src/Latte/Essential/Nodes/RollbackNode.php +++ b/src/Latte/Essential/Nodes/RollbackNode.php @@ -38,6 +38,6 @@ public function print(PrintContext $context): string public function &getIterator(): \Generator { - false && yield; + yield; } } diff --git a/src/Latte/Essential/Nodes/SwitchNode.php b/src/Latte/Essential/Nodes/SwitchNode.php index 09bb1f5b7..d530366ee 100644 --- a/src/Latte/Essential/Nodes/SwitchNode.php +++ b/src/Latte/Essential/Nodes/SwitchNode.php @@ -109,9 +109,7 @@ public function &getIterator(): \Generator { yield $this->expression; foreach ($this->cases as [&$case, , &$stmt]) { - if ($case) { - yield $case; - } + yield $case; yield $stmt; } } diff --git a/src/Latte/Essential/Nodes/TemplatePrintNode.php b/src/Latte/Essential/Nodes/TemplatePrintNode.php index 9844c44f0..c63b8dd23 100644 --- a/src/Latte/Essential/Nodes/TemplatePrintNode.php +++ b/src/Latte/Essential/Nodes/TemplatePrintNode.php @@ -40,6 +40,6 @@ public function print(PrintContext $context): string public function &getIterator(): \Generator { - false && yield; + yield; } } diff --git a/src/Latte/Essential/Nodes/TemplateTypeNode.php b/src/Latte/Essential/Nodes/TemplateTypeNode.php index 949d4f606..56b9d9da3 100644 --- a/src/Latte/Essential/Nodes/TemplateTypeNode.php +++ b/src/Latte/Essential/Nodes/TemplateTypeNode.php @@ -39,6 +39,6 @@ public function print(PrintContext $context): string public function &getIterator(): \Generator { - false && yield; + yield; } } diff --git a/src/Latte/Essential/Nodes/TraceNode.php b/src/Latte/Essential/Nodes/TraceNode.php index f2c70f786..24c6ff808 100644 --- a/src/Latte/Essential/Nodes/TraceNode.php +++ b/src/Latte/Essential/Nodes/TraceNode.php @@ -36,6 +36,6 @@ public function print(PrintContext $context): string public function &getIterator(): \Generator { - false && yield; + yield; } } diff --git a/src/Latte/Essential/Nodes/TryNode.php b/src/Latte/Essential/Nodes/TryNode.php index f7d0f9e38..b51331860 100644 --- a/src/Latte/Essential/Nodes/TryNode.php +++ b/src/Latte/Essential/Nodes/TryNode.php @@ -68,8 +68,6 @@ public function print(PrintContext $context): string public function &getIterator(): \Generator { yield $this->try; - if ($this->else) { - yield $this->else; - } + yield $this->else; } } diff --git a/src/Latte/Essential/Nodes/VarPrintNode.php b/src/Latte/Essential/Nodes/VarPrintNode.php index 8efa80586..42d8f1899 100644 --- a/src/Latte/Essential/Nodes/VarPrintNode.php +++ b/src/Latte/Essential/Nodes/VarPrintNode.php @@ -41,6 +41,6 @@ public function print(PrintContext $context): string public function &getIterator(): \Generator { - false && yield; + yield; } } diff --git a/src/Latte/Essential/Nodes/VarTypeNode.php b/src/Latte/Essential/Nodes/VarTypeNode.php index fb21a1efc..dbd9e17fb 100644 --- a/src/Latte/Essential/Nodes/VarTypeNode.php +++ b/src/Latte/Essential/Nodes/VarTypeNode.php @@ -37,6 +37,6 @@ public function print(PrintContext $context): string public function &getIterator(): \Generator { - false && yield; + yield; } } diff --git a/tests/helpers.php b/tests/helpers.php index 26a3f973a..08ed2980c 100644 --- a/tests/helpers.php +++ b/tests/helpers.php @@ -126,7 +126,9 @@ function exportAST(Node $node) }; $res = $prop ? $prop . "\n" : ''; foreach ($node as $sub) { - $res .= rtrim(exportAST($sub), "\n") . "\n"; + if ($sub !== null) { + $res .= rtrim(exportAST($sub), "\n") . "\n"; + } } return substr($node::class, strrpos($node::class, '\\') + 1, -4)