Passed
Pull Request — master (#2)
by Victor
03:19
created

OptionalTokenParser   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 32
rs 10
c 0
b 0
f 0
ccs 10
cts 10
cp 1
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getTag() 0 3 1
A parse() 0 13 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Shoot\Shoot\Twig\TokenParser;
5
6
use Shoot\Shoot\Twig\Node\OptionalNode;
7
use Twig_Error_Syntax as SyntaxError;
8
use Twig_Node as Node;
9
use Twig_Token as Token;
10
use Twig_TokenParser as AbstractTokenParser;
11
12
final class OptionalTokenParser extends AbstractTokenParser
13
{
14
    /**
15
     * Parses a token and returns a node.
16
     *
17
     * @param Token $token
18
     *
19
     * @throws SyntaxError
20
     *
21
     * @return Node
22
     */
23 2
    public function parse(Token $token): Node
24
    {
25 2
        $stream = $this->parser->getStream();
26
27 2
        $stream->expect(Token::BLOCK_END_TYPE);
28
29 2
        $body = $this->parser->subparse(function (Token $token): bool {
30 2
            return $token->test('endoptional');
31 2
        }, true);
32
33 2
        $stream->expect(Token::BLOCK_END_TYPE);
34
35 2
        return new OptionalNode($body, $token->getLine(), $this->getTag());
36
    }
37
38
    /**
39
     * @return string The tag name associated with this token parser.
40
     */
41 6
    public function getTag(): string
42
    {
43 6
        return 'optional';
44
    }
45
}
46