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 |
||
11 | class RecursiveAccessor |
||
12 | { |
||
13 | /** |
||
14 | * @param array $array |
||
15 | * @param string $path |
||
16 | * @return mixed |
||
17 | * @throws \InvalidArgumentException |
||
18 | */ |
||
19 | 36 | View Code Duplication | public static function readSingleFromPath($array, $path) |
41 | |||
42 | 63 | private static function isInvalidPath($path) |
|
46 | |||
47 | 63 | private static function isValidPath($path) |
|
51 | |||
52 | /** |
||
53 | * @param array $array |
||
54 | * @param string $path |
||
55 | */ |
||
56 | 9 | public static function unsetInPath(&$array, $path) |
|
72 | |||
73 | /** |
||
74 | * @param array $array |
||
75 | * @param string $path |
||
76 | * @param string $type |
||
77 | * @return null |
||
78 | */ |
||
79 | 12 | View Code Duplication | public static function castInPath(&$array, $path, $type) |
100 | |||
101 | /** |
||
102 | * The writeNestedValue function is simply (over)writing data to arrays, this one tries to merge data when possible |
||
103 | * |
||
104 | * @param array $array |
||
105 | * @param string $path |
||
106 | * @param mixed $value |
||
107 | */ |
||
108 | 30 | public static function integrateIntoPath(&$array, $path, $value) |
|
134 | |||
135 | /** |
||
136 | * @param array $array |
||
137 | * @param string $path |
||
138 | * @param mixed $value |
||
139 | */ |
||
140 | 6 | public static function writeToPath(&$array, $path, $value) |
|
156 | } |
||
157 |
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.