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.
Test Failed
Push — master ( 1dd304...6c6f43 )
by Mewes
06:12
created

BaseTokenParser::parseParameters()   B

Complexity

Conditions 9
Paths 32

Size

Total Lines 50

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 9.1244

Importance

Changes 0
Metric Value
dl 0
loc 50
ccs 23
cts 26
cp 0.8846
rs 7.5353
c 0
b 0
f 0
cc 9
nc 32
nop 1
crap 9.1244
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 \Exception
75
     * @throws \InvalidArgumentException
76
     */
77 48
    public function parse(\Twig_Token $token)
78
    {
79
        // parse parameters
80 48
        $nodes = $this->parseParameters($this->configureParameters($token));
81
82
        // parse body
83 48
        if ($this->hasBody()) {
84 48
            $nodes['body'] = $this->parseBody();
85
        }
86
87 48
        return $this->createNode($nodes, $token->getLine());
88
    }
89
90
    /**
91
     * @param array $parameterConfiguration
92
     *
93
     * @throws \Exception
94
     * @throws \InvalidArgumentException
95
     * @throws \Twig_Error_Syntax
96
     *
97
     * @return \Twig_Node_Expression[]
98
     */
99 48
    private function parseParameters(array $parameterConfiguration = []): array
100
    {
101
        // parse expressions
102 48
        $expressions = [];
103 48
        while (!$this->parser->getStream()->test(\Twig_Token::BLOCK_END_TYPE)) {
104 46
            $expressions[] = $this->parser->getExpressionParser()->parseExpression();
105
        }
106
107
        // end of expressions
108 48
        $this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE);
109
110
        // map expressions to parameters
111 48
        $parameters = [];
112 48
        foreach ($parameterConfiguration as $parameterName => $parameterOptions) {
113
            // try mapping expression
114 48
            $expression = reset($expressions);
115 48
            if ($expression !== false) {
116 46
                switch ($parameterOptions['type']) {
117 46
                    case self::PARAMETER_TYPE_ARRAY:
118
                        // check if expression is valid array
119 13
                        $valid = $expression instanceof \Twig_Node_Expression_Array;
120 13
                        break;
121 44
                    case self::PARAMETER_TYPE_VALUE:
122
                        // check if expression is valid value
123 44
                        $valid = !($expression instanceof \Twig_Node_Expression_Array);
124 44
                        break;
125
                    default:
126
                        throw new \InvalidArgumentException('Invalid parameter type');
127
                }
128
129 46
                if ($valid) {
130
                    // set expression as parameter and remove it from expressions list
131 46
                    $parameters[$parameterName] = array_shift($expressions);
132 46
                    continue;
133
                }
134
            }
135
136
            // set default as parameter otherwise or throw exception if default is false
137 46
            if ($parameterOptions['default'] === false) {
138
                throw new \Twig_Error_Syntax('A required parameter is missing');
139
            }
140 46
            $parameters[$parameterName] = $parameterOptions['default'];
141
        }
142
143 48
        if (\count($expressions) > 0) {
144
            throw new \Twig_Error_Syntax('Too many parameters');
145
        }
146
147 48
        return $parameters;
148
    }
149
150
    /**
151
     * @return \Twig_Node
152
     * @throws \Twig_Error_Syntax
153
     */
154
    private function parseBody(): \Twig_Node
155
    {
156
        // parse till matching end tag is found
157
        $body = $this->parser->subparse(function (\Twig_Token $token) { return $token->test('end'.$this->getTag()); }, true);
158 48
        $this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE);
159 48
        return $body;
160
    }
161
}
162