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 |
||
24 | class Array2XML |
||
25 | { |
||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | private static $encoding = 'UTF-8'; |
||
30 | |||
31 | /** |
||
32 | * @var DomDocument|null |
||
33 | */ |
||
34 | private static $xml = null; |
||
35 | |||
36 | /** |
||
37 | * Convert an Array to XML. |
||
38 | * |
||
39 | * @param string $node_name - name of the root node to be converted |
||
40 | * @param array $arr - array to be converted |
||
41 | * |
||
42 | * @return DomDocument |
||
43 | */ |
||
44 | public static function createXML($node_name, $arr = []) |
||
52 | |||
53 | /** |
||
54 | * Initialize the root XML node [optional]. |
||
55 | * |
||
56 | * @param string $version |
||
57 | * @param string $encoding |
||
58 | * @param bool $standalone |
||
59 | * @param bool $format_output |
||
60 | */ |
||
61 | View Code Duplication | public static function init($version = '1.0', $encoding = 'utf-8', $standalone = false, $format_output = true) |
|
68 | |||
69 | /** |
||
70 | * Get string representation of boolean value. |
||
71 | * |
||
72 | * @param mixed $v |
||
73 | * |
||
74 | * @return string |
||
75 | */ |
||
76 | private static function bool2str($v) |
||
84 | |||
85 | /** |
||
86 | * Convert an Array to XML. |
||
87 | * |
||
88 | * @param string $node_name - name of the root node to be converted |
||
89 | * @param array $arr - array to be converted |
||
90 | * |
||
91 | * @return DOMNode |
||
92 | * |
||
93 | * @throws Exception |
||
94 | */ |
||
95 | private static function convert($node_name, $arr = []) |
||
158 | |||
159 | /** |
||
160 | * Get the root XML node, if there isn't one, create it. |
||
161 | * |
||
162 | * @return DomDocument|null |
||
163 | */ |
||
164 | private static function getXMLRoot() |
||
172 | |||
173 | /** |
||
174 | * Check if the tag name or attribute name contains illegal characters |
||
175 | * Ref: http://www.w3.org/TR/xml/#sec-common-syn. |
||
176 | * |
||
177 | * @param string $tag |
||
178 | * |
||
179 | * @return bool |
||
180 | */ |
||
181 | private static function isValidTagName($tag) |
||
187 | } |
||
188 |
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.