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 |
||
15 | class WP2D_Helpers { |
||
16 | |||
17 | /** |
||
18 | * Debug text that get's accumulated before output. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | private static $debugging = ''; |
||
23 | |||
24 | /** |
||
25 | * Add a line to the debug output. Include the stack trace to see where it's coming from. |
||
26 | * |
||
27 | * @param string $text Text to add. |
||
28 | * |
||
29 | * @return bool |
||
30 | */ |
||
31 | public static function add_debugging( $text ) { |
||
50 | |||
51 | /** |
||
52 | * Return the debug output. |
||
53 | * |
||
54 | * @return string The debug output. |
||
55 | */ |
||
56 | public static function get_debugging() { |
||
63 | |||
64 | /** |
||
65 | * Convert a string with comma seperated values to an array. |
||
66 | * |
||
67 | * @todo Make $input by value. |
||
68 | * |
||
69 | * @param array|string $input The string to be converted. |
||
70 | * |
||
71 | * @return array The converted array. |
||
72 | */ |
||
73 | View Code Duplication | public static function str_to_arr( &$input ) { |
|
85 | |||
86 | /** |
||
87 | * Convert an array to a string with comma seperated values. |
||
88 | * |
||
89 | * @todo Make $input by value. |
||
90 | * |
||
91 | * @param array|string $input The array to be converted. |
||
92 | * |
||
93 | * @return string The converted string. |
||
94 | */ |
||
95 | View Code Duplication | public static function arr_to_str( &$input ) { |
|
107 | |||
108 | /** |
||
109 | * Encrypt the passed string with the passed key. |
||
110 | * |
||
111 | * @param string $input String to be encrypted. |
||
112 | * @param string $key The key used for the encryption. |
||
113 | * |
||
114 | * @return string The encrypted string. |
||
115 | */ |
||
116 | View Code Duplication | public static function encrypt( $input, $key = WP2D_ENC_KEY ) { |
|
124 | |||
125 | /** |
||
126 | * Decrypt the passed string with the passed key. |
||
127 | * |
||
128 | * @param string $input String to be decrypted. |
||
129 | * @param string $key The key used for the decryption. |
||
130 | * |
||
131 | * @return string The decrypted string. |
||
132 | */ |
||
133 | View Code Duplication | public static function decrypt( $input, $key = WP2D_ENC_KEY ) { |
|
141 | |||
142 | /** |
||
143 | * Set up and return an API connection using the currently saved options.. |
||
144 | * |
||
145 | * @return WP2D_API The API object. |
||
146 | */ |
||
147 | public static function api_quick_connect() { |
||
167 | } |
||
168 |
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.