GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

JsCompiler::leaveScope()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 7
nc 2
nop 0
1
<?php
2
3
/*
4
 * Copyright 2011 Johannes M. Schmitt <[email protected]>
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace TwigJs;
20
21
use TwigJs\Compiler\MacroCompiler;
22
use TwigJs\Compiler\Test\SameAsCompiler;
23
use TwigJs\Compiler\Test\OddCompiler;
24
use TwigJs\Compiler\Test\NullCompiler;
25
use TwigJs\Compiler\Test\NoneCompiler;
26
use TwigJs\Compiler\Test\EvenCompiler;
27
use TwigJs\Compiler\Test\EmptyCompiler;
28
use TwigJs\Compiler\Test\DivisibleByCompiler;
29
use TwigJs\Compiler\Test\DefinedCompiler;
30
use TwigJs\Compiler\ImportCompiler;
31
use TwigJs\Compiler\AutoEscapeCompiler;
32
use TwigJs\Compiler\Expression\TempNameCompiler;
33
use TwigJs\Compiler\SetTempCompiler;
34
use TwigJs\Compiler\ExtensionReferenceCompiler;
35
use TwigJs\Compiler\BlockReferenceCompiler;
36
use TwigJs\Compiler\Expression\DefaultFilterCompiler;
37
use TwigJs\Compiler\BodyCompiler;
38
use TwigJs\Compiler\SetCompiler;
39
use TwigJs\Compiler\SpacelessCompiler;
40
use TwigJs\Compiler\IncludeCompiler;
41
use TwigJs\Compiler\Expression\ExtensionReferenceCompiler as ExpressionExtensionReferenceCompiler;
42
use TwigJs\Compiler\Expression\ConditionalCompiler;
43
use TwigJs\Compiler\Expression\ArrayCompiler;
44
use TwigJs\Compiler\Expression\FunctionCompiler;
45
use TwigJs\Compiler\Expression\ParentCompiler;
46
use TwigJs\Compiler\BlockCompiler;
47
use TwigJs\Compiler\Expression\BlockReferenceCompiler as ExpressionBlockReferenceCompiler;
48
use TwigJs\Compiler\Expression\Binary\SubCompiler;
49
use TwigJs\Compiler\Expression\Binary\RangeCompiler;
50
use TwigJs\Compiler\Expression\Binary\PowerCompiler;
51
use TwigJs\Compiler\Expression\Binary\OrCompiler;
52
use TwigJs\Compiler\Expression\Binary\NotInCompiler;
53
use TwigJs\Compiler\Expression\Binary\NotEqualCompiler;
54
use TwigJs\Compiler\Expression\Binary\MulCompiler;
55
use TwigJs\Compiler\Expression\Binary\ModCompiler;
56
use TwigJs\Compiler\Expression\Binary\LessEqualCompiler;
57
use TwigJs\Compiler\Expression\Binary\LessCompiler;
58
use TwigJs\Compiler\Expression\Binary\InCompiler;
59
use TwigJs\Compiler\Expression\Binary\GreaterEqualCompiler;
60
use TwigJs\Compiler\Expression\Binary\GreaterCompiler;
61
use TwigJs\Compiler\Expression\Binary\FloorDivCompiler;
62
use TwigJs\Compiler\Expression\Binary\EqualCompiler;
63
use TwigJs\Compiler\Expression\Binary\DivCompiler;
64
use TwigJs\Compiler\Expression\Binary\ConcatCompiler;
65
use TwigJs\Compiler\Expression\Binary\BitwiseXorCompiler;
66
use TwigJs\Compiler\Expression\Binary\BitwiseOrCompiler;
67
use TwigJs\Compiler\Expression\Binary\BitwiseAndCompiler;
68
use TwigJs\Compiler\Expression\Binary\AndCompiler;
69
use TwigJs\Compiler\Expression\Binary\AddCompiler;
70
use TwigJs\Compiler\Expression\Unary\PosCompiler;
71
use TwigJs\Compiler\Expression\Unary\NotCompiler;
72
use TwigJs\Compiler\Expression\Unary\NegCompiler;
73
use TwigJs\Compiler\Expression\GetAttrCompiler;
74
use TwigJs\Compiler\Expression\ConstantCompiler;
75
use TwigJs\Compiler\Expression\AssignNameCompiler;
76
use TwigJs\Compiler\ForCompiler;
77
use TwigJs\Compiler\ForLoopCompiler;
78
use TwigJs\Compiler\Expression\FilterCompiler;
79
use TwigJs\Compiler\PrintCompiler;
80
use TwigJs\Compiler\Expression\NameCompiler;
81
use TwigJs\Compiler\Expression\TestCompiler;
82
use TwigJs\Compiler\IfCompiler;
83
use TwigJs\Compiler\TextCompiler;
84
use TwigJs\Compiler\NodeCompiler;
85
use TwigJs\Compiler\ModuleCompiler;
86
87
class JsCompiler extends \Twig_Compiler
88
{
89
    /** Whether the current expression is a template name */
