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 WC_Admin_Notices 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 WC_Admin_Notices, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class WC_Admin_Notices { |
||
19 | |||
20 | /** |
||
21 | * Array of notices - name => callback. |
||
22 | * @var array |
||
23 | */ |
||
24 | private static $core_notices = array( |
||
25 | 'install' => 'install_notice', |
||
26 | 'update' => 'update_notice', |
||
27 | 'updating' => 'updating_notice', |
||
28 | 'template_files' => 'template_file_check_notice', |
||
29 | 'theme_support' => 'theme_check_notice', |
||
30 | 'legacy_shipping' => 'legacy_shipping_notice', |
||
31 | 'no_shipping_methods' => 'no_shipping_methods_notice', |
||
32 | 'simplify_commerce' => 'simplify_commerce_notice', |
||
33 | ); |
||
34 | |||
35 | /** |
||
36 | * Stores notices. |
||
37 | * @var array |
||
38 | */ |
||
39 | private static $notices = array(); |
||
40 | |||
41 | /** |
||
42 | * Constructor. |
||
43 | */ |
||
44 | public static function init() { |
||
56 | |||
57 | /** |
||
58 | * Store notices to DB |
||
59 | */ |
||
60 | public static function store_notices() { |
||
63 | |||
64 | /** |
||
65 | * Get notices |
||
66 | * @return array |
||
67 | */ |
||
68 | public static function get_notices() { |
||
71 | |||
72 | /** |
||
73 | * Remove all notices. |
||
74 | */ |
||
75 | public static function remove_all_notices() { |
||
78 | |||
79 | /** |
||
80 | * Reset notices for themes when switched or a new version of WC is installed. |
||
81 | */ |
||
82 | public static function reset_admin_notices() { |
||
95 | |||
96 | /** |
||
97 | * Show a notice. |
||
98 | * @param string $name |
||
99 | */ |
||
100 | public static function add_notice( $name ) { |
||
103 | |||
104 | /** |
||
105 | * Remove a notice from being displayed. |
||
106 | * @param string $name |
||
107 | */ |
||
108 | public static function remove_notice( $name ) { |
||
112 | |||
113 | /** |
||
114 | * See if a notice is being shown. |
||
115 | * @param string $name |
||
116 | * @return boolean |
||
117 | */ |
||
118 | public static function has_notice( $name ) { |
||
121 | |||
122 | /** |
||
123 | * Hide a notice if the GET variable is set. |
||
124 | */ |
||
125 | public static function hide_notices() { |
||
140 | |||
141 | /** |
||
142 | * Add notices + styles if needed. |
||
143 | */ |
||
144 | public static function add_notices() { |
||
158 | |||
159 | /** |
||
160 | * Add a custom notice. |
||
161 | * @param string $name |
||
162 | * @param string $notice_html |
||
163 | */ |
||
164 | public static function add_custom_notice( $name, $notice_html ) { |
||
168 | |||
169 | /** |
||
170 | * Output any stored custom notices. |
||
171 | */ |
||
172 | public static function output_custom_notices() { |
||
187 | |||
188 | /** |
||
189 | * If we need to update, include a message with the update button. |
||
190 | */ |
||
191 | public static function update_notice() { |
||
194 | |||
195 | /** |
||
196 | * If we are updating, show progress. |
||
197 | */ |
||
198 | public static function updating_notice() { |
||
205 | |||
206 | /** |
||
207 | * If we have just installed, show a message with the install pages button. |
||
208 | */ |
||
209 | public static function install_notice() { |
||
212 | |||
213 | /** |
||
214 | * Show the Theme Check notice. |
||
215 | */ |
||
216 | public static function theme_check_notice() { |
||
223 | |||
224 | /** |
||
225 | * Show a notice highlighting bad template files. |
||
226 | */ |
||
227 | public static function template_file_check_notice() { |
||
261 | |||
262 | /** |
||
263 | * Show a notice asking users to convert to shipping zones. |
||
264 | */ |
||
265 | public static function legacy_shipping_notice() { |
||
282 | |||
283 | /** |
||
284 | * No shipping methods. |
||
285 | */ |
||
286 | public static function no_shipping_methods_notice() { |
||
302 | |||
303 | /** |
||
304 | * Simplify Commerce is being removed from core. |
||
305 | */ |
||
306 | public static function simplify_commerce_notice() { |
||
315 | } |
||
316 | |||
318 |
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.