TokenParser   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 113
ccs 0
cts 55
cp 0
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
C parse() 0 68 14
A getTag() 0 4 1
1
<?php
2
3
namespace Rocket\UI\Forms\Support\Twig;
4
5
use Twig_Error_Syntax;
6
use Twig_NodeInterface;
7
use Twig_Token;
8
use Twig_TokenParser;
9
10
/**
11
 * Form extension for twig
12
 */
13
14
/**
15
 * Form extension for twig
16
 *
17
 * @author Stéphane Goetz
18
 */
19
class TokenParser extends Twig_TokenParser
20
{
21
    /**
22
     * form valid methods
23
     *
24
     * @var array
25
     */
26
    private $valid_methods = [
27
        'width',
28
        'height',
29
30
        'event',
31
        'checked',
32
        'value',
33
        'values',
34
        'first',
35
        'last',
36
        'id',
37
        'tip',
38
        'placeholder',
39
        'autocomplete',
40
        'inline',
41
        'data',
42
        'options',
43
        'class',
44
    ];
45
46
    /**
47
     * Parses a token and returns a node.
48
     *
49
     * @param  Twig_Token                                   $token
50
     * @throws Twig_Error_Syntax
51
     * @return Node|Twig_NodeInterface
52
     */
53
    public function parse(Twig_Token $token)
54
    {
55
        $lineno = $token->getLine();
56
57
        $parameters_finished = false;
58
59
        $parameters = [];
60
        $methods = [];
61
62
        $method_name = '';
63
        $method_level = 0;
64
        $current_parameters = [];
65
66
        do {
67
            $next = true;
68
69
            $value_next = $this->parser->getStream()->look()->getValue();
70
            $value_current = $this->parser->getStream()->getCurrent()->getValue();
71
72
            //if we get a parenthesis, it means the next is a method
73
            if ($value_next == '(') {
74
                $method_level++;
75
                $valid_method = in_array($value_current, $this->valid_methods);
76
                if (!$parameters_finished && $valid_method) {
77
                    $parameters_finished = true;
78
                } elseif ($parameters_finished && $method_level == 1 && !$valid_method) {
79
                    $message = sprintf('The function "%s" does not exist for the Form module', $value_current);
80
                    throw new Twig_Error_Syntax($message);
81
                }
82
            }
83
84
            if (!$parameters_finished) {
85
                //Parameters
86
87
                $p = $this->parser->getExpressionParser()->parseExpression();
88
                $parameters[] = $p;
89
                $next = false;
90
            } else {
91
                //Methods
92
93
                if ($value_next == '(') {
94
                    $method_name = $value_current;
95
                } elseif (!in_array($value_current, ['(', ')', ','])) {
96
                    $current_parameters[] = $this->parser->getExpressionParser()->parseExpression();
97
98
                    $next = false;
99
                }
100
101
                if ($this->parser->getStream()->getCurrent()->getValue() == ')') {
102
                    $method_level--;
103
                    if ($method_name != '') {
104
                        $methods[][$method_name] = $current_parameters;
105
                        $current_parameters = [];
106
                        $method_name = '';
107
                        $next = true;
108
                    }
109
                }
110
            }
111
112
            if ($next) {
113
                $this->parser->getStream()->next();
114
            }
115
        } while (!$this->parser->getStream()->test(Twig_Token::BLOCK_END_TYPE));
116
117
        $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
118
119
        return new Node($parameters, $methods, $lineno, $this->getTag());
120
    }
121
122
    /**
123
     * Gets the tag name associated with this token parser.
124
     *
125
     * @return string The tag name
126
     */
127
    public function getTag()
128
    {
129
        return 'form';
130
    }
131
}
132