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

nette / latte / 6835208276

11 Nov 2023 03:59PM UTC coverage: 94.02%. Remained the same
6835208276

push

github

dg
Released version 3.0.11

4968 of 5284 relevant lines covered (94.02%)

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
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\StatementNode;
17
use Latte\Compiler\NodeTraverser;
18
use Latte\Compiler\PrintContext;
19
use Latte\Compiler\Tag;
20

21

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

29

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

38

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

44

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

50

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

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

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