Completed
Pull Request — project/php-7 (#133)
by Alex
04:04 queued 01:49
created

OptionalField   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 124
Duplicated Lines 8.87 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 97.83%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 19
c 3
b 0
f 0
lcom 1
cbo 2
dl 11
loc 124
ccs 45
cts 46
cp 0.9783
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setProperty() 0 8 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
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
     * @return $this
26
     */
27 9
    public function setProperty(NodeInterface $node, \DOMElement $domElement)
28
    {
29 9
        $element = $this->createElementFromDomNode($node, $domElement);
30
31 9
        $node->addElement($element);
32
33 9
        return $this;
34
    }
35
36
    /**
37
     * @param NodeInterface $node
38
     * @param ElementInterface $element
39
     * @param \DOMNode $domNode
40
     */
41 9
    private function addSubElements(NodeInterface $node, ElementInterface $element, \DOMNode $domNode)
42
    {
43 9
        if (!$domNode->hasChildNodes() || !$this->hasSubElements($domNode)) {
44
            // no elements to add
45 9
            return;
46
        }
47
48 2
        $this->addElementsFromNodeList($node, $element, $domNode->childNodes);
49 2
    }
50
51
    /**
52
     * @param NodeInterface $node
53
     * @param ElementInterface $element
54
     * @param \DOMNodeList $childNodeList
55
     */
56 2
    private function addElementsFromNodeList(NodeInterface $node, ElementInterface $element, \DOMNodeList $childNodeList)
57
    {
58 2
        foreach ($childNodeList as $childNode) {
59 2
            if ($childNode instanceof \DOMText) {
60
                continue;
61
            }
62
63 2
            $element->addElement($this->createElementFromDomNode($node, $childNode));
64
        }
65 2
    }
66
67
    /**
68
     * @param \DOMNode $domNode
69
     * @return bool
70
     */
71 9
    private function hasSubElements(\DOMNode $domNode)
72
    {
73 9
        foreach ($domNode->childNodes as $childDomNode) {
74 9
            if (!$childDomNode instanceof \DOMText) {
75 9
                return true;
76
            }
77
        }
78
79 8
        return false;
80
    }
81
82
    /**
83
     * @param NodeInterface $node
84
     * @param \DOMNode $domNode
85
     * @return ElementInterface
86
     */
87 9
    private function createElementFromDomNode(NodeInterface $node, \DOMNode $domNode)
88
    {
89 9
        $element = $node->newElement();
90 9
        $element->setName($domNode->nodeName);
91 9
        $element->setValue($domNode->nodeValue);
92
93 9
        foreach ($domNode->attributes as $attribute) {
94 3
            $element->setAttribute($attribute->name, $attribute->value);
95
        }
96 9
        $this->addSubElements($node, $element, $domNode);
97
98 9
        return $element;
99
    }
100
101 5
    public function buildDomElement(\DomElement $domElement, ElementInterface $element)
102
    {
103 5
        $domElement->nodeValue = $element->getValue();
104
105 5
        foreach ($element->getAttributes() as $name => $value) {
106 1
            $domElement->setAttribute($name, $value);
107
        }
108
109
        /** @var ElementInterface $subElement */
110 5
        foreach ($element->getAllElements() as $subElement) {
111 1
            $subDomElement = $domElement->ownerDocument->createElement($subElement->getName());
112 1
            $this->buildDomElement($subDomElement, $subElement);
113 1
            $domElement->appendChild($subDomElement);
114
        }
115
116 5
        return $domElement;
117
    }
118
119
    /**
120
     * @inheritDoc
121
     */
122 6
    protected function hasValue(NodeInterface $node) : bool
123
    {
124 6
        return $node instanceof ElementsAwareInterface;
125
    }
126
127
    /**
128
     * @inheritDoc
129
     */
130 6 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...
131
    {
132 6
        $domElement = $document->createElement($this->getNodeName());
133 6
        if ($node instanceof ElementsAwareInterface) {
134 6
            foreach ($node->getElementIterator($this->getNodeName()) as $element) {
135 5
                $this->buildDomElement($domElement, $element);
136
            }
137
        }
138
139 6
        $rootElement->appendChild($domElement);
140 6
    }
141
}
142