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.
Passed
Push — master ( 99c051...63fac8 )
by Mewes
18:46
created

SyntaxCheckNodeVisitor   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 4
dl 0
loc 109
ccs 0
cts 61
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getPriority() 0 4 1
A doEnterNode() 0 18 4
A doLeaveNode() 0 6 1
B checkAllowedParents() 0 26 6
B checkAllowedChildren() 0 22 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
        $hasTextNode = false;
69
70
        /**
71
         * @var \Twig_Node $currentNode
72
         */
73
        foreach ($node->getIterator() as $currentNode) {
74
            if ($currentNode instanceof \Twig_Node_Text) {
75
                if ($hasDocumentNode) {
76
                    throw new \Twig_Error_Syntax(sprintf('Node "%s" is not allowed after Node "%s".', \Twig_Node_Text::class, DocumentNode::class));
77
                }
78
                $hasTextNode = true;
79
            } elseif ($currentNode instanceof DocumentNode) {
80
                if ($hasTextNode) {
81
                    throw new \Twig_Error_Syntax(sprintf('Node "%s" is not allowed before Node "%s".', \Twig_Node_Text::class, DocumentNode::class));
82
                }
83
                $hasDocumentNode = true;
84
            }
85
        }
86
    }
87
88
    /**
89
     * @param BaseNode $node
90
     *
91
     * @throws \Twig_Error_Syntax
92
     */
93
    private function checkAllowedParents(BaseNode $node)
94
    {
95
        $parentName = null;
96
97
        // find first parent from this bundle
98
        foreach (array_reverse($this->path) as $className) {
99
            if (strpos($className, 'MewesK\\TwigSpreadsheetBundle\\Twig\\Node\\') === 0) {
100
                $parentName = $className;
101
                break;
102
            }
103
        }
104
105
        // allow no parents (e.g. macros, includes)
106
        if ($parentName === null) {
107
            return;
108
        }
109
110
        // check if parent is allowed
111
        foreach ($node->getAllowedParents() as $className) {
112
            if ($className === $parentName) {
113
                return;
114
            }
115
        }
116
117
        throw new \Twig_Error_Syntax(sprintf('Node "%s" is not allowed inside of Node "%s".', \get_class($node), $parentName));
118
    }
119
}
120