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 Setup Failed
Push — master ( 78f8b7...0f754d )
by Elemér
04:15
created

BaseTokenParser::hasBody()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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