• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nette / latte / 19653161791

24 Nov 2025 11:49PM UTC coverage: 94.744% (+0.02%) from 94.725%
19653161791

push

github

dg
x

5408 of 5708 relevant lines covered (94.74%)

0.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.08
/src/Latte/Essential/Nodes/NElseNode.php
1
<?php
2

3
/**
4
 * This file is part of the Latte (https://latte.nette.org)
5
 * Copyright (c) 2008 David Grudl (https://davidgrudl.com)
6
 */
7

8
declare(strict_types=1);
9

10
namespace Latte\Essential\Nodes;
11

12
use Latte\CompileException;
13
use Latte\Compiler\Node;
14
use Latte\Compiler\Nodes;
15
use Latte\Compiler\Nodes\AreaNode;
16
use Latte\Compiler\Nodes\Php\ExpressionNode;
17
use Latte\Compiler\Nodes\StatementNode;
18
use Latte\Compiler\NodeTraverser;
19
use Latte\Compiler\PrintContext;
20
use Latte\Compiler\Tag;
21

22

23
/**
24
 * n:else & n:elseif
25
 */
26
final class NElseNode extends StatementNode
27
{
28
        public AreaNode $content;
29
        public ?ExpressionNode $condition = null;
30

31

32
        /** @return \Generator<int, ?array, array{AreaNode, ?Tag}, static> */
33
        public static function create(Tag $tag): \Generator
1✔
34
        {
35
                $node = $tag->node = new static;
1✔
36
                if ($tag->name === 'elseif') {
1✔
37
                        $tag->expectArguments();
1✔
38
                        $node->condition = $tag->parser->parseExpression();
1✔
39
                }
40
                [$node->content] = yield;
1✔
41
                return $node;
1✔
42
        }
43

44

45
        public function print(PrintContext $context): string
46
        {
47
                throw new \LogicException('Cannot directly print');
×
48
        }
49

50

51
        public function &getIterator(): \Generator
1✔
52
        {
53
                if ($this->condition) {
1✔
54
                        yield $this->condition;
1✔
55
                }
56
                yield $this->content;
1✔
57
        }
1✔
58

59

60
        public static function processPass(Node $node): void
1✔
61
        {
62
                (new NodeTraverser)->traverse($node, function (Node $node) {
1✔
63
                        if ($node instanceof Nodes\FragmentNode) {
1✔
64
                                $node->children = self::processFragment($node->children);
1✔
65
                        } elseif ($node instanceof self) {
1✔
66
                                self::processFragment([$node]);
67
                        }
68
                });
1✔
69
        }
1✔
70

71

72
        private static function processFragment(array $children): array
1✔
73
        {
74
                $currentNode = null;
1✔
75
                for ($i = 0; isset($children[$i]); $i++) {
1✔
76
                        $child = $children[$i];
1✔
77

78
                        if ($child instanceof IfNode
1✔
79
                                || $child instanceof ForeachNode
1✔
80
                                || $child instanceof TryNode
1✔
81
                                || $child instanceof IfChangedNode
1✔
82
                                || $child instanceof IfContentNode
1✔
83
                        ) {
84
                                $currentNode = $child;
1✔
85

86
                        } elseif ($child instanceof Nodes\TextNode && trim($child->content) === '') {
1✔
87
                                continue;
1✔
88

89
                        } elseif ($child instanceof self) {
1✔
90
                                $nElse = $child;
1✔
91
                                if ($currentNode === null) {
1✔
92
                                        throw new CompileException('n:else must be immediately after n:if, n:foreach etc', $nElse->position);
1✔
93
                                } elseif ($currentNode->else) {
1✔
94
                                        throw new CompileException('Multiple "else" found.', $nElse->position);
1✔
95
                                }
96

97
                                if ($nElse->condition) {
1✔
98
                                        $elseIfNode = new IfNode;
1✔
99
                                        $elseIfNode->condition = $nElse->condition;
1✔
100
                                        $elseIfNode->then = $nElse->content;
1✔
101
                                        $elseIfNode->position = $nElse->position;
1✔
102
                                        $currentNode->else = $elseIfNode;
1✔
103
                                        $currentNode = $elseIfNode;
1✔
104
                                } else {
105
                                        $currentNode->else = $nElse->content;
1✔
106
                                        $currentNode = null;
1✔
107
                                }
108

109
                                unset($children[$i]);
1✔
110
                                for ($o = 1; ($children[$i - $o] ?? null) instanceof Nodes\TextNode; $o++) {
1✔
111
                                        unset($children[$i - $o]);
1✔
112
                                }
113
                        } else {
114
                                $currentNode = null;
1✔
115
                        }
116
                }
117

118
                return array_values($children);
1✔
119
        }
120
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc