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); |
||
18 | class OptionalField extends RuleAbstract |
||
19 | { |
||
20 | const NODE_NAME = 'default'; |
||
21 | |||
22 | /** |
||
23 | * @param NodeInterface $node |
||
24 | * @param \DOMElement $domElement |
||
25 | */ |
||
26 | 9 | public function setProperty(NodeInterface $node, \DOMElement $domElement) : void |
|
27 | { |
||
28 | 9 | $element = $this->createElementFromDomNode($node, $domElement); |
|
29 | |||
30 | 9 | $node->addElement($element); |
|
31 | } |
||
32 | 9 | ||
33 | /** |
||
34 | * @param NodeInterface $node |
||
35 | * @param ElementInterface $element |
||
36 | * @param \DOMNode $domNode |
||
37 | */ |
||
38 | private function addSubElements(NodeInterface $node, ElementInterface $element, \DOMNode $domNode) : void |
||
39 | { |
||
40 | 9 | if (!$domNode->hasChildNodes() || !$this->hasSubElements($domNode)) { |
|
41 | // no elements to add |
||
42 | 9 | return; |
|
43 | } |
||
44 | 9 | ||
45 | $this->addElementsFromNodeList($node, $element, $domNode->childNodes); |
||
46 | } |
||
47 | 2 | ||
48 | 2 | /** |
|
49 | * @param NodeInterface $node |
||
50 | * @param ElementInterface $element |
||
51 | * @param \DOMNodeList $childNodeList |
||
52 | */ |
||
53 | private function addElementsFromNodeList(NodeInterface $node, ElementInterface $element, \DOMNodeList $childNodeList) : void |
||
54 | { |
||
55 | 2 | foreach ($childNodeList as $childNode) { |
|
56 | if ($childNode instanceof \DOMText) { |
||
57 | 2 | continue; |
|
58 | 2 | } |
|
59 | |||
60 | $element->addElement($this->createElementFromDomNode($node, $childNode)); |
||
61 | } |
||
62 | 2 | } |
|
63 | 2 | ||
64 | 2 | /** |
|
65 | * @param \DOMNode $domNode |
||
66 | * @return bool |
||
67 | */ |
||
68 | private function hasSubElements(\DOMNode $domNode) : bool |
||
69 | { |
||
70 | 9 | foreach ($domNode->childNodes as $childDomNode) { |
|
71 | if (!$childDomNode instanceof \DOMText) { |
||
72 | 9 | return true; |
|
73 | 9 | } |
|
74 | 2 | } |
|
75 | |||
76 | 8 | return false; |
|
77 | } |
||
78 | 8 | ||
79 | /** |
||
80 | * @param NodeInterface $node |
||
81 | * @param \DOMNode $domNode |
||
82 | * @return ElementInterface |
||
83 | */ |
||
84 | private function createElementFromDomNode(NodeInterface $node, \DOMNode $domNode) : ElementInterface |
||
97 | 9 | ||
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 | 6 | foreach ($element->getAttributes() as $name => $value) { |
|
108 | $domElement->setAttribute($name, $value); |
||
109 | 6 | } |
|
110 | 6 | ||
111 | 5 | /** @var ElementInterface $subElement */ |
|
112 | 6 | foreach ($element->getAllElements() as $subElement) { |
|
113 | $subDomElement = $domElement->ownerDocument->createElement($subElement->getName()); |
||
114 | 6 | $this->buildDomElement($subDomElement, $subElement); |
|
115 | $domElement->appendChild($subDomElement); |
||
116 | } |
||
117 | 5 | ||
118 | return $domElement; |
||
119 | 5 | } |
|
120 | |||
121 | 5 | /** |
|
122 | 1 | * @inheritDoc |
|
123 | 5 | */ |
|
124 | protected function hasValue(NodeInterface $node) : bool |
||
128 | 1 | ||
129 | 1 | /** |
|
130 | 5 | * @inheritDoc |
|
131 | */ |
||
132 | 5 | View Code Duplication | protected function addElement(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void |
|
|||
133 | { |
||
143 | } |
||
144 |
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.