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 TextData 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 TextData, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class TextData |
||
28 | { |
||
29 | private static $invalidChars; |
||
30 | |||
31 | 15 | private static function unicodeToOrd($character) |
|
35 | |||
36 | /** |
||
37 | * CHARACTER. |
||
38 | * |
||
39 | * @param string $character Value |
||
40 | * |
||
41 | * @return string |
||
42 | */ |
||
43 | 11 | public static function CHARACTER($character) |
|
57 | |||
58 | /** |
||
59 | * TRIMNONPRINTABLE. |
||
60 | * |
||
61 | * @param mixed $stringValue Value to check |
||
62 | * |
||
63 | * @return string |
||
64 | */ |
||
65 | 5 | public static function TRIMNONPRINTABLE($stringValue = '') |
|
83 | |||
84 | /** |
||
85 | * TRIMSPACES. |
||
86 | * |
||
87 | * @param mixed $stringValue Value to check |
||
88 | * |
||
89 | * @return string |
||
90 | */ |
||
91 | 7 | public static function TRIMSPACES($stringValue = '') |
|
104 | |||
105 | /** |
||
106 | * ASCIICODE. |
||
107 | * |
||
108 | * @param string $characters Value |
||
109 | * |
||
110 | * @return int |
||
111 | */ |
||
112 | 17 | public static function ASCIICODE($characters) |
|
133 | |||
134 | /** |
||
135 | * CONCATENATE. |
||
136 | * |
||
137 | * @return string |
||
138 | */ |
||
139 | 3 | public static function CONCATENATE(...$args) |
|
158 | |||
159 | /** |
||
160 | * DOLLAR. |
||
161 | * |
||
162 | * This function converts a number to text using currency format, with the decimals rounded to the specified place. |
||
163 | * The format used is $#,##0.00_);($#,##0.00).. |
||
164 | * |
||
165 | * @param float $value The value to format |
||
166 | * @param int $decimals The number of digits to display to the right of the decimal point. |
||
167 | * If decimals is negative, number is rounded to the left of the decimal point. |
||
168 | * If you omit decimals, it is assumed to be 2 |
||
169 | * |
||
170 | * @return string |
||
171 | */ |
||
172 | 6 | public static function DOLLAR($value = 0, $decimals = 2) |
|
196 | |||
197 | /** |
||
198 | * SEARCHSENSITIVE. |
||
199 | * |
||
200 | * @param string $needle The string to look for |
||
201 | * @param string $haystack The string in which to look |
||
202 | * @param int $offset Offset within $haystack |
||
203 | * |
||
204 | * @return string |
||
205 | */ |
||
206 | 13 | View Code Duplication | public static function SEARCHSENSITIVE($needle, $haystack, $offset = 1) |
231 | |||
232 | /** |
||
233 | * SEARCHINSENSITIVE. |
||
234 | * |
||
235 | * @param string $needle The string to look for |
||
236 | * @param string $haystack The string in which to look |
||
237 | * @param int $offset Offset within $haystack |
||
238 | * |
||
239 | * @return string |
||
240 | */ |
||
241 | 11 | View Code Duplication | public static function SEARCHINSENSITIVE($needle, $haystack, $offset = 1) |
266 | |||
267 | /** |
||
268 | * FIXEDFORMAT. |
||
269 | * |
||
270 | * @param mixed $value Value to check |
||
271 | * @param int $decimals |
||
272 | * @param bool $no_commas |
||
273 | * |
||
274 | * @return string |
||
275 | */ |
||
276 | 5 | public static function FIXEDFORMAT($value, $decimals = 2, $no_commas = false) |
|
298 | |||
299 | /** |
||
300 | * LEFT. |
||
301 | * |
||
302 | * @param string $value Value |
||
303 | * @param int $chars Number of characters |
||
304 | * |
||
305 | * @return string |
||
306 | */ |
||
307 | 11 | View Code Duplication | public static function LEFT($value = '', $chars = 1) |
322 | |||
323 | /** |
||
324 | * MID. |
||
325 | * |
||
326 | * @param string $value Value |
||
327 | * @param int $start Start character |
||
328 | * @param int $chars Number of characters |
||
329 | * |
||
330 | * @return string |
||
331 | */ |
||
332 | 9 | public static function MID($value = '', $start = 1, $chars = null) |
|
352 | |||
353 | /** |
||
354 | * RIGHT. |
||
355 | * |
||
356 | * @param string $value Value |
||
357 | * @param int $chars Number of characters |
||
358 | * |
||
359 | * @return string |
||
360 | */ |
||
361 | 11 | View Code Duplication | public static function RIGHT($value = '', $chars = 1) |
376 | |||
377 | /** |
||
378 | * STRINGLENGTH. |
||
379 | * |
||
380 | * @param string $value Value |
||
381 | * |
||
382 | * @return int |
||
383 | */ |
||
384 | 11 | View Code Duplication | public static function STRINGLENGTH($value = '') |
394 | |||
395 | /** |
||
396 | * LOWERCASE. |
||
397 | * |
||
398 | * Converts a string value to upper case. |
||
399 | * |
||
400 | * @param string $mixedCaseString |
||
401 | * |
||
402 | * @return string |
||
403 | */ |
||
404 | 4 | View Code Duplication | public static function LOWERCASE($mixedCaseString) |
414 | |||
415 | /** |
||
416 | * UPPERCASE. |
||
417 | * |
||
418 | * Converts a string value to upper case. |
||
419 | * |
||
420 | * @param string $mixedCaseString |
||
421 | * |
||
422 | * @return string |
||
423 | */ |
||
424 | 4 | View Code Duplication | public static function UPPERCASE($mixedCaseString) |
434 | |||
435 | /** |
||
436 | * PROPERCASE. |
||
437 | * |
||
438 | * Converts a string value to upper case. |
||
439 | * |
||
440 | * @param string $mixedCaseString |
||
441 | * |
||
442 | * @return string |
||
443 | */ |
||
444 | 3 | View Code Duplication | public static function PROPERCASE($mixedCaseString) |
454 | |||
455 | /** |
||
456 | * REPLACE. |
||
457 | * |
||
458 | * @param string $oldText String to modify |
||
459 | * @param int $start Start character |
||
460 | * @param int $chars Number of characters |
||
461 | * @param string $newText String to replace in defined position |
||
462 | * |
||
463 | * @return string |
||
464 | */ |
||
465 | 5 | public static function REPLACE($oldText, $start, $chars, $newText) |
|
477 | |||
478 | /** |
||
479 | * SUBSTITUTE. |
||
480 | * |
||
481 | * @param string $text Value |
||
482 | * @param string $fromText From Value |
||
483 | * @param string $toText To Value |
||
484 | * @param int $instance Instance Number |
||
485 | * |
||
486 | * @return string |
||
487 | */ |
||
488 | 4 | public static function SUBSTITUTE($text = '', $fromText = '', $toText = '', $instance = 0) |
|
514 | |||
515 | /** |
||
516 | * RETURNSTRING. |
||
517 | * |
||
518 | * @param mixed $testValue Value to check |
||
519 | * |
||
520 | * @return string|null |
||
521 | */ |
||
522 | 5 | public static function RETURNSTRING($testValue = '') |
|
532 | |||
533 | /** |
||
534 | * TEXTFORMAT. |
||
535 | * |
||
536 | * @param mixed $value Value to check |
||
537 | * @param string $format Format mask to use |
||
538 | * |
||
539 | * @return string |
||
540 | */ |
||
541 | 13 | public static function TEXTFORMAT($value, $format) |
|
552 | |||
553 | /** |
||
554 | * VALUE. |
||
555 | * |
||
556 | * @param mixed $value Value to check |
||
557 | * |
||
558 | * @return bool |
||
559 | */ |
||
560 | 10 | public static function VALUE($value = '') |
|
598 | } |
||
599 |
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.