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 InputValidation 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 InputValidation, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class InputValidation |
||
24 | { |
||
25 | /** |
||
26 | * @return string |
||
27 | */ |
||
28 | public static function displayName($displayName) |
||
38 | |||
39 | /** |
||
40 | * @return string |
||
41 | */ |
||
42 | public static function commonName($commonName) |
||
50 | |||
51 | /** |
||
52 | * @return string |
||
53 | */ |
||
54 | public static function serverCommonName($serverCommonName) |
||
62 | |||
63 | /** |
||
64 | * @return string |
||
65 | */ |
||
66 | public static function profileId($profileId) |
||
74 | |||
75 | /** |
||
76 | * @return string |
||
77 | */ |
||
78 | public static function instanceId($instanceId) |
||
86 | |||
87 | /** |
||
88 | * @return string |
||
89 | */ |
||
90 | public static function languageCode($languageCode) |
||
99 | |||
100 | /** |
||
101 | * @return string |
||
102 | */ |
||
103 | public static function totpSecret($totpSecret) |
||
111 | |||
112 | /** |
||
113 | * @return string |
||
114 | */ |
||
115 | public static function yubiKeyOtp($yubiKeyOtp) |
||
123 | |||
124 | /** |
||
125 | * @return string |
||
126 | */ |
||
127 | public static function totpKey($totpKey) |
||
135 | |||
136 | /** |
||
137 | * @return string |
||
138 | */ |
||
139 | public static function clientId($clientId) |
||
147 | |||
148 | /** |
||
149 | * @return string |
||
150 | */ |
||
151 | public static function userId($userId) |
||
159 | |||
160 | /** |
||
161 | * @return int |
||
162 | */ |
||
163 | public static function dateTime($dateTime) |
||
180 | |||
181 | /** |
||
182 | * @return string |
||
183 | */ |
||
184 | View Code Duplication | public static function ipAddress($ipAddress) |
|
193 | |||
194 | /** |
||
195 | * @return string |
||
196 | */ |
||
197 | public static function vootToken($vootToken) |
||
205 | |||
206 | /** |
||
207 | * @return string |
||
208 | */ |
||
209 | public static function ip4($ip4) |
||
217 | |||
218 | /** |
||
219 | * @return string |
||
220 | */ |
||
221 | View Code Duplication | public static function ip6($ip6) |
|
230 | |||
231 | /** |
||
232 | * @return int |
||
233 | */ |
||
234 | public static function connectedAt($connectedAt) |
||
242 | |||
243 | /** |
||
244 | * @return int |
||
245 | */ |
||
246 | public static function disconnectedAt($disconnectedAt) |
||
254 | |||
255 | /** |
||
256 | * @return int |
||
257 | */ |
||
258 | public static function bytesTransferred($bytesTransferred) |
||
266 | |||
267 | /** |
||
268 | * @return string |
||
269 | */ |
||
270 | public static function twoFactorType($twoFactorType) |
||
278 | |||
279 | /** |
||
280 | * @return string |
||
281 | */ |
||
282 | public static function twoFactorValue($twoFactorValue) |
||
290 | |||
291 | /** |
||
292 | * @return int |
||
293 | */ |
||
294 | public static function messageId($messageId) |
||
302 | |||
303 | /** |
||
304 | * @return string |
||
305 | */ |
||
306 | public static function messageType($messageType) |
||
314 | } |
||
315 |
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.