Completed
Branch master (6cd78a)
by Mewes
04:43 queued 01:43
created

SyntaxCheckNodeVisitor::checkContainsXlsNode()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 8.8571
cc 6
eloc 8
nc 5
nop 1
1
<?php
2
3
namespace MewesK\TwigExcelBundle\Twig\NodeVisitor;
4
5
use MewesK\TwigExcelBundle\Twig\Node\XlsNode;
6
use MewesK\TwigExcelBundle\Twig\NodeHelper;
7
use Twig_BaseNodeVisitor;
8
use Twig_Environment;
9
use Twig_Error_Syntax;
10
use Twig_Node;
11
use Twig_Node_Block;
12
use Twig_Node_Macro;
13
14
/**
15
 * Class SyntaxCheckNodeVisitor
16
 *
17
 * @package MewesK\TwigExcelBundle\Twig\NodeVisitor
18
 */
19
class SyntaxCheckNodeVisitor extends Twig_BaseNodeVisitor
20
{
21
    /**
22
     * @var array
23
     */
24
    protected $path = [];
25
26
    /**
27
     * {@inheritdoc}
28
     *
29
     * @throws Twig_Error_Syntax
30
     */
31
    protected function doEnterNode(Twig_Node $node, Twig_Environment $env)
32
    {
33
        if (($node instanceof Twig_Node_Block || $node instanceof Twig_Node_Macro) && !$node->hasAttribute('twigExcelBundle') && NodeHelper::checkContainsXlsNode($node)) {
34
            if ($node instanceof Twig_Node_Block) {
35
                throw new Twig_Error_Syntax('Block tags do not work together with Twig tags provided by TwigExcelBundle. Please use \'xlsblock\' instead.');
36
            }
37
            elseif ($node instanceof Twig_Node_Macro) {
38
                throw new Twig_Error_Syntax('Macro tags do not work together with Twig tags provided by TwigExcelBundle. Please use \'xlsmacro\' instead.');
39
            }
40
        }
41
        elseif ($node instanceof XlsNode) {
42
            /**
43
             * @var XlsNode $node
44
             */
45
            try {
46
                NodeHelper::checkAllowedParents($node, $this->path);
47
            } catch(Twig_Error_Syntax $e) {
48
                // reset path since throwing an error prevents doLeaveNode to be called
49
                $this->path = [];
50
                throw $e;
51
            }
52
        }
53
54
        $this->path[] = get_class($node);
55
56
        return $node;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    protected function doLeaveNode(Twig_Node $node, Twig_Environment $env)
63
    {
64
        array_pop($this->path);
65
66
        return $node;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function getPriority()
73
    {
74
        return 0;
75
    }
76
}
77