|
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 ArrayCompiler implements TypeCompilerInterface |
|
25
|
|
|
{ |
|
26
|
|
|
public function getType() |
|
27
|
|
|
{ |
|
28
|
|
|
return 'Twig_Node_Expression_Array'; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) |
|
32
|
|
|
{ |
|
33
|
|
|
if (!$node instanceof \Twig_Node_Expression_Array) { |
|
34
|
|
|
throw new \RuntimeException( |
|
35
|
|
|
sprintf('$node must be an instanceof of \Expression_Array, but got "%s".', get_class($node)) |
|
36
|
|
|
); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
$pairs = $this->getKeyValuePairs($node); |
|
41
|
|
|
|
|
42
|
|
|
if ($isList = $this->isList($pairs)) { |
|
43
|
|
|
$compiler->raw('['); |
|
44
|
|
|
} elseif ($hasDynamicKeys = $this->hasDynamicKeys($pairs)) { |
|
45
|
|
|
$compiler->raw('twig.createObj('); |
|
46
|
|
|
} else { |
|
47
|
|
|
$compiler->raw('{'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$first = true; |
|
51
|
|
|
foreach ($pairs as $pair) { |
|
52
|
|
|
if (!$first) { |
|
53
|
|
|
$compiler->raw(', '); |
|
54
|
|
|
} |
|
55
|
|
|
$first = false; |
|
56
|
|
|
|
|
57
|
|
|
if ($isList) { |
|
58
|
|
|
$compiler->subcompile($pair['value']); |
|
59
|
|
|
} elseif ($hasDynamicKeys) { |
|
|
|
|
|
|
60
|
|
|
$compiler |
|
61
|
|
|
->subcompile($pair['key']) |
|
62
|
|
|
->raw(', ') |
|
63
|
|
|
->subcompile($pair['value']) |
|
64
|
|
|
; |
|
65
|
|
|
} else { |
|
66
|
|
|
$compiler |
|
67
|
|
|
->subcompile($pair['key']) |
|
68
|
|
|
->raw(': ') |
|
69
|
|
|
->subcompile($pair['value']) |
|
70
|
|
|
; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if ($isList) { |
|
75
|
|
|
$compiler->raw(']'); |
|
76
|
|
|
} elseif ($hasDynamicKeys) { |
|
77
|
|
|
$compiler->raw(')'); |
|
78
|
|
|
} else { |
|
79
|
|
|
$compiler->raw('}'); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
private function hasDynamicKeys(array $pairs) |
|
84
|
|
|
{ |
|
85
|
|
|
foreach ($pairs as $pair) { |
|
86
|
|
|
if (!$pair['key'] instanceof \Twig_Node_Expression_Constant) { |
|
87
|
|
|
return true; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return false; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
private function isList(array $pairs) |
|
95
|
|
|
{ |
|
96
|
|
|
for ($i=0,$c=count($pairs); $i<$c; $i++) { |
|
97
|
|
|
if (!$pairs[$i]['key'] instanceof \Twig_Node_Expression_Constant) { |
|
98
|
|
|
return false; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
if ($pairs[$i]['key']->getAttribute('value') !== $i) { |
|
102
|
|
|
return false; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return true; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
private function getKeyValuePairs(\Twig_Node $node) |
|
110
|
|
|
{ |
|
111
|
|
|
$pairs = array(); |
|
112
|
|
|
|
|
113
|
|
|
foreach (array_chunk($node->getIterator()->getArrayCopy(), 2) as $pair) { |
|
114
|
|
|
$pairs[] = array( |
|
115
|
|
|
'key' => $pair[0], |
|
116
|
|
|
'value' => $pair[1], |
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $pairs; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: