Element::parse()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 11
cp 0
rs 9.6333
c 0
b 0
f 0
cc 3
nc 4
nop 1
crap 12
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of TwigView.
6
 *
7
 ** (c) 2014 Cees-Jan Kiewiet
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace WyriHaximus\TwigView\Lib\Twig\TokenParser;
14
15
use Twig\Node\Node;
16
use Twig\Token;
17
use Twig\TokenParser\IncludeTokenParser;
18
use WyriHaximus\TwigView\Lib\Twig\Node\Element as ElementNode;
19
20
/**
21
 * Class Element.
22
 * @package WyriHaximus\TwigView\Lib\Twig\TokenParser
23
 */
24
final class Element extends IncludeTokenParser
25
{
26
    /**
27
     * Parse token.
28
     *
29
     * @param \Twig\Token $token Token.
30
     *
31
     * @return \WyriHaximus\TwigView\Lib\Twig\Node\Element
32
     */
33
    public function parse(Token $token): Node
34
    {
35
        $stream = $this->parser->getStream();
36
        $name = $this->parser->getExpressionParser()->parseExpression();
37
38
        $data = null;
39
        if (!$stream->test(Token::BLOCK_END_TYPE)) {
40
            $data = $this->parser->getExpressionParser()->parseExpression();
41
        }
42
43
        $options = null;
44
        if (!$stream->test(Token::BLOCK_END_TYPE)) {
45
            $options = $this->parser->getExpressionParser()->parseExpression();
46
        }
47
48
        $stream->expect(Token::BLOCK_END_TYPE);
49
50
        return new ElementNode($name, $data, $options, $token->getLine(), $this->getTag());
51
    }
52
53
    /**
54
     * Get tag name.
55
     *
56
     * @return string
57
     */
58
    public function getTag(): string
59
    {
60
        return 'element';
61
    }
62
}
63