Passed
Push — master ( 8ef4d4...d6939e )
by Victor
46s queued 11s
created

OptionalTokenParser   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

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
     * @return Node
20
     */
21 2
    public function parse(Token $token): Node
22
    {
23 2
        $stream = $this->parser->getStream();
24
25 2
        $stream->expect(Token::BLOCK_END_TYPE);
26
27 2
        $body = $this->parser->subparse(function (Token $token): bool {
28 2
            return $token->test('endoptional');
29 2
        }, true);
30
31 2
        $stream->expect(Token::BLOCK_END_TYPE);
32
33 2
        return new OptionalNode($body, $token->getLine(), $this->getTag());
34
    }
35
36
    /**
37
     * @return string The tag name associated with this token parser.
38
     */
39 6
    public function getTag(): string
40
    {
41 6
        return 'optional';
42
    }
43
}
44