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 Util 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 Util, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class Util |
||
6 | { |
||
7 | public static function stripPhpComments($str) |
||
18 | |||
19 | public static function boolToString($bool) |
||
23 | |||
24 | public static function filterSitesByName(array $sites, $filterRegex) |
||
38 | |||
39 | public static function findInArrayWithRegex(array $arr, $regex) |
||
49 | |||
50 | /** |
||
51 | * @param array $sspages should contain only objects of class SilverstripePage |
||
52 | * @param string $filterRegex |
||
53 | * @return array all SilverstripePages that match the regex |
||
54 | * |
||
55 | */ |
||
56 | View Code Duplication | public static function filterPagesByModules(array $sspages, $filterRegex) |
|
70 | |||
71 | |||
72 | /** |
||
73 | * @param array $sspages |
||
74 | * @param string $env environment type dev OR live OR test |
||
75 | * @return array all SilverstripePages that have environment_type $env |
||
76 | */ |
||
77 | View Code Duplication | public static function filterPagesByEnvironmentType($sspages, $env) { |
|
91 | |||
92 | /** |
||
93 | * @param array $sspages |
||
94 | * @param string $filterRegex environment type dev OR live OR test |
||
95 | * |
||
96 | * @return array all SilverstripePages where the version string matches $filterRegex |
||
97 | */ |
||
98 | View Code Duplication | public static function filterPagesByVersion($sspages, $filterRegex) { |
|
111 | |||
112 | /** |
||
113 | * @param SilverstripePage[] $sspages |
||
114 | * @param bool $da_val |
||
115 | * @return SilverstripePage[] |
||
116 | */ |
||
117 | public static function filterPagesByDefaultAdmin($sspages, $da_val = true) { |
||
130 | |||
131 | /** |
||
132 | * @param $version "1.1.1" "2.1.4" "2.0" |
||
133 | * |
||
134 | * @return array [1,1,1] [2,1,4] [2,0,0] |
||
135 | */ |
||
136 | public static function VersionStringsToArray($version) |
||
152 | |||
153 | public static function VersionStringsGreaterThenOrEqual($version1, $version2) |
||
165 | |||
166 | /** |
||
167 | * puts blanks at the end of a string until it reaches the minimum length. |
||
168 | * |
||
169 | * @param string $str |
||
170 | * @param int $min |
||
171 | */ |
||
172 | public static function forceStringMinLength(&$str, $min) |
||
178 | |||
179 | public static function reportError($string,$die = true) { |
||
186 | } |
||
187 |
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.