Passed
Push — master ( 1c0893...f7ffcc )
by Fran
03:22
created

AssetsTokenParser::findTemplateNode()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 2
nop 0
dl 0
loc 12
ccs 0
cts 9
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
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
    protected $type;
12
    private $values = array();
13
    private $end = false;
14
15
    /**
16
     * @param string $type
17
     */
18 1
    public function __construct($type = 'js') {
19 1
        $this->type = $type;
20 1
    }
21
22
    /**
23
     * Método que parsea los nodos de la plantilla
24
     * @param \Twig_Token $token
25
     * @return AssetsNode
26
     * @throws \Twig_Error_Syntax
27
     * @throws \Twig_Error_Loader
28
     */
29
    public function parse(\Twig_Token $token) {
30
        $hash = substr(md5($this->parser->getStream()->getSourceContext()->getPath()), 0, 8);
31
        $name = $token->getValue();
32
        $this->extractTemplateNodes();
33
        $node = $this->findTemplateNode();
34
        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...
35
    }
36
37
    /**
38
     * Método que devuelve el tag a buscar en la plantilla
39
     * @return string
40
     */
41
    public function getTag()
42
    {
43
        switch ($this->type)
44
        {
45
            default:
46
            case 'js': $return = 'scripts'; break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
47
            case 'css': $return = 'styles'; break;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
48
        }
49
        return $return;
50
    }
51
52
    /**
53
     * Método que revisa cada l�nea de la plantilla
54
     * @param \Twig_TokenStream $stream
55
     * @return \Twig_TokenStream
56
     */
57
    protected function checkTemplateLine(\Twig_TokenStream $stream)
58
    {
59
        $value = $stream->getCurrent();
60
        switch ($value->getType()) {
61
            case \Twig_Token::STRING_TYPE:
62
                $this->values[] = $this->parser->getExpressionParser()->parseExpression();
63
                break;
64
            case \Twig_Token::BLOCK_END_TYPE:
65
                $this->end = true;
66
                $stream->next();
67
                break;
68
            default:
69
                $stream->next();
70
                break;
71
        }
72
        return $stream;
73
    }
74
75
    /**
76
     * Método que procesa cada l�nea de la plantilla para extraer los nodos
77
     * @throws \Twig_Error_Syntax
78
     */
79
    protected function extractTemplateNodes()
80
    {
81
        $stream = $this->parser->getStream();
82
        while (!$this->end) {
83
            $stream = $this->checkTemplateLine($stream);
84
        }
85
86
        $stream->expect(\Twig_Token::BLOCK_START_TYPE);
87
        $stream->expect(\Twig_Token::NAME_TYPE);
88
        $stream->expect(\Twig_Token::BLOCK_END_TYPE);
89
    }
90
91
    /**
92
     * Método que busca el nodo a parsear
93
     * @return \Twig_Node_Expression|null
94
     */
95
    protected function findTemplateNode()
96
    {
97
        $node = null;
98
        if (0 < count($this->values)) {
99
            /** @var \Twig_Node_Expression|\Twig_Node_Expression_Conditional $value */
100
            foreach ($this->values as $value) {
101
                list($tmp, $node) = $this->extractTmpAttribute($node, $value);
102
                $node->setAttribute("value", $tmp);
103
            }
104
        }
105
        return $node;
106
    }
107
108
    /**
109
     * Método que extrae el valor del token
110
     * @param \Twig_Node_Expression|\Twig_Node_Expression_Conditional|null $node
111
     *
112
     * @return array
113
     */
114
    protected function getTmpAttribute($node = null)
115
    {
116
        $tmp = $node->getAttribute("value");
117
        if (!is_array($tmp)) {
118
            $tmp = array($tmp);
119
        }
120
121
        return $tmp;
122
    }
123
124
    /**
125
     * Método
126
     * @param \Twig_Node_Expression|\Twig_Node_Expression_Conditional|null $node
127
     * @param \Twig_Node_Expression|\Twig_Node_Expression_Conditional|null $value
128
     *
129
     * @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...
130
     */
131
    protected function extractTmpAttribute($node = null, $value = null)
132
    {
133
        $tmp = array();
134
        if (NULL === $node) {
135
            $node = $value;
136
        }else {
137
            $tmp = $this->getTmpAttribute($node);
138
        }
139
        $tmp[] = $value->getAttribute("value");
140
141
        return array($tmp, $node);
142
    }
143
}
144