Passed
Push — master ( 61c4e2...c8eeb1 )
by Fran
03:27
created

AssetsTokenParser   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 20.9%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 141
ccs 14
cts 67
cp 0.209
rs 10
c 1
b 0
f 0
wmc 17
lcom 1
cbo 7

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A parse() 0 8 1
A getTag() 0 13 3
A checkTemplateLine() 0 17 3
A extractTemplateNodes() 0 11 2
A findTemplateNode() 0 12 3
A getTmpAttribute() 0 9 2
A extractTmpAttribute() 0 12 2
1
<?php
2
3
namespace PSFS\base\extension;
4
5
/**
6
 * Class AssetsTokenParser
7
 * @package PSFS\base\extension
8
 */
9
class AssetsTokenParser extends \Twig_TokenParser
10
{
11
12
    protected $type;
13
    private $values = array();
14
    private $end = false;
15
16
    /**
17
     * @param string $type
18
     */
19 1
    public function __construct($type = 'js')
20
    {
21 1
        $this->type = $type;
22 1
    }
23
24
    /**
25
     * Método que parsea los nodos de la plantilla
26
     * @param \Twig_Token $token
27
     * @return AssetsNode
28
     * @throws \Twig_Error_Syntax
29
     * @throws \Twig_Error_Loader
30
     */
31
    public function parse(\Twig_Token $token)
32
    {
33
        $hash = substr(md5($this->parser->getStream()->getSourceContext()->getPath()), 0, 8);
34
        $name = $token->getValue();
35
        $this->extractTemplateNodes();
36
        $node = $this->findTemplateNode();
37
        return new AssetsNode($name, array("node" => $node, "hash" => $hash), $token->getLine(), $this->getTag(), $this->type);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 127 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
38
    }
39
40
    /**
41
     * Método que devuelve el tag a buscar en la plantilla
42
     * @return string
43
     */
44 1
    public function getTag()
45
    {
46 1
        switch ($this->type) {
47 1
            default:
48 1
            case 'js':
49 1
                $return = 'scripts';
50 1
                break;
51 1
            case 'css':
52 1
                $return = 'styles';
53 1
                break;
54 1
        }
55 1
        return $return;
56
    }
57
58
    /**
59
     * Método que revisa cada l�nea de la plantilla
60
     * @param \Twig_TokenStream $stream
61
     * @return \Twig_TokenStream
62
     */
63
    protected function checkTemplateLine(\Twig_TokenStream $stream)
64
    {
65
        $value = $stream->getCurrent();
66
        switch ($value->getType()) {
67
            case \Twig_Token::STRING_TYPE:
68
                $this->values[] = $this->parser->getExpressionParser()->parseExpression();
69
                break;
70
            case \Twig_Token::BLOCK_END_TYPE:
71
                $this->end = true;
72
                $stream->next();
73
                break;
74
            default:
75
                $stream->next();
76
                break;
77
        }
78
        return $stream;
79
    }
80
81
    /**
82
     * Método que procesa cada l�nea de la plantilla para extraer los nodos
83
     * @throws \Twig_Error_Syntax
84
     */
85
    protected function extractTemplateNodes()
86
    {
87
        $stream = $this->parser->getStream();
88
        while (!$this->end) {
89
            $stream = $this->checkTemplateLine($stream);
90
        }
91
92
        $stream->expect(\Twig_Token::BLOCK_START_TYPE);
93
        $stream->expect(\Twig_Token::NAME_TYPE);
94
        $stream->expect(\Twig_Token::BLOCK_END_TYPE);
95
    }
96
97
    /**
98
     * Método que busca el nodo a parsear
99
     * @return \Twig_Node_Expression|null
100
     */
101
    protected function findTemplateNode()
102
    {
103
        $node = null;
104
        if (0 < count($this->values)) {
105
            /** @var \Twig_Node_Expression|\Twig_Node_Expression_Conditional $value */
106
            foreach ($this->values as $value) {
107
                list($tmp, $node) = $this->extractTmpAttribute($node, $value);
108
                $node->setAttribute("value", $tmp);
109
            }
110
        }
111
        return $node;
112
    }
113
114
    /**
115
     * Método que extrae el valor del token
116
     * @param \Twig_Node_Expression|\Twig_Node_Expression_Conditional|null $node
117
     *
118
     * @return array
119
     */
120
    protected function getTmpAttribute($node = null)
121
    {
122
        $tmp = $node->getAttribute("value");
123
        if (!is_array($tmp)) {
124
            $tmp = array($tmp);
125
        }
126
127
        return $tmp;
128
    }
129
130
    /**
131
     * Método
132
     * @param \Twig_Node_Expression|\Twig_Node_Expression_Conditional|null $node
133
     * @param \Twig_Node_Expression|\Twig_Node_Expression_Conditional|null $value
134
     *
135
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<array|\Twig_Node_Expression|null>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
136
     */
137
    protected function extractTmpAttribute($node = null, $value = null)
138
    {
139
        $tmp = array();
140
        if (NULL === $node) {
141
            $node = $value;
142
        } else {
143
            $tmp = $this->getTmpAttribute($node);
144
        }
145
        $tmp[] = $value->getAttribute("value");
146
147
        return array($tmp, $node);
148
    }
149
}
150