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:
Complex classes like ArrayPathDefinition often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ArrayPathDefinition, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | final class ArrayPathDefinition |
||
38 | { |
||
39 | /** |
||
40 | * constructor |
||
41 | * |
||
42 | * @author Dominik del Bondio <[email protected]> |
||
43 | * @since 0.11.0 |
||
44 | */ |
||
45 | private function __construct() |
||
48 | |||
49 | /** |
||
50 | * Converts the given argument to an array of parts for use in the path getter/setters |
||
51 | * @param array|string $partsArrayOrPathString The path string or an array containing the path |
||
52 | * divided into its individual parts. |
||
53 | * |
||
54 | * @return array The array of parts. |
||
55 | * |
||
56 | * @author Dominik del Bondio <[email protected]> |
||
57 | * @since 0.11.6 |
||
58 | */ |
||
59 | protected static function preparePartsArray($partsArrayOrPathString) |
||
73 | |||
74 | /** |
||
75 | * Unsets a value at the given path. |
||
76 | * |
||
77 | * @param array|string $partsArrayOrPathString The path string or an array containing the path |
||
78 | * divided into its individual parts. |
||
79 | * @param array $array The array we should operate on. |
||
80 | * |
||
81 | * @return mixed The previously stored value. |
||
82 | * |
||
83 | * @author Dominik del Bondio <[email protected]> |
||
84 | * @since 0.11.0 |
||
85 | */ |
||
86 | public static function &unsetValue($partsArrayOrPathString, array &$array) |
||
117 | |||
118 | /** |
||
119 | * Checks whether the array has a value at the given path. |
||
120 | * |
||
121 | * @param array|string $partsArrayOrPathString The path string or an array containing the path |
||
122 | * divided into its individual parts. |
||
123 | * @param array $array The array we should operate on. |
||
124 | * |
||
125 | * @return bool Whether the path exists in this array. |
||
126 | * |
||
127 | * @author Dominik del Bondio <[email protected]> |
||
128 | * @since 0.11.0 |
||
129 | */ |
||
130 | public static function hasValue($partsArrayOrPathString, array &$array) |
||
151 | |||
152 | /** |
||
153 | * Returns the value at the given path. |
||
154 | * |
||
155 | * @param array|string $partsArrayOrPathString The path string or an array containing the path |
||
156 | * divided into its individual parts. |
||
157 | * @param array $array The array we should operate on. |
||
158 | * @param mixed $default A default value if the path doesn't exist in the array. |
||
159 | * |
||
160 | * @return mixed The value stored at the given path. |
||
161 | * |
||
162 | * @author Dominik del Bondio <[email protected]> |
||
163 | * @since 0.11.0 |
||
164 | */ |
||
165 | public static function &getValue($partsArrayOrPathString, array &$array, $default = null) |
||
187 | |||
188 | /** |
||
189 | * Sets the value at the given path. |
||
190 | * |
||
191 | * @param array|string $partsArrayOrPathString The path string or an array containing the path |
||
192 | * divided into its individual parts. |
||
193 | * @param array $array The array we should operate on. |
||
194 | * @param mixed $value The value. |
||
195 | * |
||
196 | * @author Dominik del Bondio <[email protected]> |
||
197 | * @since 0.11.0 |
||
198 | */ |
||
199 | public static function setValue($partsArrayOrPathString, array &$array, $value) |
||
219 | |||
220 | /** |
||
221 | * Returns an array with the single parts of the given path. |
||
222 | * |
||
223 | * @param string $path The path. |
||
224 | * |
||
225 | * @return array The parts of the given path. |
||
226 | * |
||
227 | * @author Dominik del Bondio <[email protected]> |
||
228 | * @since 0.11.0 |
||
229 | */ |
||
230 | public static function getPartsFromPath($path) |
||
296 | |||
297 | |||
298 | /** |
||
299 | * Returns the flat key names of an array. |
||
300 | * |
||
301 | * This method calls itself recursively to flatten the keys. |
||
302 | * |
||
303 | * @param array $array The array which keys should be returned. |
||
304 | * @param string $prefix The prefix for the name (only for internal use). |
||
305 | * |
||
306 | * @return array The flattened keys. |
||
307 | * |
||
308 | * @author Dominik del Bondio <[email protected]> |
||
309 | * @since 0.11.0 |
||
310 | */ |
||
311 | View Code Duplication | public static function getFlatKeyNames(array $array, $prefix = null) |
|
335 | |||
336 | /** |
||
337 | * Returns the flattened version of an array. So the returned array |
||
338 | * will be one dimensional with the flattened key names as keys |
||
339 | * and their values from the original array as values. |
||
340 | * |
||
341 | * This method calls itself recursively to flatten the array. |
||
342 | * |
||
343 | * @param array $array The array which should be flattened. |
||
344 | * @param string $prefix The prefix for the key names (only for internal use). |
||
345 | * |
||
346 | * @return array The flattened array. |
||
347 | * |
||
348 | * @author Dominik del Bondio <[email protected]> |
||
349 | * @since 1.0.0 |
||
350 | */ |
||
351 | View Code Duplication | public static function flatten($array, $prefix = null) |
|
375 | } |
||
376 |
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.