FeatureTokenParser::decideFeatureEnd()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Ae\FeatureBundle\Twig\TokenParser;
4
5
use Ae\FeatureBundle\Twig\Node\FeatureNode;
6
use Twig_Error_Syntax;
7
use Twig_Token;
8
use Twig_TokenParser;
9
10
/**
11
 * @author Carlo Forghieri <[email protected]>
12
 */
13
class FeatureTokenParser extends Twig_TokenParser
14
{
15
    /**
16
     * Parses a token and returns a node.
17
     *
18
     * @param Twig_Token $token A Twig_Token instance
19
     *
20
     * @return \Twig_NodeInterface A Twig_NodeInterface instance
21
     */
22
    public function parse(Twig_Token $token)
23
    {
24
        $lineno = $token->getLine();
25
        $stream = $this->parser->getStream();
26
27
        $name = null;
28
        $parent = null;
29
        if (!$stream->test(Twig_Token::BLOCK_END_TYPE)) {
30
            if ($stream->test(Twig_Token::STRING_TYPE)) {
31
                // {% feature "name" %}
32
                $name = $stream->next()->getValue();
33
            } elseif (!$stream->test(Twig_Token::BLOCK_END_TYPE)) {
34
                throw new Twig_Error_Syntax('Unexpected token. Twig was looking for the "name" string.');
35
            }
36
            if ($stream->test('from')) {
37
                // {% feature "name" from "parent" %}
38
                $stream->next();
39
                $parent = $stream->next()->getValue();
40
            } elseif (!$stream->test(Twig_Token::BLOCK_END_TYPE)) {
41
                throw new Twig_Error_Syntax('Unexpected token. Twig was looking for the "from" keyword.');
42
            }
43
        }
44
45
        // {% feature %}...{% endfeature %}
46
        $stream->expect(Twig_Token::BLOCK_END_TYPE);
47
        $body = $this->parser->subparse([$this, 'decideFeatureFork']);
48
49
        $else = null;
50
        $end = false;
51
        while (!$end) {
52
            switch ($this->parser->getStream()->next()->getValue()) {
53
                case 'else':
54
                    $stream->expect(Twig_Token::BLOCK_END_TYPE);
55
                    $else = $this->parser->subparse([$this, 'decideFeatureEnd']);
56
                    break;
57
58
                case 'endfeature':
59
                    $end = true;
60
                    break;
61
62
                default:
63
                    throw new Twig_Error_Syntax(sprintf('Unexpected end of template. Twig was looking for the following tags "else" or "endfeature" to close the "feature" block started at line %d)', $lineno), -1);
64
            }
65
        }
66
67
        $stream->expect(Twig_Token::BLOCK_END_TYPE);
68
69
        return new FeatureNode(
70
            $name,
71
            $parent,
72
            $body,
73
            $else,
74
            $lineno,
75
            $this->getTag()
76
        );
77
    }
78
79
    public function decideFeatureFork(Twig_Token $token)
80
    {
81
        return $token->test(['else', 'endfeature']);
82
    }
83
84
    public function decideFeatureEnd(Twig_Token $token)
85
    {
86
        return $token->test(['endfeature']);
87
    }
88
89
    /**
90
     * Gets the tag name associated with this token parser.
91
     *
92
     * @return string The tag name
93
     */
94
    public function getTag()
95
    {
96
        return 'feature';
97
    }
98
}
99