90
    public $isTemplateName = false;
91
92
    /** The function name of the current template */
93
    public $templateFunctionName;
94
95
    /** Map for local variables */
96
    public $localVarMap = array();
97
98
    private $defines = array();
99
100
    private $scopes = array();
101
    private $scopeVariables = array();
102
    private $functionNamingStrategy;
103
104
    private $typeCompilers;
105
    private $filterCompilers;
106
    private $testCompilers;
107
108
    private $filterFunctions;
109
    private $functionMap;
110
111
    public function __construct(\Twig_Environment $env)
112
    {
113
        parent::__construct($env);
114
115
        $this->typeCompilers = array(
116
            'Twig_Node' => new NodeCompiler(),
117
            'Twig_Node_Body' => new BodyCompiler(),
118
            'Twig_Node_Module' => new ModuleCompiler\GoogleCompiler(),
119
            'Twig_Node_Block' => new BlockCompiler(),
120
            'Twig_Node_Text' => new TextCompiler(),
121
            'Twig_Node_If' => new IfCompiler(),
122
            'Twig_Node_Print' => new PrintCompiler(),
123
            'Twig_Node_For' => new ForCompiler(),
124
            'Twig_Node_ForLoop' => new ForLoopCompiler(),
125
            'Twig_Node_Set' => new SetCompiler(),
126
            'Twig_Node_Include' => new IncludeCompiler(),
127
            'Twig_Node_Spaceless' => new SpacelessCompiler(),
128
            'Twig_Node_SetTemp' => new SetTempCompiler(),
129
            'Twig_Node_ExtensionReference' => new ExtensionReferenceCompiler(),
130
            'Twig_Node_BlockReference' => new BlockReferenceCompiler(),
131
            'Twig_Node_AutoEscape' => new AutoEscapeCompiler(),
132
            'Twig_Node_Import' => new ImportCompiler(),
133
            'Twig_Node_Macro' => new MacroCompiler(),
134
            'Twig_Node_Expression_TempName' => new TempNameCompiler(),
135
            'Twig_Node_Expression_DefaultFilter' => new DefaultFilterCompiler(),
136
            'Twig_Node_Expression_ExtensionReference' => new ExpressionExtensionReferenceCompiler(),
137
            'Twig_Node_Expression_Conditional' => new ConditionalCompiler(),
138
            'Twig_Node_Expression_Array' => new ArrayCompiler(),
139
            'Twig_Node_Expression_Function' => new FunctionCompiler(),
140
            'Twig_Node_Expression_Parent' => new ParentCompiler(),
141
            'Twig_Node_Expression_BlockReference' => new ExpressionBlockReferenceCompiler(),
142
            'Twig_Node_Expression_AssignName' => new AssignNameCompiler(),
143
            'Twig_Node_Expression_Test' => new TestCompiler(),
144
            'Twig_Node_Expression_Name' => new NameCompiler(),
145
            'Twig_Node_Expression_Filter' => new FilterCompiler(),
146
            'Twig_Node_Expression_Filter_Default' => new Compiler\Expression\Filter\DefaultCompiler(),
147
            'Twig_Node_Expression_Constant' => new ConstantCompiler(),
148
            'Twig_Node_Expression_GetAttr' => new GetAttrCompiler(),
149
            'Twig_Node_Expression_MacroCall' => new Compiler\Expression\MacroCallCompiler(),
150
            'Twig_Node_Expression_MethodCall' => new Compiler\Expression\MethodCallCompiler(),
151
            'Twig_Node_Expression_Binary_Add' => new AddCompiler(),
152
            'Twig_Node_Expression_Binary_And' => new AndCompiler(),
153
            'Twig_Node_Expression_Binary_BitwiseAnd' => new BitwiseAndCompiler(),
154
            'Twig_Node_Expression_Binary_BitwiseOr' => new BitwiseOrCompiler(),
155
            'Twig_Node_Expression_Binary_BitwiseXor' => new BitwiseXorCompiler(),
156
            'Twig_Node_Expression_Binary_Concat' => new ConcatCompiler(),
157
            'Twig_Node_Expression_Binary_Div' => new DivCompiler(),
158
            'Twig_Node_Expression_Binary_Equal' => new EqualCompiler(),
159
            'Twig_Node_Expression_Binary_FloorDiv' => new FloorDivCompiler(),
160
            'Twig_Node_Expression_Binary_Greater' => new GreaterCompiler(),
161
            'Twig_Node_Expression_Binary_GreaterEqual' => new GreaterEqualCompiler(),
162
            'Twig_Node_Expression_Binary_In' => new InCompiler(),
163
            'Twig_Node_Expression_Binary_Less' => new LessCompiler(),
164
            'Twig_Node_Expression_Binary_LessEqual' => new LessEqualCompiler(),
165
            'Twig_Node_Expression_Binary_Mod' => new ModCompiler(),
166
            'Twig_Node_Expression_Binary_Mul' => new MulCompiler(),
167
            'Twig_Node_Expression_Binary_NotEqual' => new NotEqualCompiler(),
168
            'Twig_Node_Expression_Binary_NotIn' => new NotInCompiler(),
169
            'Twig_Node_Expression_Binary_Or' => new OrCompiler(),
170
            'Twig_Node_Expression_Binary_Power' => new PowerCompiler(),
171
            'Twig_Node_Expression_Binary_Range' => new RangeCompiler(),
172
            'Twig_Node_Expression_Binary_Sub' => new SubCompiler(),
173
            'Twig_Node_Expression_Unary_Neg' => new NegCompiler(),
174
            'Twig_Node_Expression_Unary_Not' => new NotCompiler(),
175
            'Twig_Node_Expression_Unary_Pos' => new PosCompiler(),
176
            'Twig_Node_Expression_Test_Constant' => new Compiler\Expression\Test\ConstantCompiler(),
177
            'Twig_Node_Expression_Test_Defined' => new Compiler\Expression\Test\DefinedCompiler(),
178
            'Twig_Node_Expression_Test_Divisibleby' => new Compiler\Expression\Test\DivisiblebyCompiler(),
179
            'Twig_Node_Expression_Test_Even' => new Compiler\Expression\Test\EvenCompiler(),
180
            'Twig_Node_Expression_Test_Null' => new Compiler\Expression\Test\NullCompiler(),
181
            'Twig_Node_Expression_Test_Odd' => new Compiler\Expression\Test\OddCompiler(),
182
            'Twig_Node_Expression_Test_Sameas' => new Compiler\Expression\Test\SameasCompiler(),
183
184
        );
185
186
        $this->testCompilers = array(
187
            'defined' => new DefinedCompiler(),
188
            'divisibleby' => new DivisibleByCompiler(),
189
            'empty' => new EmptyCompiler(),
190
            'even' => new EvenCompiler(),
191
            'none' => new NoneCompiler(),
192
            'null' => new NullCompiler(),
193
            'odd' => new OddCompiler(),
194
            'sameas' => new SameAsCompiler(),
195
        );
196
197
        $this->filterCompilers = array();
198
        $this->filterFunctions = array(
199
            '_default'    => 'twig.filter.def',
200
            'abs'         => 'twig.filter.abs',
201
            'batch'       => 'twig.filter.batch',
202
            'capitalize'  => 'twig.filter.capitalize',
203
            'default'     => 'twig.filter.def',
204
            'e'           => 'twig.filter.escape',
205
            'escape'      => 'twig.filter.escape',
206
            'first'       => 'twig.filter.first',
207
            'join'        => 'twig.filter.join',
208
            'json_encode' => 'twig.filter.json_encode',
209
            'keys'        => 'twig.filter.keys',
210
            'last'        => 'twig.filter.last',
211
            'length'      => 'twig.filter.length',
212
            'lower'       => 'twig.filter.lower',
213
            'merge'       => 'twig.filter.merge',
214
            'nl2br'       => 'twig.filter.nl2br',
215
            'replace'     => 'twig.filter.replace',
216
            'reverse'     => 'twig.filter.reverse',
217
            'title'       => 'twig.filter.title',
218
            'trim'        => 'twig.filter.trim',
219
            'upper'       => 'twig.filter.upper',
220
            'url_encode'  => 'encodeURIComponent',
221
        );
222
223
        $this->functionMap = array(
224
            'max'    => 'twig.functions.max',
225
            'min'    => 'twig.functions.min',
226
            'random' => 'twig.functions.random',
227
            'range'  => 'twig.range',
228
        );
229
    }
