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\Compiler\Expression; |
20
|
|
|
|
21
|
|
|
use TwigJs\JsCompiler; |
22
|
|
|
use TwigJs\TypeCompilerInterface; |
23
|
|
|
|
24
|
|
|
class NameCompiler implements TypeCompilerInterface |
25
|
|
|
{ |
26
|
|
|
public function getType() |
27
|
|
|
{ |
28
|
|
|
return 'Twig_Node_Expression_Name'; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) |
32
|
|
|
{ |
33
|
|
|
if (!$node instanceof \Twig_Node_Expression_Name) { |
34
|
|
|
throw new \RuntimeException( |
35
|
|
|
sprintf( |
36
|
|
|
'$node must be an instanceof of \Expression_Name, but got "%s".', |
37
|
|
|
get_class($node) |
38
|
|
|
) |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$name = $node->getAttribute('name'); |
43
|
|
|
|
44
|
|
|
if ($node->getAttribute('is_defined_test')) { |
45
|
|
|
if ($node->isSpecial()) { |
46
|
|
|
$compiler->repr(true); |
47
|
|
|
} else { |
48
|
|
|
$compiler->raw('(')->repr($name)->raw(' in context)'); |
49
|
|
|
} |
50
|
|
|
} elseif ($node->isSpecial()) { |
51
|
|
|
static $specialVars = array( |
52
|
|
|
'_self' => 'this', |
53
|
|
|
'_context' => 'context', |
54
|
|
|
'_charset' => 'this.env_.getCharset()', |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
if (!isset($specialVars[$name])) { |
58
|
|
|
throw new \RuntimeException( |
59
|
|
|
sprintf( |
60
|
|
|
'The special var "%s" is not supported by the NameCompiler.', |
61
|
|
|
$name |
62
|
|
|
) |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$compiler->raw($specialVars[$name]); |
67
|
|
|
} else { |
68
|
|
|
if (isset($compiler->localVarMap[$name])) { |
69
|
|
|
$compiler->raw($compiler->localVarMap[$name]); |
70
|
|
|
|
71
|
|
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// FIXME: Add strict behavior? |
75
|
|
|
// see Template::getContext() |
|
|
|
|
76
|
|
|
$compiler |
77
|
|
|
->raw('(') |
78
|
|
|
->string($name) |
79
|
|
|
->raw(' in context ? context[') |
80
|
|
|
->string($name) |
81
|
|
|
->raw('] : null)') |
82
|
|
|
; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.