Cell::parse()   A
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 0
cts 18
cp 0
rs 9.1608
c 0
b 0
f 0
cc 5
nc 16
nop 1
crap 30
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\Cell as CellNode;
19
20
/**
21
 * Class Element.
22
 * @package WyriHaximus\TwigView\Lib\Twig\TokenParser
23
 */
24
final class Cell extends IncludeTokenParser
25
{
26
    /**
27
     * Parse token.
28
     *
29
     * @param \Twig\Token $token Token.
30
     *
31
     * @return \WyriHaximus\TwigView\Lib\Twig\Node\Cell
32
     */
33
    public function parse(Token $token): Node
34
    {
35
        $stream = $this->parser->getStream();
36
37
        $variable = null;
38
        if ($stream->test(Token::NAME_TYPE)) {
39
            $variable = $stream->expect(Token::NAME_TYPE)->getValue();
40
        }
41
        $assign = false;
42
        if ($stream->test(Token::OPERATOR_TYPE)) {
43
            $stream->expect(Token::OPERATOR_TYPE, '=');
44
            $assign = true;
45
        }
46
47
        $name = $this->parser->getExpressionParser()->parseExpression();
48
        $data = null;
49
        if (!$stream->test(Token::BLOCK_END_TYPE)) {
50
            $data = $this->parser->getExpressionParser()->parseExpression();
51
        }
52
        $options = null;
53
        if (!$stream->test(Token::BLOCK_END_TYPE)) {
54
            $options = $this->parser->getExpressionParser()->parseExpression();
55
        }
56
57
        $stream->expect(Token::BLOCK_END_TYPE);
58
59
        return new CellNode($assign, $variable, $name, $data, $options, $token->getLine(), $this->getTag());
60
    }
61
62
    /**
63
     * Tag name.
64
     *
65
     * @return string
66
     */
67
    public function getTag(): string
68
    {
69
        return 'cell';
70
    }
71
}
72