230
231
    public function setDefines(array $defines)
232
    {
233
        $this->defines = $defines;
234
    }
235
236
    public function setDefine($key, $value)
237
    {
238
        $this->defines[$key] = $value;
239
    }
240
241
    public function getDefine($key)
242
    {
243
        return isset($this->defines[$key]) ? $this->defines[$key] : null;
244
    }
245
246
    public function setFunctionNamingStrategy(FunctionNamingStrategyInterface $strategy)
247
    {
248
        $this->functionNamingStrategy = $strategy;
249
    }
250
251
    /**
252
     * Returns the function name for the given template name.
253
     *
254
     * @param  \Twig_Node_Module $templateName
0 ignored issues
show
Bug introduced by
There is no parameter named $templateName. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
255
     * @return string
256
     */
257
    final public function getFunctionName(\Twig_Node_Module $module)
258
    {
259
        if (null === $this->functionNamingStrategy) {
260
            $this->functionNamingStrategy = new DefaultFunctionNamingStrategy();
261
        }
262
263
        return $this->functionNamingStrategy->getFunctionName($module);
264
    }
265
266
    public function setTypeCompilers(array $compilers)
267
    {
268
        $this->typeCompilers = $compilers;
269
    }
270
271
    public function addTypeCompiler(TypeCompilerInterface $compiler)
