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 | public static function add_debugging( $text ) { |
||
47 | |||
48 | /** |
||
49 | * Return the debug output. |
||
50 | * |
||
51 | * @return string The debug output. |
||
52 | */ |
||
53 | public static function get_debugging() { |
||
59 | |||
60 | /** |
||
61 | * Convert a string with comma seperated values to an array. |
||
62 | * |
||
63 | * @todo Make $input by value. |
||
64 | * |
||
65 | * @param array|string $input The string to be converted. |
||
66 | * @return array The converted array. |
||
67 | */ |
||
68 | View Code Duplication | public static function str_to_arr( &$input ) { |
|
79 | |||
80 | /** |
||
81 | * Convert an array to a string with comma seperated values. |
||
82 | * |
||
83 | * @todo Make $input by value. |
||
84 | * |
||
85 | * @param array|string $input The array to be converted. |
||
86 | * @return string The converted string. |
||
87 | */ |
||
88 | View Code Duplication | public static function arr_to_str( &$input ) { |
|
99 | |||
100 | /** |
||
101 | * Encrypt the passed string with the passed key. |
||
102 | * |
||
103 | * @param string $input String to be encrypted. |
||
104 | * @param string $key The key used for the encryption. |
||
105 | * @return string The encrypted string. |
||
106 | */ |
||
107 | View Code Duplication | public static function encrypt( $input, $key = AUTH_KEY ) { |
|
114 | |||
115 | /** |
||
116 | * Decrypt the passed string with the passed key. |
||
117 | * |
||
118 | * @param string $input String to be decrypted. |
||
119 | * @param string $key The key used for the decryption. |
||
120 | * @return string The decrypted string. |
||
121 | */ |
||
122 | View Code Duplication | public static function decrypt( $input, $key = AUTH_KEY ) { |
|
129 | |||
130 | /** |
||
131 | * Set up and return an API connection using the currently saved options.. |
||
132 | * |
||
133 | * @return WP2D_API The API object. |
||
134 | */ |
||
135 | public static function api_quick_connect() { |
||
155 | } |
||
156 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.