Completed
Pull Request — master (#15)
by Emanuele
04:39
created

FeatureTokenParser   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 98
Duplicated Lines 13.27 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 12
c 3
b 1
f 0
lcom 1
cbo 6
dl 13
loc 98
ccs 0
cts 55
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
C parse() 13 68 9
A decideFeatureFork() 0 4 1
A decideFeatureEnd() 0 4 1
A getTag() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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