Passed
Push — master ( 371449...88c28f )
by Maurício
09:28
created

libraries/classes/Twig/I18n/NodeTrans.php (2 issues)

1
<?php
2
/* vim: set expandtab sw=4 ts=4 sts=4: */
3
/**
4
 * hold PhpMyAdmin\Twig\I18n\NodeTrans class
5
 *
6
 * @package PhpMyAdmin\Twig\I18n
7
 */
8
declare(strict_types=1);
9
10
namespace PhpMyAdmin\Twig\I18n;
11
12
use Twig\Compiler;
13
use Twig\Extensions\Node\TransNode;
14
use Twig\Node\Expression\AbstractExpression;
15
use Twig\Node\Node;
16
17
/**
18
 * Class NodeTrans
19
 *
20
 * @package PhpMyAdmin\Twig\I18n
21
 */
22
class NodeTrans extends TransNode
23
{
24
    /**
25
     * Constructor.
26
     *
27
     * The nodes are automatically made available as properties ($this->node).
28
     * The attributes are automatically made available as array items ($this['name']).
29
     *
30
     * @param Node               $body    Body of node trans
31
     * @param Node               $plural  Node plural
32
     * @param AbstractExpression $count   Node count
33
     * @param Node               $context Node context
34
     * @param Node               $notes   Node notes
35
     * @param int                $lineno  The line number
36
     * @param string             $tag     The tag name associated with the Node
37
     */
38
    public function __construct(
39
        Node $body,
40
        Node $plural = null,
41
        AbstractExpression $count = null,
42
        Node $context = null,
43
        Node $notes = null,
44
        $lineno,
45
        $tag = null
46
    ) {
47
        $nodes = ['body' => $body];
48
        if (null !== $count) {
49
            $nodes['count'] = $count;
50
        }
51
        if (null !== $plural) {
52
            $nodes['plural'] = $plural;
53
        }
54
        if (null !== $context) {
55
            $nodes['context'] = $context;
56
        }
57
        if (null !== $notes) {
58
            $nodes['notes'] = $notes;
59
        }
60
61
        Node::__construct($nodes, [], $lineno, $tag);
62
    }
63
64
    /**
65
     * Compiles the node to PHP.
66
     *
67
     * @param Compiler $compiler Node compiler
68
     *
69
     * @return void
70
     */
71
    public function compile(Compiler $compiler)
72
    {
73
        $compiler->addDebugInfo($this);
74
75
        list($msg, $vars) = $this->compileString($this->getNode('body'));
76
77
        if ($this->hasNode('plural')) {
78
            list($msg1, $vars1) = $this->compileString($this->getNode('plural'));
79
80
            $vars = array_merge($vars, $vars1);
81
        }
82
83
        $function = $this->getTransFunction(
84
            $this->hasNode('plural'),
85
            $this->hasNode('context')
86
        );
87
88
        if ($this->hasNode('notes')) {
89
            $message = trim($this->getNode('notes')->getAttribute('data'));
90
91
            // line breaks are not allowed cause we want a single line comment
92
            $message = str_replace(["\n", "\r"], ' ', $message);
93
            $compiler->write("// l10n: {$message}\n");
94
        }
95
96
        if ($vars) {
97
            $compiler
98
                ->write('echo strtr(' . $function . '(')
99
                ->subcompile($msg)
100
            ;
101
102
            if ($this->hasNode('plural')) {
103
                $compiler
104
                    ->raw(', ')
105
                    ->subcompile($msg1)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $msg1 does not seem to be defined for all execution paths leading up to this point.
Loading history...
106
                    ->raw(', abs(')
107
                    ->subcompile($this->hasNode('count') ? $this->getNode('count') : null)
0 ignored issues
show
It seems like $this->hasNode('count') ...getNode('count') : null can also be of type null; however, parameter $node of Twig\Compiler::subcompile() does only seem to accept Twig\Node\Node, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
                    ->subcompile(/** @scrutinizer ignore-type */ $this->hasNode('count') ? $this->getNode('count') : null)
Loading history...
108
                    ->raw(')')
109
                ;
110
            }
111
112
            $compiler->raw('), array(');
113
114
            foreach ($vars as $var) {
115
                if ('count' === $var->getAttribute('name')) {
116
                    $compiler
117
                        ->string('%count%')
118
                        ->raw(' => abs(')
119
                        ->subcompile($this->hasNode('count') ? $this->getNode('count') : null)
120
                        ->raw('), ')
121
                    ;
122
                } else {
123
                    $compiler
124
                        ->string('%' . $var->getAttribute('name') . '%')
125
                        ->raw(' => ')
126
                        ->subcompile($var)
127
                        ->raw(', ')
128
                    ;
129
                }
130
            }
131
132
            $compiler->raw("));\n");
133
        } else {
134
            $compiler->write('echo ' . $function . '(');
135
136
            if ($this->hasNode('context')) {
137
                $context = trim($this->getNode('context')->getAttribute('data'));
138
                $compiler->write('"' . $context . '", ');
139
            }
140
141
            $compiler->subcompile($msg);
142
143
            if ($this->hasNode('plural')) {
144
                $compiler
145
                    ->raw(', ')
146
                    ->subcompile($msg1)
147
                    ->raw(', abs(')
148
                    ->subcompile($this->hasNode('count') ? $this->getNode('count') : null)
149
                    ->raw(')')
150
                ;
151
            }
152
153
            $compiler->raw(");\n");
154
        }
155
    }
156
157
    /**
158
     * @param bool $plural        Return plural or singular function to use
159
     * @param bool $hasMsgContext It has message context?
160
     *
161
     * @return string
162
     */
163
    protected function getTransFunction($plural, $hasMsgContext = false)
164
    {
165
        if ($hasMsgContext) {
166
            return $plural ? '_ngettext' : '_pgettext';
167
        }
168
169
        return $plural ? '_ngettext' : '_gettext';
170
    }
171
}
172