StrictNode   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 20
c 2
b 1
f 0
dl 0
loc 31
rs 10
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A compile() 0 24 1
1
<?php
2
/**
3
 * @author Gerard van Helden <[email protected]>
4
 * @copyright Zicht Online <http://zicht.nl>
5
 */
6
7
namespace Zicht\Bundle\FrameworkExtraBundle\Twig\ControlStructures;
8
9
use Twig_Compiler;
10
11
/**
12
 * A 'strict' node wraps the contents within a 'true' or 'false' setting for the strict_variables option.
13
 *
14
 * When set to false, the contents' variable references that don't exist will NOT throw errors.
15
 * When set to true, the contents' variable references that don't exist WILL throw errors.
16
 *
17
 * After the node ends, the original value the block started with (whether it was true or false) is restored.
18
 */
19
class StrictNode extends \Twig_Node
20
{
21
    /**
22
     * Compile
23
     *
24
     * @param Twig_Compiler $compiler
25
     */
26
    public function compile(Twig_Compiler $compiler)
27
    {
28
        $compiler
29
            ->addDebugInfo($this)
30
            ->write('$restoreStrict = $this->env->isStrictVariables();' . PHP_EOL)
31
            ->write('$setStrict = (bool)')
32
            ->subcompile($this->nodes['expr'])
33
            ->write(';');
34
35
        $compiler
36
            ->write('if ($setStrict) { ' . PHP_EOL)
37
            ->write('    $this->env->enableStrictVariables();' . PHP_EOL)
38
            ->write('} else {' . PHP_EOL)
39
            ->write('    $this->env->disableStrictVariables();' . PHP_EOL)
40
            ->write('}');
41
42
        $compiler->subcompile($this->nodes['body']);
43
44
        $compiler
45
            ->write('if ($restoreStrict) { ' . PHP_EOL)
46
            ->write('    $this->env->enableStrictVariables();' . PHP_EOL)
47
            ->write('} else {' . PHP_EOL)
48
            ->write('    $this->env->disableStrictVariables();' . PHP_EOL)
49
            ->write('}');
50
    }
51
}
52