ContentEditableTokenParser::parse()   B
last analyzed

Complexity

Conditions 7
Paths 14

Size

Total Lines 58
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 7.5581

Importance

Changes 0
Metric Value
dl 0
loc 58
ccs 31
cts 40
cp 0.775
rs 7.6045
c 0
b 0
f 0
cc 7
eloc 34
nc 14
nop 1
crap 7.5581

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
/*
4
 * This file is part of the Ivoaz ContentEditable bundle.
5
 *
6
 * (c) Ivo Azirjans <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Ivoaz\Bundle\ContentEditableBundle\Twig;
13
14
/**
15
 * <pre>
16
 * {% contenteditable "content_name" locale="en" option1="value1" option2="value2" %}
17
 *   <p>This is a sample description</p>
18
 * {% endcontenteditable %}
19
 * </pre>
20
 *
21
 * @author Ivo Azirjans <[email protected]>
22
 */
23
class ContentEditableTokenParser extends \Twig_TokenParser
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28 2
    public function parse(\Twig_Token $token)
29
    {
30 2
        $lineno = $token->getLine();
31 2
        $stream = $this->parser->getStream();
32 2
        $name = $stream->expect(\Twig_Token::STRING_TYPE)->getValue();
33 2
        $options = [];
34
35 2
        while (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) {
36 1
            if (!$stream->test(\Twig_Token::NAME_TYPE)) {
37
                $this->throwSyntaxError($stream);
38
            }
39
40 1
            $key = $stream->getCurrent()->getValue();
41 1
            $stream->next();
42
43 1
            if (!$stream->test(\Twig_Token::OPERATOR_TYPE, '=')) {
44 1
                $options[$key] = true;
45
46 1
                continue;
47
            }
48
49 1
            $stream->next();
50
51 1
            if (!$this->testOptionValue($stream)) {
52
                $this->throwSyntaxError($stream);
53
            }
54
55 1
            $options[$key] = $this->getOptionValue($stream);
56
57 1
            $stream->next();
58 1
        }
59
60 2
        $stream->next();
61
62 2
        $body = $this->parser->subparse(
63 2
            function (\Twig_Token $token) {
64 2
                return $token->test(['endcontenteditable']);
65 2
            },
66
            true
67 2
        );
68
69 2
        if (!$body instanceof \Twig_Node_Text && !$body instanceof \Twig_Node_Expression) {
70
            throw new \Twig_Error_Syntax(
71
                'A text inside a contenteditable tag must be a simple text.',
72
                $body->getLine(),
73
                $stream->getFilename()
74
            );
75
        }
76
77 2
        $stream->expect(\Twig_Token::BLOCK_END_TYPE);
78
79 2
        return new ContentEditableNode(
80 2
            ['body' => $body],
81 2
            ['name' => $name, 'options' => $options],
82 2
            $lineno,
83 2
            $this->getTag()
84 2
        );
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 2
    public function getTag()
91
    {
92 2
        return 'contenteditable';
93
    }
94
95
    /**
96
     * @param \Twig_TokenStream $stream
97
     *
98
     * @return bool
99
     */
100 1
    private function testOptionValue(\Twig_TokenStream $stream)
101
    {
102
        return
103 1
            $stream->test(\Twig_Token::STRING_TYPE) ||
104 1
            $stream->test(\Twig_Token::NUMBER_TYPE) ||
105 1
            $stream->test(\Twig_Token::NAME_TYPE, ['true', 'false']);
106
    }
107
108 1
    private function getOptionValue(\Twig_TokenStream $stream)
109
    {
110 1
        $value = $stream->getCurrent()->getValue();
111
112 1
        if ($stream->test(\Twig_Token::NAME_TYPE, ['true', 'false'])) {
113 1
            return 'true' === $value ? true : false;
114
        }
115
116 1
        return $value;
117
    }
118
119
    /**
120
     * @param \Twig_TokenStream $stream
121
     *
122
     * @throws \Twig_Error_Syntax
123
     */
124
    private function throwSyntaxError(\Twig_TokenStream $stream)
125
    {
126
        $token = $stream->getCurrent();
127
128
        throw new \Twig_Error_Syntax(
129
            sprintf(
130
                'Unexpected token "%s" of value "%s"',
131
                \Twig_Token::typeToEnglish($token->getType()),
132
                $token->getValue()
133
            ),
134
            $token->getLine(),
135
            $stream->getFilename()
136
        );
137
    }
138
}
139