EmbedNode::visit()   B
last analyzed

Complexity

Conditions 10
Paths 49

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 10.0268

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 29
cts 31
cp 0.9355
rs 7.6666
c 0
b 0
f 0
cc 10
nc 49
nop 2
crap 10.0268

How to fix   Complexity   

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\Node;
3
4
use Goetas\Twital\Compiler;
5
use Goetas\Twital\Exception;
6
use Goetas\Twital\Helper\DOMHelper;
7
use Goetas\Twital\Node;
8
9
/**
10
 *
11
 * @author Asmir Mustafic <[email protected]>
12
 *
13
 */
14
class EmbedNode implements Node
15
{
16 6
    public function visit(\DOMElement $node, Compiler $context)
17
    {
18 6
        if ($node->hasAttribute("from-exp")) {
19
            $filename = $node->getAttribute("from-exp");
20 6
        } elseif ($node->hasAttribute("from")) {
21 6
            $filename = '"' . $node->getAttribute("from") . '"';
22 6
        } else {
23
            throw new Exception("The 'from' or 'from-exp' attribute is required");
24
        }
25
26
        // remove any non-element node
27 6
        foreach (iterator_to_array($node->childNodes) as $child) {
28 6
            if (!($child instanceof \DOMElement)) {
29 6
                $child->parentNode->removeChild($child);
30 6
            }
31 6
        }
32
33 6
        $context->compileChilds($node);
34
35 6
        $code = "embed {$filename}";
36
37 6
        if ($node->hasAttribute("ignore-missing") && $node->hasAttribute("ignore-missing") !== false) {
38 3
            $code .= " ignore missing";
39 3
        }
40 6
        if ($node->hasAttribute("with")) {
41 3
            $code .= " with " . $node->getAttribute("with");
42 3
        }
43 6
        if ($node->hasAttribute("only") && $node->getAttribute("only") !== "false") {
44 3
            $code .= " only";
45 3
        }
46
47 6
        $ext = $context->createControlNode($code);
48
49 6
        $set = iterator_to_array($node->childNodes);
50
51 6
        $n = $node->ownerDocument->createTextNode("\n");
52 6
        array_unshift($set, $n);
53 6
        array_unshift($set, $ext);
54
55 6
        $set[] = $context->createControlNode("endembed");
56
57 6
        DOMHelper::replaceWithSet($node, $set);
58 6
    }
59
}
60