Conditions | 4 |
Paths | 4 |
Total Lines | 35 |
Code Lines | 26 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
1 | <?php |
||
27 | public function compile(\Twig_Compiler $compiler) |
||
28 | { |
||
29 | $compiler->addDebugInfo($this); |
||
30 | $compiler |
||
31 | ->write('switch (') |
||
32 | ->subcompile($this->getNode('value')) |
||
33 | ->raw(") {\n") |
||
34 | ->indent |
||
35 | ; |
||
36 | for ($i = 0; $i < count($this->getNode('cases')); $i += 2) { |
||
|
|||
37 | $compiler |
||
38 | ->write('case ') |
||
39 | ->subcompile($this->getNode('cases')->getNode($i)) |
||
40 | ->raw(":\n") |
||
41 | ->indent() |
||
42 | ->subcompile($this->getNode('cases')->getNode($i + 1)) |
||
43 | ->addIndentation() |
||
44 | ->raw("break;\n") |
||
45 | ; |
||
46 | } |
||
47 | |||
48 | if ($this->hasNode('default') && null !== $this->getNode('default')) { |
||
49 | $compiler |
||
50 | ->write("default:\n") |
||
51 | ->indent() |
||
52 | ->subcompile($this->getNode('default')) |
||
53 | ->addIndentation() |
||
54 | ->raw("break;\n") |
||
55 | ; |
||
56 | } |
||
57 | |||
58 | $compiler |
||
59 | ->outdent() |
||
60 | ->write("}\n"); |
||
61 | } |
||
62 | } |
||
63 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: