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 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.