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

nette / latte / 22359082277

24 Feb 2026 04:03PM UTC coverage: 93.959% (+0.05%) from 93.907%
22359082277

push

github

dg
fixed operator ! priority

5273 of 5612 relevant lines covered (93.96%)

0.94 hits per line

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

96.88
/src/Latte/Essential/Nodes/NElseNode.php
1
<?php declare(strict_types=1);
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
namespace Latte\Essential\Nodes;
9

10
use Latte\CompileException;
11
use Latte\Compiler\Node;
12
use Latte\Compiler\Nodes;
13
use Latte\Compiler\Nodes\AreaNode;
14
use Latte\Compiler\Nodes\StatementNode;
15
use Latte\Compiler\NodeTraverser;
16
use Latte\Compiler\PrintContext;
17
use Latte\Compiler\Tag;
18
use function array_splice, count, trim;
19

20

21
/**
22
 * n:else
23
 */
24
final class NElseNode extends StatementNode
25
{
26
        public AreaNode $content;
27

28

29
        /** @return \Generator<int, ?array, array{AreaNode, ?Tag}, static> */
30
        public static function create(Tag $tag): \Generator
1✔
31
        {
32
                $node = $tag->node = new static;
1✔
33
                [$node->content] = yield;
1✔
34
                return $node;
1✔
35
        }
36

37

38
        public function print(PrintContext $context): string
39
        {
40
                throw new \LogicException('Cannot directly print');
×
41
        }
42

43

44
        public function &getIterator(): \Generator
1✔
45
        {
46
                yield $this->content;
1✔
47
        }
1✔
48

49

50
        public static function processPass(Node $node): void
1✔
51
        {
52
                (new NodeTraverser)->traverse($node, function (Node $node) {
1✔
53
                        if ($node instanceof Nodes\FragmentNode) {
1✔
54
                                for ($i = count($node->children) - 1; $i >= 0; $i--) {
1✔
55
                                        $nElse = $node->children[$i];
1✔
56
                                        if (!$nElse instanceof self) {
1✔
57
                                                continue;
1✔
58
                                        }
59

60
                                        array_splice($node->children, $i, 1);
1✔
61
                                        $prev = $node->children[--$i] ?? null;
1✔
62
                                        while ($prev instanceof Nodes\TextNode && trim($prev->content) === '') {
1✔
63
                                                array_splice($node->children, $i, 1);
1✔
64
                                                $prev = $node->children[--$i] ?? null;
1✔
65
                                        }
66

67
                                        if (
68
                                                $prev instanceof IfNode
1✔
69
                                                || $prev instanceof ForeachNode
1✔
70
                                                || $prev instanceof TryNode
1✔
71
                                                || $prev instanceof IfChangedNode
1✔
72
                                                || $prev instanceof IfContentNode
1✔
73
                                        ) {
74
                                                if ($prev->else) {
1✔
75
                                                        throw new CompileException('Multiple "else" found.', $nElse->position);
1✔
76
                                                }
77
                                                $prev->else = $nElse->content;
1✔
78
                                        } else {
79
                                                throw new CompileException('n:else must be immediately after n:if, n:foreach etc', $nElse->position);
1✔
80
                                        }
81
                                }
82
                        } elseif ($node instanceof self) {
1✔
83
                                throw new CompileException('n:else must be immediately after n:if, n:foreach etc', $node->position);
84
                        }
85
                });
1✔
86
        }
1✔
87
}
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