Completed
Pull Request — master (#230)
by
unknown
02:43 queued 01:26
created

OptionalField   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 126
Duplicated Lines 8.73 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 58.7%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 2
dl 11
loc 126
ccs 27
cts 46
cp 0.587
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setProperty() 0 6 1
A addSubElements() 0 9 3
A addElementsFromNodeList() 0 10 3
A hasSubElements() 0 10 3
A createElementFromDomNode() 0 13 2
A buildDomElement() 0 17 3
A hasValue() 0 4 1
A addElement() 11 11 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the feed-io package.
4
 *
5
 * (c) Alexandre Debril <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace FeedIo\Rule;
12
13
use FeedIo\Feed\ElementsAwareInterface;
14
use FeedIo\Feed\NodeInterface;
15
use FeedIo\Feed\Node\ElementInterface;
16
use FeedIo\RuleAbstract;
17
18
class OptionalField extends RuleAbstract
19
{
20
    const NODE_NAME = 'default';
21
22
    /**
23
     * @param  NodeInterface $node
24
     * @param  \DOMElement   $domElement
25
     */
26 11
    public function setProperty(NodeInterface $node, \DOMElement $domElement) : void
27
    {
28 11
        $element = $this->createElementFromDomNode($node, $domElement);
29
30 11
        $node->addElement($element);
31 11
    }
32
33
    /**
34
     * @param NodeInterface $node
35
     * @param ElementInterface $element
36
     * @param \DOMNode $domNode
37
     */
38 11
    private function addSubElements(NodeInterface $node, ElementInterface $element, \DOMNode $domNode) : void
39
    {
40 11
        if (!$domNode->hasChildNodes() || !$this->hasSubElements($domNode)) {
41
            // no elements to add
42 11
            return;
43
        }
44
45 1
        $this->addElementsFromNodeList($node, $element, $domNode->childNodes);
46 1
    }
47
48
    /**
49
     * @param NodeInterface $node
50
     * @param ElementInterface $element
51
     * @param \DOMNodeList $childNodeList
52
     */
53 1
    private function addElementsFromNodeList(NodeInterface $node, ElementInterface $element, \DOMNodeList $childNodeList) : void
54
    {
55 1
        foreach ($childNodeList as $childNode) {
56 1
            if ($childNode instanceof \DOMText) {
57
                continue;
58
            }
59
60 1
            $element->addElement($this->createElementFromDomNode($node, $childNode));
61
        }
62 1
    }
63
64
    /**
65
     * @param \DOMNode $domNode
66
     * @return bool
67
     */
68 11
    private function hasSubElements(\DOMNode $domNode) : bool
69
    {
70 11
        foreach ($domNode->childNodes as $childDomNode) {
71 11
            if (!$childDomNode instanceof \DOMText) {
72 11
                return true;
73
            }
74
        }
75
76 10
        return false;
77
    }
78
79
    /**
80
     * @param NodeInterface $node
81
     * @param \DOMNode $domNode
82
     * @return ElementInterface
83
     */
84 11
    private function createElementFromDomNode(NodeInterface $node, \DOMNode $domNode) : ElementInterface
85
    {
86 11
        $element = $node->newElement();
87 11
        $element->setName($domNode->nodeName);
88 11
        $element->setValue($domNode->nodeValue);
89
90 11
        foreach ($domNode->attributes as $attribute) {
91 2
            $element->setAttribute($attribute->name, $attribute->value);
92
        }
93 11
        $this->addSubElements($node, $element, $domNode);
94
95 11
        return $element;
96
    }
97
98
    /**
99
     * @param \DomElement $domElement
100
     * @param ElementInterface $element
101
     * @return \DomElement
102
     */
103
    public function buildDomElement(\DomElement $domElement, ElementInterface $element) : \DOMElement
104
    {
105
        $domElement->nodeValue = $element->getValue();
106
107
        foreach ($element->getAttributes() as $name => $value) {
108
            $domElement->setAttribute($name, $value);
109
        }
110
111
        /** @var ElementInterface $subElement */
112
        foreach ($element->getAllElements() as $subElement) {
113
            $subDomElement = $domElement->ownerDocument->createElement($subElement->getName());
114
            $this->buildDomElement($subDomElement, $subElement);
115
            $domElement->appendChild($subDomElement);
116
        }
117
118
        return $domElement;
119
    }
120
121
    /**
122
     * @inheritDoc
123
     */
124
    protected function hasValue(NodeInterface $node) : bool
125
    {
126
        return $node instanceof ElementsAwareInterface;
127
    }
128
129
    /**
130
     * @inheritDoc
131
     */
132 View Code Duplication
    protected function addElement(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
133
    {
134
        $domElement = $document->createElement($this->getNodeName());
135
        if ($node instanceof ElementsAwareInterface) {
136
            foreach ($node->getElementIterator($this->getNodeName()) as $element) {
137
                $this->buildDomElement($domElement, $element);
138
            }
139
        }
140
141
        $rootElement->appendChild($domElement);
142
    }
143
}
144