GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 0529a5...06bc6c )
by Mewes
02:43
created

SyntaxCheckNodeVisitor   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 4
dl 0
loc 120
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A doLeaveNode() 0 6 1
A getPriority() 0 4 1
A doEnterNode() 0 18 4
C checkAllowedChildren() 0 33 8
B checkAllowedParents() 0 26 6
1
<?php
2
3
namespace MewesK\TwigSpreadsheetBundle\Twig\NodeVisitor;
4
5
use MewesK\TwigSpreadsheetBundle\Twig\Node\BaseNode;
6
use MewesK\TwigSpreadsheetBundle\Twig\Node\DocumentNode;
7
8
/**
9
 * Class SyntaxCheckNodeVisitor.
10
 */
11
class SyntaxCheckNodeVisitor extends \Twig_BaseNodeVisitor
12
{
13
    /**
14
     * @var array
15
     */
16
    protected $path = [];
17
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function getPriority()
22
    {
23
        return 0;
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     *
29
     * @throws \Twig_Error_Syntax
30
     */
31
    protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
32
    {
33
        try {
34
            if ($node instanceof BaseNode) {
35
                $this->checkAllowedParents($node);
36
            } else {
37
                $this->checkAllowedChildren($node);
38
            }
39
        } catch (\Twig_Error_Syntax $e) {
40
            // reset path since throwing an error prevents doLeaveNode to be called
41
            $this->path = [];
42
            throw $e;
43
        }
44
45
        $this->path[] = $node !== null ? get_class($node) : null;
46
47
        return $node;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    protected function doLeaveNode(\Twig_Node $node, \ Twig_Environment $env)
54
    {
55
        array_pop($this->path);
56
57
        return $node;
58
    }
59
60
    /**
61
     * @param \Twig_Node $node
62
     *
63
     * @throws \Twig_Error_Syntax
64
     */
65
    private function checkAllowedChildren(\Twig_Node $node)
66
    {
67
        $hasDocumentNode = false;
68
        $hasTextNodeBefore = false;
69
        $hasTextNodeAfter = false;
70
71
        /**
72
         * @var \Twig_Node $currentNode
73
         */
74
        foreach ($node->getIterator() as $currentNode) {
75
            if ($currentNode instanceof \Twig_Node_Text) {
76
                if ($hasDocumentNode) {
77
                    $hasTextNodeAfter = true;
78
                } else {
79
                    $hasTextNodeBefore = true;
80
                }
81
            }
82
83
            if ($currentNode instanceof DocumentNode) {
84
                $hasDocumentNode = true;
85
            }
86
        }
87
88
        if ($hasDocumentNode) {
89
            if ($hasTextNodeBefore) {
90
                throw new \Twig_Error_Syntax(sprintf('Node "%s" is not allowed before Node "%s".', \Twig_Node_Text::class, DocumentNode::class));
91
            }
92
93
            if ($hasTextNodeAfter) {
94
                throw new \Twig_Error_Syntax(sprintf('Node "%s" is not allowed after Node "%s".', \Twig_Node_Text::class, DocumentNode::class));
95
            }
96
        }
97
    }
98
99
    /**
100
     * @param BaseNode $node
101
     *
102
     * @throws \Twig_Error_Syntax
103
     */
104
    private function checkAllowedParents(BaseNode $node)
105
    {
106
        $parentName = null;
107
108
        // find first parent from this bundle
109
        foreach (array_reverse($this->path) as $className) {
110
            if (strpos($className, 'MewesK\\TwigSpreadsheetBundle\\Twig\\Node\\') === 0) {
111
                $parentName = $className;
112
                break;
113
            }
114
        }
115
116
        // allow no parents (e.g. macros, includes)
117
        if ($parentName === null) {
118
            return;
119
        }
120
121
        // check if parent is allowed
122
        foreach ($node->getAllowedParents() as $className) {
123
            if ($className === $parentName) {
124
                return;
125
            }
126
        }
127
128
        throw new \Twig_Error_Syntax(sprintf('Node "%s" is not allowed inside of Node "%s".', get_class($node), $parentName));
129
    }
130
}
131