272
    {
273
        $this->typeCompilers[$compiler->getType()] = $compiler;
274
    }
275
276
    public function getTestCompiler($name)
277
    {
278
        return isset($this->testCompilers[$name]) ?
279
            $this->testCompilers[$name] : null;
280
    }
281
282
    public function addTestCompiler(TestCompilerInterface $compiler)
283
    {
284
        $this->testCompilers[$compiler->getName()] = $compiler;
285
    }
286
287
    public function getFilterFunction($name)
288
    {
289
        return isset($this->filterFunctions[$name]) ?
290
            $this->filterFunctions[$name] : null;
291
    }
292
293
    public function setFilterFunction($filterName, $functionName)
294
    {
295
        $this->filterFunctions[$filterName] = $functionName;
296
    }
297
298
    public function getFilterCompiler($name)
299
    {
300
        return isset($this->filterCompilers[$name]) ?
301
            $this->filterCompilers[$name] : null;
302
    }
303
304
    public function addFilterCompiler(FilterCompilerInterface $compiler)
305
    {
306
        $this->filterCompilers[$compiler->getName()] = $compiler;
307
    }
308
309
    public function setJsFunction($twigFunctionName, $jsFunctionName)
310
    {
311
        $this->functionMap[$twigFunctionName] = $jsFunctionName;
312
    }
313
314
    public function getJsFunction($twigFunctionName)
315
    {
316
        return isset($this->functionMap[$twigFunctionName]) ?
317
            $this->functionMap[$twigFunctionName] : null;
318
    }
319
320
    public function compile(\Twig_NodeInterface $node, $indentation = 0)
321
    {
322
        $this->lastLine = null;
323
        $this->source = '';
324
        $this->sourceOffset = 0;
325
        $this->sourceLine = 0;
326
        $this->indentation = $indentation;
327
328
        $this->subcompile($node);
329
330
        return $this;
331
    }
332
333
    public function subcompile(\Twig_NodeInterface $node, $raw = true)
334
    {
335
        if ($node instanceof \Twig_Profiler_Node_EnterProfile || $node instanceof \Twig_Profiler_Node_LeaveProfile) {
336
            return $this;
337
        }
338
339
        if (false === $raw) {
340
            $this->addIndentation();
0 ignored issues
show
Deprecated Code introduced by
The method Twig_Compiler::addIndentation() has been deprecated with message: since 1.27 (to be removed in 2.0).

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
341
        }
342
343
        $nodeClass = get_class($node);
344
345
        if (!isset($this->typeCompilers[$nodeClass])) {
346
            throw new \RuntimeException(sprintf('There is no compiler for node type "%s".', $nodeClass));
347
        }
348
349
        $this->typeCompilers[$nodeClass]->compile($this, $node);
350
351
        return $this;
352
    }
353
354
    public function enterScope()
355
    {
356
        $this->scopes[] = $this->scopeVariables;
357
        $this->scopeVariables = array();
358
359
        return $this;
360
    }
361
362
    public function leaveScope()
363
    {
364
        if (false === $lastScope = array_pop($this->scopes)) {
365
            throw new \RuntimeException('leaveScope() must be called only after enterScope.');
366
        }
367
368
        $this->localVarMap = array_diff_key($this->localVarMap, $this->scopeVariables);
369
        $this->scopeVariables = $lastScope;
370
        $this->localVarMap = array_merge($this->localVarMap, $this->scopeVariables);
371
372
        return $this;
373
    }
374
375
    public function setVar($var, $localName)
376
    {
377
        $this->localVarMap[$var] =
378
        $this->scopeVariables[$var] = $localName;
379
380
        return $this;
381
    }
382
383
    public function unsetVar($var)
384
    {
385
        unset($this->localVarMap[$var]);
386
387
        return $this;
388
    }
389
390
    public function setTemplateName($bool)
391
    {
392
        $this->isTemplateName = (Boolean) $bool;
393
394
        return $this;
395
    }
396
397
    public function string($value)
398
    {
399
        return $this->repr($value);
400
    }
401
402
    public function repr($value)
403
    {
404
        $this->source .= json_encode($value);
405
406
        return $this;
407
    }
408
}
409