AttrAppendAttribute::visit()   B
last analyzed

Complexity

Conditions 8
Paths 36

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 43
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 51
ccs 43
cts 43
cp 1
rs 7.8246
c 0
b 0
f 0
cc 8
nc 36
nop 2
crap 8

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Goetas\Twital\Attribute;
3
4
use Goetas\Twital\Compiler;
5
use Goetas\Twital\Helper\ParserHelper;
6
7
/**
8
 *
9
 * @author Asmir Mustafic <[email protected]>
10
 *
11
 */
12
class AttrAppendAttribute extends AttrAttribute
13
{
14 6
    public function visit(\DOMAttr $att, Compiler $context)
15
    {
16 6
        $node = $att->ownerElement;
17 6
        $expressions = ParserHelper::staticSplitExpression($att->value, ",");
18
19 6
        $attributes = array();
20 6
        foreach ($expressions as $k => $expression) {
21 6
            $expressions[$k] = $attrExpr = self::splitAttrExpression($expression);
22 6
            $attNode = null;
23 6
            if (!isset($attributes[$attrExpr['name']])) {
24 6
                $attributes[$attrExpr['name']] = array();
25 6
            }
26 6
            if ($node->hasAttribute($attrExpr['name'])) {
27 1
                $attNode = $node->getAttributeNode($attrExpr['name']);
28 1
                $node->removeAttributeNode($attNode);
29 1
                $attributes[$attrExpr['name']][] = "'" . addcslashes($attNode->value, "'") . "'";
30 1
            }
31 6
            if ($attrExpr['test'] === "true" || $attrExpr['test'] === "1") {
32 3
                unset($expressions[$k]);
33 3
                $attributes[$attrExpr['name']][] = $attrExpr['expr'];
34 3
            }
35 6
        }
36
37 6
        $code = array();
38
39 6
        $varName = self::getVarname($node);
40 6
        $code[] = $context->createControlNode("if $varName is not defined");
41 6
        $code[] = $context->createControlNode("set $varName = {" . ParserHelper::implodeKeyedDouble(",", $attributes, true) . " }");
42 6
        $code[] = $context->createControlNode("else");
43
44 6
        foreach ($attributes as $attribute => $values) {
45 6
            $code[] = $context->createControlNode("if {$varName}['{$attribute}'] is defined");
46 6
            $code[] = $context->createControlNode("set $varName = $varName|merge({ '$attribute' : ({$varName}['{$attribute}']|merge([" . implode(",", $values) . "])) })");
47 6
            $code[] = $context->createControlNode("else");
48 6
            $code[] = $context->createControlNode("set $varName = $varName|merge({ '$attribute' : [" . implode(",", $values) . "]})");
49 6
            $code[] = $context->createControlNode("endif");
50 6
        }
51
52 6
        $code[] = $context->createControlNode("endif");
53
54 6
        foreach ($expressions as $attrExpr) {
55 3
            $code[] = $context->createControlNode("if {$attrExpr['test']}");
56 3
            $code[] = $context->createControlNode(
57 3
                "set {$varName} = {$varName}|merge({ '{$attrExpr['name']}':{$varName}.{$attrExpr['name']}|merge([{$attrExpr['expr']}]) })"
58 3
            );
59 3
            $code[] = $context->createControlNode("endif");
60 6
        }
61
62 6
        $this->addSpecialAttr($node, $varName, $code);
63 6
        $node->removeAttributeNode($att);
64 6
    }
65
}
66