• 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

77.36
/src/Latte/Compiler/ExpressionBuilder.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\Compiler;
9

10
use Latte\Compiler\Nodes\Php\ArgumentNode;
11
use Latte\Compiler\Nodes\Php\Expression;
12
use Latte\Compiler\Nodes\Php\ExpressionNode;
13
use Latte\Compiler\Nodes\Php\IdentifierNode;
14
use Latte\Compiler\Nodes\Php\NameNode;
15
use Latte\Compiler\Nodes\Php\Scalar;
16
use function is_array, is_bool, is_float, is_int, is_string;
17

18

19
/** @deprecated */
20
final class ExpressionBuilder
21
{
22
        public function __construct(
1✔
23
                private ExpressionNode|NameNode $expr,
24
        ) {
25
        }
1✔
26

27

28
        public static function variable(string $name): self
1✔
29
        {
30
                return new self(new Expression\VariableNode(ltrim($name, '$')));
1✔
31
        }
32

33

34
        public static function class(string $name): self
1✔
35
        {
36
                return new self(new NameNode($name));
1✔
37
        }
38

39

40
        public static function function(ExpressionNode|self|string $name, array $args = []): self
1✔
41
        {
42
                $name = is_string($name)
1✔
43
                        ? new NameNode($name)
1✔
44
                        : ($name instanceof self ? $name->expr : $name);
1✔
45
                return new self(new Expression\FunctionCallNode($name, self::arrayToArgs($args)));
1✔
46
        }
47

48

49
        public function property(ExpressionNode|self|string $name): self
1✔
50
        {
51
                $name = is_string($name)
1✔
52
                        ? new IdentifierNode($name)
1✔
53
                        : ($name instanceof self ? $name->expr : $name);
×
54
                return new self(new Expression\PropertyFetchNode($this->expr, $name));
1✔
55
        }
56

57

58
        public function method(ExpressionNode|self|string $name, array $args = []): self
1✔
59
        {
60
                $name = is_string($name)
1✔
61
                        ? new IdentifierNode($name)
1✔
62
                        : ($name instanceof self ? $name->expr : $name);
×
63
                return new self(new Expression\MethodCallNode($this->expr, $name, self::arrayToArgs($args)));
1✔
64
        }
65

66

67
        public function staticMethod(ExpressionNode|self|string $name, array $args = []): self
1✔
68
        {
69
                $name = is_string($name)
1✔
70
                        ? new IdentifierNode($name)
1✔
71
                        : ($name instanceof self ? $name->expr : $name);
×
72
                return new self(new Expression\StaticMethodCallNode($this->expr, $name, self::arrayToArgs($args)));
1✔
73
        }
74

75

76
        public function build(): ExpressionNode|NameNode
77
        {
78
                return $this->expr;
1✔
79
        }
80

81

82
        public static function valueToNode(bool|int|float|string|array|null|ExpressionNode $value): ExpressionNode
1✔
83
        {
84
                return match (true) {
1✔
85
                        $value === null => new Scalar\NullNode,
×
86
                        is_bool($value) => new Scalar\BooleanNode($value),
1✔
87
                        is_int($value) => new Scalar\IntegerNode($value),
1✔
88
                        is_float($value) => new Scalar\FloatNode($value),
1✔
89
                        is_string($value) => new Scalar\StringNode($value),
1✔
90
                        is_array($value) => self::arrayToNode($value),
1✔
91
                        default => $value, // ExpressionNode
1✔
92
                };
93
        }
94

95

96
        private static function arrayToNode(array $arr): Expression\ArrayNode
97
        {
98
                $node = new Expression\ArrayNode;
×
99
                $lastKey = -1;
×
100
                foreach ($arr as $key => $val) {
×
101
                        if ($lastKey !== null && ++$lastKey === $key) {
×
102
                                $node->items[] = new Nodes\Php\ArrayItemNode(self::valueToNode($val));
×
103
                        } else {
104
                                $lastKey = null;
×
105
                                $node->items[] = new Nodes\Php\ArrayItemNode(self::valueToNode($val), self::valueToNode($key));
×
106
                        }
107
                }
108

109
                return $node;
×
110
        }
111

112

113
        /** @return ArgumentNode[] */
114
        private static function arrayToArgs(array $arr): array
1✔
115
        {
116
                $args = [];
1✔
117
                foreach ($arr as $key => $arg) {
1✔
118
                        $args[] = $arg instanceof ArgumentNode
1✔
119
                                ? $arg
1✔
120
                                : new ArgumentNode(
1✔
121
                                        self::valueToNode($arg),
1✔
122
                                        name: is_string($key) ? new IdentifierNode($key) : null,
1✔
123
                                );
124
                }
125

126
                return $args;
1✔
127
        }
128
}
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