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 ( 82ab4e...5bf5e4 )
by Mewes
06:05 queued 02:02
created

BaseTokenParser   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 93.48%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 6
dl 0
loc 158
ccs 43
cts 46
cp 0.9348
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A configureParameters() 0 4 1
A getAttributes() 0 4 1
C parseParameters() 0 50 9
A parseBody() 0 14 1
createNode() 0 1 ?
A hasBody() 0 4 1
A parse() 0 12 2
1
<?php
2
3
namespace MewesK\TwigSpreadsheetBundle\Twig\TokenParser;
4
5
/**
6
 * Class BaseTokenParser.
7
 */
8
abstract class BaseTokenParser extends \Twig_TokenParser
9
{
10
    /**
11
     * @var int
12
     */
13
    const PARAMETER_TYPE_ARRAY = 0;
14
15
    /**
16
     * @var int
17
     */
18
    const PARAMETER_TYPE_VALUE = 1;
19
20
    /**
21
     * @var array
22
     */
23
    private $attributes;
24
25
    /**
26
     * BaseTokenParser constructor.
27
     *
28
     * @param array $attributes optional attributes for the corresponding node
29
     */
30 8
    public function __construct(array $attributes = [])
31
    {
32 8
        $this->attributes = $attributes;
33 8
    }
34
35
    /**
36
     * @param \Twig_Token $token
37
     *
38
     * @return array
39
     */
40 3
    public function configureParameters(\Twig_Token $token): array
41
    {
42 3
        return [];
43
    }
44
45
    /**
46
     * @return array
47
     */
48 48
    public function getAttributes(): array
49
    {
50 48
        return $this->attributes;
51
    }
52
53
    /**
54
     * Create a concrete node.
55
     *
56
     * @param array $nodes
57
     * @param int   $lineNo
58
     *
59
     * @return \Twig_Node
60
     */
61
    abstract public function createNode(array $nodes = [], int $lineNo = 0): \Twig_Node;
62
63
    /**
64
     * @return bool
65
     */
66 48
    public function hasBody(): bool
67
    {
68 48
        return true;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     *
74
     * @throws \InvalidArgumentException
75
     */
76 48
    public function parse(\Twig_Token $token)
77
    {
78
        // parse parameters
79 48
        $nodes = $this->parseParameters($this->configureParameters($token));
80
81
        // parse body
82 48
        if ($this->hasBody()) {
83 48
            $nodes['body'] = $this->parseBody();
84
        }
85
86 48
        return $this->createNode($nodes, $token->getLine());
87
    }
88
89
    /**
90
     * @param array $parameterConfiguration
91
     *
92
     * @throws \InvalidArgumentException
93
     * @throws \Twig_Error_Syntax
94
     *
95
     * @return \Twig_Node_Expression[]
96
     */
97 48
    private function parseParameters(array $parameterConfiguration = []): array
98
    {
99
        // parse expressions
100 48
        $expressions = [];
101 48
        while (!$this->parser->getStream()->test(\Twig_Token::BLOCK_END_TYPE)) {
102 46
            $expressions[] = $this->parser->getExpressionParser()->parseExpression();
103
        }
104
105
        // end of expressions
106 48
        $this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE);
107
108
        // map expressions to parameters
109 48
        $parameters = [];
110 48
        foreach ($parameterConfiguration as $parameterName => $parameterOptions) {
111
            // try mapping expression
112 48
            $expression = reset($expressions);
113 48
            if ($expression !== false) {
114 46
                switch ($parameterOptions['type']) {
115 46
                    case self::PARAMETER_TYPE_ARRAY:
116
                        // check if expression is valid array
117 13
                        $valid = $expression instanceof \Twig_Node_Expression_Array;
118 13
                        break;
119 44
                    case self::PARAMETER_TYPE_VALUE:
120
                        // check if expression is valid value
121 44
                        $valid = !($expression instanceof \Twig_Node_Expression_Array);
122 44
                        break;
123
                    default:
124
                        throw new \InvalidArgumentException('Invalid parameter type');
125
                }
126
127 46
                if ($valid) {
128
                    // set expression as parameter and remove it from expressions list
129 46
                    $parameters[$parameterName] = array_shift($expressions);
130 46
                    continue;
131
                }
132
            }
133
134
            // set default as parameter otherwise or throw exception if default is false
135 46
            if ($parameterOptions['default'] === false) {
136
                throw new \Twig_Error_Syntax('A required parameter is missing');
137
            }
138 46
            $parameters[$parameterName] = $parameterOptions['default'];
139
        }
140
141 48
        if (count($expressions) > 0) {
142
            throw new \Twig_Error_Syntax('Too many parameters');
143
        }
144
145 48
        return $parameters;
146
    }
147
148
    /**
149
     * @return \Twig_Node
150
     */
151
    private function parseBody(): \Twig_Node
152
    {
153
        // parse body
154 48
        $body = $this->parser->subparse(function (\Twig_Token $token) {
155 48
            return $token->test('end'.$this->getTag());
156 48
        },
157 48
            true
158
        );
159
160
        // end of body
161 48
        $this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE);
162
163 48
        return $body;
164
    }
165
}
166