Completed
Pull Request — master (#13)
by Asmir
02:23
created

TranslateAttrNAttribute::visit()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 35
Code Lines 19

Duplication

Lines 3
Ratio 8.57 %

Code Coverage

Tests 19
CRAP Score 6.0045

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 3
loc 35
ccs 19
cts 20
cp 0.95
rs 8.439
cc 6
eloc 19
nc 10
nop 2
crap 6.0045
1
<?php
2
3
namespace Goetas\TwitalBundle\Attribute;
4
5
use DOMAttr;
6
use Goetas\Twital\Attribute;
7
use Goetas\Twital\Compiler;
8
use Goetas\Twital\Helper\ParserHelper;
9
10
/**
11
 *
12
 * @author Asmir Mustafic <[email protected]>
13
 *
14
 */
15
class TranslateAttrNAttribute implements Attribute
16
{
17
18 6
    public static function getVarname(\DOMNode $node)
19
    {
20 6
        return "__a" . str_replace("-", "_", spl_object_hash($node));
21
    }
22
23 6
    public function visit(DOMAttr $att, Compiler $context)
24
    {
25 6
        $node = $att->ownerElement;
26 6
        $expressions = ParserHelper::staticSplitExpression($att->value, ",");
27 6
        $varName = self::getVarname($node);
28
29 6
        $parts = array();
30
31 6
        foreach ($expressions as $expression) {
32
33 6
            $attrExpr = ParserHelper::staticSplitExpression($expression, ":", 2);
34
35 6 View Code Duplication
            if (!$node->hasAttribute($attrExpr[0])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
                throw new \Exception("non trovo l'attributo " . $attrExpr[0] . " da tradurre");
37
            }
38 6
            $attNode = $node->getAttributeNode($attrExpr[0]);
39
40 6
            $transParams = isset($attrExpr[1]) ? trim($attrExpr[1], "\n\t\r []") : array();
41 6
            $transParams = ParserHelper::staticSplitExpression($transParams, ",", 2);
42
43 6
            $parts[$attrExpr[0]] = "['" . addcslashes($attNode->value, "'") . "'|transchoice($transParams[0]" . (isset($transParams[1]) ? (", " . $transParams[1]) : '') . "" . (isset($transParams[2]) ? ", $transParams[2]" : "") . ")]";
44
45 6
            $node->removeAttributeNode($attNode);
46
        }
47
48 6
        $code = "set $varName = $varName|default({})|merge({" . ParserHelper::implodeKeyed(",", $parts, true) . "})";
49
50 6
        $node->setAttribute("__attr__", $varName);
51
52 6
        $pi = $context->createControlNode($code);
53
54 6
        $node->parentNode->insertBefore($pi, $node);
55
56 6
        $node->removeAttributeNode($att);
57 6
    }
58
}
59