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 ( 8182e1...f8e5a3 )
by Mewes
02:18
created

BaseTokenParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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
    public function __construct(array $attributes = [])
31
    {
32
        $this->attributes = $attributes;
33
    }
34
35
    /**
36
     * @param \Twig_Token $token
37
     *
38
     * @return array
39
     */
40
    public function configureParameters(\Twig_Token $token): array
41
    {
42
        return [];
43
    }
44
45
    /**
46
     * @return array
47
     */
48
    public function getAttributes(): array
49
    {
50
        return $this->attributes;
51
    }
52
53
    /**
54
     * The class name of the corresponding node.
55
     *
56
     * @return string
57
     */
58
    abstract public function getNode(): string;
59
60
    /**
61
     * @return bool
62
     */
63
    public function hasBody(): bool
64
    {
65
        return true;
66
    }
67
68
    /**
69
     * @param \Twig_Token $token
70
     *
71
     * @throws \Twig_Error_Syntax
72
     * @throws \InvalidArgumentException
73
     *
74
     * @return \Twig_node
75
     */
76
    public function parse(\Twig_Token $token)
77
    {
78
        // parse attributes
79
        $nodes = $this->parseParameters($this->configureParameters($token));
80
81
        // parse body
82
        if ($this->hasBody()) {
83
            $nodes['body'] = $this->parseBody();
84
        }
85
86
        // return node
87
        $nodeClass = $this->getNode();
88
89
        return new $nodeClass($nodes, $this->getAttributes(), $token->getLine(), $this->getTag());
90
    }
91
92
    /**
93
     * @param array $parameterConfiguration
94
     *
95
     * @throws \InvalidArgumentException
96
     * @throws \Twig_Error_Syntax
97
     *
98
     * @return \Twig_Node_Expression[]
99
     */
100
    private function parseParameters(array $parameterConfiguration = []): array
101
    {
102
        // parse expressions
103
        $expressions = [];
104
        while (!$this->parser->getStream()->test(\Twig_Token::BLOCK_END_TYPE)) {
105
            $expressions[] = $this->parser->getExpressionParser()->parseExpression();
106
        }
107
108
        // end of expressions
109
        $this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE);
110
111
        // map expressions to parameters
112
        $parameters = [];
113
        foreach ($parameterConfiguration as $parameterName => $parameterOptions) {
114
            // try mapping expression
115
            $expression = reset($expressions);
116
            if ($expression !== false) {
117
                switch ($parameterOptions['type']) {
118
                    case self::PARAMETER_TYPE_ARRAY:
119
                        // check if expression is valid array
120
                        $valid = $expression instanceof \Twig_Node_Expression_Array;
121
                        break;
122
                    case self::PARAMETER_TYPE_VALUE:
123
                        // check if expression is valid value
124
                        $valid = !($expression instanceof \Twig_Node_Expression_Array);
125
                        break;
126
                    default:
127
                        throw new \InvalidArgumentException('Invalid parameter type');
128
                }
129
130
                if ($valid) {
131
                    // set expression as parameter and remove it from expressions list
132
                    $parameters[$parameterName] = array_shift($expressions);
133
                    continue;
134
                }
135
            }
136
137
            // set default as parameter otherwise or throw exception if default is false
138
            if ($parameterOptions['default'] === false) {
139
                throw new \Twig_Error_Syntax('A required parameter is missing');
140
            }
141
            $parameters[$parameterName] = $parameterOptions['default'];
142
        }
143
144
        if (count($expressions) > 0) {
145
            throw new \Twig_Error_Syntax('Too many parameters');
146
        }
147
148
        return $parameters;
149
    }
150
151
    /**
152
     * @return \Twig_Node
153
     */
154
    private function parseBody(): \Twig_Node
155
    {
156
        // parse body
157
        $body = $this->parser->subparse(function (\Twig_Token $token) {
158
            return $token->test('end'.$this->getTag());
159
        },
160
            true
161
        );
162
163
        // end of body
164
        $this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE);
165
166
        return $body;
167
    }
168
}
169