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 | * Stores notices. |
||
22 | * @var array |
||
23 | */ |
||
24 | private static $notices = array(); |
||
25 | |||
26 | /** |
||
27 | * Array of notices - name => callback. |
||
28 | * @var array |
||
29 | */ |
||
30 | private static $core_notices = array( |
||
31 | 'install' => 'install_notice', |
||
32 | 'update' => 'update_notice', |
||
33 | 'updating' => 'updating_notice', |
||
34 | 'template_files' => 'template_file_check_notice', |
||
35 | 'theme_support' => 'theme_check_notice', |
||
36 | 'legacy_shipping' => 'legacy_shipping_notice', |
||
37 | 'no_shipping_methods' => 'no_shipping_methods_notice', |
||
38 | 'simplify_commerce' => 'simplify_commerce_notice', |
||
39 | ); |
||
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() { |
||
83 | View Code Duplication | if ( ! current_theme_supports( 'woocommerce' ) && ! in_array( get_option( 'template' ), wc_get_core_supported_themes() ) ) { |
|
|
|||
84 | self::add_notice( 'theme_support' ); |
||
85 | } |
||
86 | |||
87 | $simplify_options = get_option( 'woocommerce_simplify_commerce_settings', array() ); |
||
88 | $location = wc_get_base_location(); |
||
89 | |||
90 | if ( ! class_exists( 'WC_Gateway_Simplify_Commerce_Loader' ) && ! empty( $simplify_options['enabled'] ) && 'yes' === $simplify_options['enabled'] && in_array( $location['country'], apply_filters( 'woocommerce_gateway_simplify_commerce_supported_countries', array( 'US', 'IE' ) ) ) ) { |
||
91 | WC_Admin_Notices::add_notice( 'simplify_commerce' ); |
||
92 | } |
||
93 | |||
94 | self::add_notice( 'template_files' ); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Show a notice. |
||
99 | * @param string $name |
||
100 | */ |
||
101 | public static function add_notice( $name ) { |
||
104 | |||
105 | /** |
||
106 | * Remove a notice from being displayed. |
||
107 | * @param string $name |
||
108 | */ |
||
109 | public static function remove_notice( $name ) { |
||
113 | |||
114 | /** |
||
115 | * See if a notice is being shown. |
||
116 | * @param string $name |
||
117 | * @return boolean |
||
118 | */ |
||
119 | public static function has_notice( $name ) { |
||
122 | |||
123 | /** |
||
124 | * Hide a notice if the GET variable is set. |
||
125 | */ |
||
126 | public static function hide_notices() { |
||
141 | |||
142 | /** |
||
143 | * Add notices + styles if needed. |
||
144 | */ |
||
145 | public static function add_notices() { |
||
159 | |||
160 | /** |
||
161 | * Add a custom notice. |
||
162 | * @param string $name |
||
163 | * @param string $notice_html |
||
164 | */ |
||
165 | public static function add_custom_notice( $name, $notice_html ) { |
||
169 | |||
170 | /** |
||
171 | * Output any stored custom notices. |
||
172 | */ |
||
173 | public static function output_custom_notices() { |
||
188 | |||
189 | /** |
||
190 | * If we need to update, include a message with the update button. |
||
191 | */ |
||
192 | public static function update_notice() { |
||
195 | |||
196 | /** |
||
197 | * If we are updating, show progress. |
||
198 | */ |
||
199 | public static function updating_notice() { |
||
206 | |||
207 | /** |
||
208 | * If we have just installed, show a message with the install pages button. |
||
209 | */ |
||
210 | public static function install_notice() { |
||
213 | |||
214 | /** |
||
215 | * Show the Theme Check notice. |
||
216 | */ |
||
217 | public static function theme_check_notice() { |
||
224 | |||
225 | /** |
||
226 | * Show a notice highlighting bad template files. |
||
227 | */ |
||
228 | public static function template_file_check_notice() { |
||
262 | |||
263 | /** |
||
264 | * Show a notice asking users to convert to shipping zones. |
||
265 | */ |
||
266 | public static function legacy_shipping_notice() { |
||
283 | |||
284 | /** |
||
285 | * No shipping methods. |
||
286 | */ |
||
287 | public static function no_shipping_methods_notice() { |
||
303 | |||
304 | /** |
||
305 | * Simplify Commerce is being removed from core. |
||
306 | */ |
||
307 | public static function simplify_commerce_notice() { |
||
318 | } |
||
319 | |||
321 |
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.