DOMHelper::checkNamespaces()   B
last analyzed

Complexity

Conditions 10
Paths 18

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 10.5498

Importance

Changes 0
Metric Value
cc 10
nc 18
nop 2
dl 0
loc 19
ccs 14
cts 17
cp 0.8235
crap 10.5498
rs 7.6666
c 0
b 0
f 0

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\Helper;
3
4
/**
5
 *
6
 * @author Asmir Mustafic <[email protected]>
7
 *
8
 */
9
class DOMHelper
10
{
11 4
    public static function removeChilds(\DOMNode $ref)
12
    {
13 4
        while ($ref->hasChildNodes()) {
14 4
            $ref->removeChild($ref->firstChild);
15 4
        }
16 4
    }
17
18 60
    public static function insertAfterSet(\DOMNode $node, array $newNodes)
19
    {
20 60
        $ref = $node;
21 60
        foreach ($newNodes as $newNode) {
22 60
            if ($newNode->parentNode) {
23 54
                $newNode->parentNode->removeChild($newNode);
24 54
            }
25 60
            $ref->parentNode->insertBefore($newNode, $ref->nextSibling);
26 60
            $ref = $newNode;
27 60
        }
28 60
    }
29
30 60
    public static function replaceWithSet(\DOMNode $node, array $newNodes)
31
    {
32 60
        self::insertAfterSet($node, $newNodes);
33 60
        $node->parentNode->removeChild($node);
34 60
    }
35
36
    public static function remove(\DOMNode $ref)
37
    {
38
        return $ref->parentNode->removeChild($ref);
39
    }
40
41 24
    public static function checkNamespaces(\DOMElement $element, array $namespaces = array())
42
    {
43 24
        if ($element->namespaceURI === null && preg_match('/^([a-z0-9\-]+):(.+)$/i', $element->nodeName, $mch) && isset($namespaces[$mch[1]])) {
44
            $oldElement = $element;
45
            $element = self::copyElementInNs($oldElement, $namespaces[$mch[1]]);
46
        }
47
        // fix attrs
48 24
        foreach (iterator_to_array($element->attributes) as $attr) {
49 11
            if ($attr->namespaceURI === null && preg_match('/^([a-z0-9\-]+):/i', $attr->name, $mch) && isset($namespaces[$mch[1]])) {
50 1
                $element->removeAttributeNode($attr);
51 1
                $element->setAttributeNS($namespaces[$mch[1]], $attr->name, $attr->value);
52 1
            }
53 24
        }
54 24
        foreach (iterator_to_array($element->childNodes) as $child) {
55 24
            if ($child instanceof \DOMElement) {
56 3
                self::checkNamespaces($child, $namespaces);
57 3
            }
58 24
        }
59 24
    }
60
61
    /**
62
     * @param \DOMElement $oldElement
63
     * @param string $newNamespace
64
     * @return mixed
65
     */
66
    public static function copyElementInNs($oldElement, $newNamespace)
67
    {
68
        $element = $oldElement->ownerDocument->createElementNS($newNamespace, $oldElement->nodeName);
69
70
        // copy attributes
71
        foreach (iterator_to_array($oldElement->attributes) as $attr) {
72
            $oldElement->removeAttributeNode($attr);
73
            if ($attr->namespaceURI) {
74
                $element->setAttributeNodeNS($attr);
75
            } else {
76
                $element->setAttributeNode($attr);
77
            }
78
        }
79
        // copy children
80
        while ($child = $oldElement->firstChild) {
81
            $oldElement->removeChild($child);
82
            $element->appendChild($child);
83
        }
84
        $oldElement->parentNode->replaceChild($element, $oldElement);
85
86
        return $element;
87
    }
88
}
89