Completed
Pull Request — master (#15)
by Emanuele
14:44
created

FeatureTokenParser::parse()   C

Complexity

Conditions 9
Paths 23

Size

Total Lines 68
Code Lines 46

Duplication

Lines 13
Ratio 19.12 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 13
loc 68
ccs 0
cts 49
cp 0
rs 6.2813
cc 9
eloc 46
nc 23
nop 1
crap 90

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 View Code Duplication
            if ($stream->test(Twig_Token::STRING_TYPE)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
31
                // {% feature "name" %}
32
                $name = $stream->next()->getValue();
33
            } elseif (!$stream->test(Twig_Token::BLOCK_END_TYPE)) {
34
                throw new Twig_Error_Syntax(
35
                    'Unexpected token. Twig was looking for the "name" string.'
36
                );
37
            }
38 View Code Duplication
            if ($stream->test('from')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
                // {% feature "name" from "parent" %}
40
                $stream->next();
41
                $parent = $stream->next()->getValue();
42
            } elseif (!$stream->test(Twig_Token::BLOCK_END_TYPE)) {
43
                throw new Twig_Error_Syntax(
44
                    'Unexpected token. Twig was looking for the "from" keyword.'
45
                );
46
            }
47
        }
48
49
        // {% feature %}...{% endfeature %}
50
        $stream->expect(Twig_Token::BLOCK_END_TYPE);
51
        $body = $this->parser->subparse([$this, 'decideFeatureFork']);
52
53
        $else = null;
54
        $end = false;
55
        while (!$end) {
56
            switch ($this->parser->getStream()->next()->getValue()) {
57
                case 'else':
58
                    $stream->expect(Twig_Token::BLOCK_END_TYPE);
59
                    $else = $this->parser->subparse([$this, 'decideFeatureEnd']);
60
                    break;
61
62
                case 'endfeature':
63
                    $end = true;
64
                    break;
65
66
                default:
67
                    throw new Twig_Error_Syntax(
68
                        sprintf(
69
                            'Unexpected end of template. Twig was looking for '.
70
                            'the following tags "else" or "endfeature" to '.
71
                            'close the "feature" block started at line %d)',
72
                            $lineno
73
                        ),
74
                        -1
75
                    );
76
            }
77
        }
78
79
        $stream->expect(Twig_Token::BLOCK_END_TYPE);
80
81
        return new FeatureNode(
82
            $name,
83
            $parent,
84
            $body,
85
            $else,
86
            $lineno,
87
            $this->getTag()
88
        );
89
    }
90
91
    public function decideFeatureFork(Twig_Token $token)
92
    {
93
        return $token->test(['else', 'endfeature']);
94
    }
95
96
    public function decideFeatureEnd(Twig_Token $token)
97
    {
98
        return $token->test(['endfeature']);
99
    }
100
101
    /**
102
     * Gets the tag name associated with this token parser.
103
     *
104
     * @return string The tag name
105
     */
106
    public function getTag()
107
    {
108
        return 'feature';
109
    }
110
}
111