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 |
|
32 | |||
33 | /** |
||
34 | * @param NodeInterface $node |
||
35 | * @param ElementInterface $element |
||
36 | * @param \DOMNode $domNode |
||
37 | */ |
||
38 | 9 | private function addSubElements(NodeInterface $node, ElementInterface $element, \DOMNode $domNode) : void |
|
47 | |||
48 | /** |
||
49 | * @param NodeInterface $node |
||
50 | * @param ElementInterface $element |
||
51 | * @param \DOMNodeList $childNodeList |
||
52 | */ |
||
53 | 2 | private function addElementsFromNodeList(NodeInterface $node, ElementInterface $element, \DOMNodeList $childNodeList) : void |
|
63 | |||
64 | /** |
||
65 | * @param \DOMNode $domNode |
||
66 | * @return bool |
||
67 | */ |
||
68 | 9 | private function hasSubElements(\DOMNode $domNode) : bool |
|
78 | |||
79 | /** |
||
80 | * @param NodeInterface $node |
||
81 | * @param \DOMNode $domNode |
||
82 | * @return ElementInterface |
||
83 | */ |
||
84 | 9 | private function createElementFromDomNode(NodeInterface $node, \DOMNode $domNode) : ElementInterface |
|
97 | |||
98 | /** |
||
99 | * @param \DomElement $domElement |
||
100 | * @param ElementInterface $element |
||
101 | * @return \DomElement |
||
102 | */ |
||
103 | 5 | public function buildDomElement(\DomElement $domElement, ElementInterface $element) : \DOMElement |
|
120 | |||
121 | /** |
||
122 | * @inheritDoc |
||
123 | */ |
||
124 | 6 | protected function hasValue(NodeInterface $node) : bool |
|
128 | |||
129 | /** |
||
130 | * @inheritDoc |
||
131 | */ |
||
132 | 6 | View Code Duplication | protected function addElement(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void |
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.