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 |
||
5 | class ArrayUtils extends UtilsBase |
||
6 | { |
||
7 | const NO_PRESERVE_KEYS = 0; |
||
8 | const PRESERVE_KEYS = 1; |
||
9 | const PRESERVE_ASSOCIATIVE_KEYS = 2; |
||
10 | |||
11 | /** |
||
12 | * @param array $toFlat |
||
13 | * @param int $preserveKeys |
||
14 | * |
||
15 | * @return array |
||
16 | */ |
||
17 | public function flatten(array $toFlat, $preserveKeys = self::NO_PRESERVE_KEYS) |
||
29 | |||
30 | /** |
||
31 | * @param array $data |
||
32 | * |
||
33 | * @return string |
||
34 | */ |
||
35 | public function toXmlString(array $data) |
||
43 | |||
44 | /** |
||
45 | * @param array $arrayToParseAsXml |
||
46 | * |
||
47 | * @return \SimpleXMLElement |
||
48 | */ |
||
49 | public function toXml(array $arrayToParseAsXml) |
||
53 | |||
54 | View Code Duplication | private function flattenPreservingKeys(array $toFlat) |
|
68 | |||
69 | View Code Duplication | private function flattenNotPreservingKeys(array $toFlat) |
|
83 | |||
84 | private function flattenPreservingAssociativeKeys(array $toFlat) |
||
100 | |||
101 | private function parseAsXml(array $data, $inheritedKey = null) |
||
121 | |||
122 | private function parseXmlKeyValue($key, $value = null) |
||
130 | |||
131 | private function isAssociative(array $data) |
||
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.