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

nette / latte / 16317387179

16 Jul 2025 10:46AM UTC coverage: 93.699% (-0.1%) from 93.84%
16317387179

push

github

dg
added Helpers::stringOrNull() [Closes #401]

6 of 6 new or added lines in 2 files covered. (100.0%)

57 existing lines in 19 files now uncovered.

5160 of 5507 relevant lines covered (93.7%)

0.94 hits per line

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

96.55
/src/Latte/Essential/Nodes/DefineNode.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\Compiler\Block;
13
use Latte\Compiler\Nodes\AreaNode;
14
use Latte\Compiler\Nodes\Php\Expression\AssignNode;
15
use Latte\Compiler\Nodes\Php\Expression\VariableNode;
16
use Latte\Compiler\Nodes\Php\ParameterNode;
17
use Latte\Compiler\Nodes\Php\Scalar;
18
use Latte\Compiler\Nodes\StatementNode;
19
use Latte\Compiler\PrintContext;
20
use Latte\Compiler\Tag;
21
use Latte\Compiler\TemplateParser;
22
use Latte\Compiler\Token;
23
use Latte\Runtime\Template;
24
use function is_string;
25

26

27
/**
28
 * {define [local] name}
29
 */
30
class DefineNode extends StatementNode
31
{
32
        public Block $block;
33
        public AreaNode $content;
34

35

36
        /** @return \Generator<int, ?array, array{AreaNode, ?Tag}, static> */
37
        public static function create(Tag $tag, TemplateParser $parser): \Generator
1✔
38
        {
39
                $tag->expectArguments();
1✔
40
                $layer = $tag->parser->tryConsumeTokenBeforeUnquotedString('local')
1✔
41
                        ? Template::LayerLocal
1✔
42
                        : $parser->blockLayer;
1✔
43
                $tag->parser->stream->tryConsume('#');
1✔
44
                $name = $tag->parser->parseUnquotedStringOrExpression();
1✔
45

46
                $node = $tag->node = new static;
1✔
47
                $node->block = new Block($name, $layer, $tag);
1✔
48
                if (!$node->block->isDynamic()) {
1✔
49
                        $parser->checkBlockIsUnique($node->block);
1✔
50
                        $tag->parser->stream->tryConsume(',');
1✔
51
                        $node->block->parameters = self::parseParameters($tag);
1✔
52
                }
53

54
                [$node->content, $endTag] = yield;
1✔
55
                if ($endTag && $name instanceof Scalar\StringNode) {
1✔
56
                        $endTag->parser->stream->tryConsume($name->value);
1✔
57
                }
58

59
                return $node;
1✔
60
        }
61

62

63
        private static function parseParameters(Tag $tag): array
1✔
64
        {
65
                $stream = $tag->parser->stream;
1✔
66
                $params = [];
1✔
67
                while (!$stream->is(Token::End)) {
1✔
68
                        $type = $tag->parser->parseType();
1✔
69

70
                        $save = $stream->getIndex();
1✔
71
                        $expr = $stream->is(Token::Php_Variable) ? $tag->parser->parseExpression() : null;
1✔
72
                        if ($expr instanceof VariableNode && is_string($expr->name)) {
1✔
73
                                $params[] = new ParameterNode($expr, new Scalar\NullNode, $type);
1✔
74
                        } elseif (
75
                                $expr instanceof AssignNode
1✔
76
                                && $expr->var instanceof VariableNode
1✔
77
                                && is_string($expr->var->name)
1✔
78
                        ) {
79
                                $params[] = new ParameterNode($expr->var, $expr->expr, $type);
1✔
80
                        } else {
UNCOV
81
                                $stream->seek($save);
×
82
                                $stream->throwUnexpectedException(addendum: ' in ' . $tag->getNotation());
×
83
                        }
84

85
                        if (!$stream->tryConsume(',')) {
1✔
86
                                break;
1✔
87
                        }
88
                }
89

90
                return $params;
1✔
91
        }
92

93

94
        public function print(PrintContext $context): string
1✔
95
        {
96
                return $this->block->isDynamic()
1✔
97
                        ? $this->printDynamic($context)
1✔
98
                        : $this->printStatic($context);
1✔
99
        }
100

101

102
        private function printStatic(PrintContext $context): string
1✔
103
        {
104
                $context->addBlock($this->block);
1✔
105
                $this->block->content = $this->content->print($context); // must be compiled after is added
1✔
106
                return '';
1✔
107
        }
108

109

110
        private function printDynamic(PrintContext $context): string
1✔
111
        {
112
                $context->addBlock($this->block);
1✔
113
                $this->block->content = $this->content->print($context); // must be compiled after is added
1✔
114

115
                return $context->format(
1✔
116
                        '$this->addBlock(%raw, %dump, [[$this, %dump]], %dump);',
1✔
117
                        $context->ensureString($this->block->name, 'Block name'),
1✔
118
                        $context->getEscaper()->export(),
1✔
119
                        $this->block->method,
1✔
120
                        $this->block->layer,
1✔
121
                );
122
        }
123

124

125
        public function &getIterator(): \Generator
1✔
126
        {
127
                yield $this->block->name;
1✔
128
                foreach ($this->block->parameters as &$param) {
1✔
129
                        yield $param;
1✔
130
                }
131

132
                yield $this->content;
1✔
133
        }
1✔
134
}
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