Test Failed
Push — master ( cd3718...faedb3 )
by Fran
03:03
created

AssetsTokenParser::getTmpAttribute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 11
ccs 6
cts 7
cp 0.8571
crap 3.0261
rs 10
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
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
     * @param \Twig_Token $token
26
     * @return AssetsNode|\Twig_Node
27
     * @throws \Twig_Error_Syntax
28
     */
29 2
    public function parse(\Twig_Token $token)
30
    {
31 2
        $hash = substr(md5($this->parser->getStream()->getSourceContext()->getPath()), 0, 8);
32 2
        $name = $token->getValue();
33 2
        $this->extractTemplateNodes();
34 1
        $node = $this->findTemplateNode();
35 1
        return new AssetsNode($name, array('node' => $node, 'hash' => $hash), $token->getLine(), $this->getTag(), $this->type);
36
    }
37
38
    /**
39
     * Método que devuelve el tag a buscar en la plantilla
40
     * @return string
41
     */
42 2
    public function getTag()
43
    {
44 2
        switch ($this->type) {
45
            default:
46
            case 'js':
47 2
                $return = 'scripts';
48 2
                break;
49
            case 'css':
50 2
                $return = 'styles';
51 2
                break;
52
        }
53 2
        return $return;
54
    }
55
56
    /**
57
     * @param \Twig_TokenStream $stream
58
     * @return \Twig_TokenStream
59
     * @throws \Twig_Error_Syntax
60
     */
61 1
    protected function checkTemplateLine(\Twig_TokenStream $stream)
62
    {
63 1
        $value = $stream->getCurrent();
64 1
        switch ($value->getType()) {
65 1
            case \Twig_Token::STRING_TYPE:
66 1
                $this->values[] = $this->parser->getExpressionParser()->parseExpression();
67 1
                break;
68 1
            case \Twig_Token::BLOCK_END_TYPE:
69 1
                $this->end = true;
70 1
                $stream->next();
71 1
                break;
72
            default:
73
                $stream->next();
74
                break;
75
        }
76 1
        return $stream;
77
    }
78
79
    /**
80
     * @throws \Twig_Error_Syntax
81
     */
82 2
    protected function extractTemplateNodes()
83
    {
84 2
        $stream = $this->parser->getStream();
85 2
        while (!$this->end) {
86 1
            $stream = $this->checkTemplateLine($stream);
87
        }
88
89 2
        $stream->expect(\Twig_Token::BLOCK_START_TYPE);
90 1
        $stream->expect(\Twig_Token::NAME_TYPE);
91 1
        $stream->expect(\Twig_Token::BLOCK_END_TYPE);
92 1
    }
93
94
    /**
95
     * @return mixed|null
96
     */
97 1
    protected function findTemplateNode()
98
    {
99 1
        $node = null;
100 1
        if (0 < count($this->values)) {
101
            /** @var \Twig_Node_Expression|\Twig_Node_Expression_Conditional $value */
102 1
            foreach ($this->values as $value) {
103 1
                list($tmp, $node) = $this->extractTmpAttribute($node, $value);
104 1
                $node->setAttribute('value', $tmp);
105
            }
106
        }
107 1
        return $node;
108
    }
109
110
    /**
111
     * @param null $node
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $node is correct as it would always require null to be passed?
Loading history...
112
     * @return array
113
     */
114 1
    protected function getTmpAttribute($node = null)
115
    {
116 1
        $tmp = [];
117 1
        if(null !== $node) {
0 ignored issues
show
introduced by
The condition null !== $node is always false.
Loading history...
118 1
            $tmp = $node->getAttribute('value');
119 1
            if (!is_array($tmp)) {
120
                $tmp = [$tmp];
121
            }
122
        }
123
124 1
        return $tmp;
125
    }
126
127
    /**
128
     * Método
129
     * @param \Twig_Node_Expression|\Twig_Node_Expression_Conditional|null $node
130
     * @param \Twig_Node_Expression|\Twig_Node_Expression_Conditional|null $value
131
     *
132
     * @return array
133
     */
134 1
    protected function extractTmpAttribute($node = null, $value = null)
135
    {
136 1
        $tmp = [];
137 1
        if (null === $node) {
138 1
            $node = $value;
139
        } else {
140 1
            $tmp = $this->getTmpAttribute($node);
141
        }
142 1
        if(null !== $node) {
143 1
            $tmp[] = $value->getAttribute('value');
0 ignored issues
show
Bug introduced by
The method getAttribute() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

143
            /** @scrutinizer ignore-call */ 
144
            $tmp[] = $value->getAttribute('value');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
144
        }
145 1
        return [$tmp, $node];
146
    }
147
}
148