| Conditions | 6 |
| Paths | 7 |
| Total Lines | 40 |
| Code Lines | 26 |
| Lines | 8 |
| Ratio | 20 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 31 | public function compile(JsCompiler $compiler, \Twig_NodeInterface $node) |
||
| 32 | { |
||
| 33 | if (!$node instanceof \Twig_Node_If) { |
||
| 34 | throw new \RuntimeException(sprintf('$node must be an instanceof of \If, but got "%s".', get_class($node))); |
||
| 35 | } |
||
| 36 | |||
| 37 | $compiler->addDebugInfo($node); |
||
| 38 | for ($i = 0; $i < count($node->getNode('tests')); $i += 2) { |
||
|
|
|||
| 39 | if ($i > 0) { |
||
| 40 | $compiler |
||
| 41 | ->outdent() |
||
| 42 | ->write("} else if (") |
||
| 43 | ; |
||
| 44 | } else { |
||
| 45 | $compiler |
||
| 46 | ->write('if (') |
||
| 47 | ; |
||
| 48 | } |
||
| 49 | |||
| 50 | $compiler |
||
| 51 | ->subcompile($node->getNode('tests')->getNode($i)) |
||
| 52 | ->raw(") {\n") |
||
| 53 | ->indent() |
||
| 54 | ->subcompile($node->getNode('tests')->getNode($i + 1)) |
||
| 55 | ; |
||
| 56 | } |
||
| 57 | |||
| 58 | View Code Duplication | if ($node->hasNode('else') && null !== $node->getNode('else')) { |
|
| 59 | $compiler |
||
| 60 | ->outdent() |
||
| 61 | ->write("} else {\n") |
||
| 62 | ->indent() |
||
| 63 | ->subcompile($node->getNode('else')) |
||
| 64 | ; |
||
| 65 | } |
||
| 66 | |||
| 67 | $compiler |
||
| 68 | ->outdent() |
||
| 69 | ->write("}\n"); |
||
| 70 | } |
||
| 71 | } |
||
| 72 |
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: