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 ( b43f4a...009a79 )
by Mewes
10:25
created

SyntaxCheckNodeVisitor   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
lcom 1
cbo 4
dl 0
loc 109
ccs 39
cts 39
cp 1
rs 10
c 1
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 49
    public function getPriority()
22
    {
23 49
        return 0;
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     *
29
     * @throws \Twig_Error_Syntax
30
     */
31 49
    protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env)
32
    {
33
        try {
34 49
            if ($node instanceof BaseNode) {
35 40
                $this->checkAllowedParents($node);
36
            } else {
37 49
                $this->checkAllowedChildren($node);
38
            }
39 16
        } catch (\Twig_Error_Syntax $e) {
40
            // reset path since throwing an error prevents doLeaveNode to be called
41 16
            $this->path = [];
42 16
            throw $e;
43
        }
44
45 49
        $this->path[] = $node !== null ? get_class($node) : null;
46
47 49
        return $node;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 41
    protected function doLeaveNode(\Twig_Node $node, \ Twig_Environment $env)
54
    {
55 41
        array_pop($this->path);
56
57 41
        return $node;
58
    }
59
60
    /**
61
     * @param \Twig_Node $node
62
     *
63
     * @throws \Twig_Error_Syntax
64
     */
65 49
    private function checkAllowedChildren(\Twig_Node $node)
66
    {
67 49
        $hasDocumentNode = false;
68 49
        $hasTextNode = false;
69
70
        /**
71
         * @var \Twig_Node $currentNode
72
         */
73 49
        foreach ($node->getIterator() as $currentNode) {
74 49
            if ($currentNode instanceof \Twig_Node_Text) {
75 43
                if ($hasDocumentNode) {
76 4
                    throw new \Twig_Error_Syntax(sprintf('Node "%s" is not allowed after Node "%s".', \Twig_Node_Text::class, DocumentNode::class));
77
                }
78 39
                $hasTextNode = true;
79 49
            } elseif ($currentNode instanceof DocumentNode) {
80 41
                if ($hasTextNode) {
81 4
                    throw new \Twig_Error_Syntax(sprintf('Node "%s" is not allowed before Node "%s".', \Twig_Node_Text::class, DocumentNode::class));
82
                }
83 49
                $hasDocumentNode = true;
84
            }
85
        }
86 49
    }
87
88
    /**
89
     * @param BaseNode $node
90
     *
91
     * @throws \Twig_Error_Syntax
92
     */
93 40
    private function checkAllowedParents(BaseNode $node)
94
    {
95 40
        $parentName = null;
96
97
        // find first parent from this bundle
98 40
        foreach (array_reverse($this->path) as $className) {
99 40
            if (strpos($className, 'MewesK\\TwigSpreadsheetBundle\\Twig\\Node\\') === 0) {
100 37
                $parentName = $className;
101 40
                break;
102
            }
103
        }
104
105
        // allow no parents (e.g. macros, includes)
106 40
        if ($parentName === null) {
107 40
            return;
108
        }
109
110
        // check if parent is allowed
111 37
        foreach ($node->getAllowedParents() as $className) {
112 33
            if ($className === $parentName) {
113 33
                return;
114
            }
115
        }
116
117 8
        throw new \Twig_Error_Syntax(sprintf('Node "%s" is not allowed inside of Node "%s".', get_class($node), $parentName));
118
    }
119
}
120