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_Status 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_Status, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class WC_Admin_Status { |
||
15 | |||
16 | /** |
||
17 | * Handles output of the reports page in admin. |
||
18 | */ |
||
19 | public static function output() { |
||
22 | |||
23 | /** |
||
24 | * Handles output of report. |
||
25 | */ |
||
26 | public static function status_report() { |
||
29 | |||
30 | /** |
||
31 | * Handles output of tools. |
||
32 | */ |
||
33 | public static function status_tools() { |
||
83 | |||
84 | /** |
||
85 | * Get tools. |
||
86 | * |
||
87 | * @return array of tools |
||
88 | */ |
||
89 | public static function get_tools() { |
||
93 | |||
94 | /** |
||
95 | * Show the logs page. |
||
96 | */ |
||
97 | public static function status_logs() { |
||
104 | |||
105 | /** |
||
106 | * Show the log page contents for file log handler. |
||
107 | */ |
||
108 | public static function status_logs_file() { |
||
109 | $logs = self::scan_log_files(); |
||
110 | |||
111 | if ( ! empty( $_REQUEST['log_file'] ) && isset( $logs[ sanitize_title( wp_unslash( $_REQUEST['log_file'] ) ) ] ) ) { // WPCS: input var ok, CSRF ok. |
||
112 | $viewed_log = $logs[ sanitize_title( wp_unslash( $_REQUEST['log_file'] ) ) ]; // WPCS: input var ok, CSRF ok. |
||
113 | } elseif ( ! empty( $logs ) ) { |
||
114 | $viewed_log = current( $logs ); |
||
115 | } |
||
116 | |||
117 | $handle = ! empty( $viewed_log ) ? self::get_log_file_handle( $viewed_log ) : ''; |
||
|
|||
118 | |||
119 | if ( ! empty( $_REQUEST['handle'] ) ) { // WPCS: input var ok, CSRF ok. |
||
120 | self::remove_log(); |
||
121 | } |
||
122 | |||
123 | include_once 'views/html-admin-page-status-logs.php'; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Show the log page contents for db log handler. |
||
128 | */ |
||
129 | public static function status_logs_db() { |
||
130 | if ( ! empty( $_REQUEST['flush-logs'] ) ) { // WPCS: input var ok, CSRF ok. |
||
131 | self::flush_db_logs(); |
||
132 | } |
||
133 | |||
134 | if ( isset( $_REQUEST['action'] ) && isset( $_REQUEST['log'] ) ) { // WPCS: input var ok, CSRF ok. |
||
135 | self::log_table_bulk_actions(); |
||
136 | } |
||
137 | |||
138 | $log_table_list = new WC_Admin_Log_Table_List(); |
||
139 | $log_table_list->prepare_items(); |
||
140 | |||
141 | include_once 'views/html-admin-page-status-logs-db.php'; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Retrieve metadata from a file. Based on WP Core's get_file_data function. |
||
146 | * |
||
147 | * @since 2.1.1 |
||
148 | * @param string $file Path to the file. |
||
149 | * @return string |
||
150 | */ |
||
151 | public static function get_file_version( $file ) { |
||
177 | |||
178 | /** |
||
179 | * Return the log file handle. |
||
180 | * |
||
181 | * @param string $filename Filename to get the handle for. |
||
182 | * @return string |
||
183 | */ |
||
184 | public static function get_log_file_handle( $filename ) { |
||
187 | |||
188 | /** |
||
189 | * Scan the template files. |
||
190 | * |
||
191 | * @param string $template_path Path to the template directory. |
||
192 | * @return array |
||
193 | */ |
||
194 | public static function scan_template_files( $template_path ) { |
||
217 | |||
218 | /** |
||
219 | * Scan the log files. |
||
220 | * |
||
221 | * @return array |
||
222 | */ |
||
223 | public static function scan_log_files() { |
||
226 | |||
227 | /** |
||
228 | * Get latest version of a theme by slug. |
||
229 | * |
||
230 | * @param object $theme WP_Theme object. |
||
231 | * @return string Version number if found. |
||
232 | */ |
||
233 | public static function get_latest_theme_version( $theme ) { |
||
285 | |||
286 | /** |
||
287 | * Remove/delete the chosen file. |
||
288 | */ |
||
289 | public static function remove_log() { |
||
302 | |||
303 | /** |
||
304 | * Clear DB log table. |
||
305 | * |
||
306 | * @since 3.0.0 |
||
307 | */ |
||
308 | private static function flush_db_logs() { |
||
309 | View Code Duplication | if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'woocommerce-status-logs' ) ) { // WPCS: input var ok, sanitization ok. |
|
310 | wp_die( esc_html__( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) ); |
||
311 | } |
||
312 | |||
313 | WC_Log_Handler_DB::flush(); |
||
314 | |||
315 | wp_safe_redirect( esc_url_raw( admin_url( 'admin.php?page=wc-status&tab=logs' ) ) ); |
||
316 | exit(); |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * Bulk DB log table actions. |
||
321 | * |
||
322 | * @since 3.0.0 |
||
323 | */ |
||
324 | private static function log_table_bulk_actions() { |
||
325 | View Code Duplication | if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'woocommerce-status-logs' ) ) { // WPCS: input var ok, sanitization ok. |
|
326 | wp_die( esc_html__( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) ); |
||
327 | } |
||
328 | |||
329 | $log_ids = array_map( 'absint', (array) isset( $_REQUEST['log'] ) ? wp_unslash( $_REQUEST['log'] ) : array() ); // WPCS: input var ok, sanitization ok. |
||
330 | |||
331 | if ( ( isset( $_REQUEST['action'] ) && 'delete' === $_REQUEST['action'] ) || ( isset( $_REQUEST['action2'] ) && 'delete' === $_REQUEST['action2'] ) ) { // WPCS: input var ok, sanitization ok. |
||
332 | WC_Log_Handler_DB::delete( $log_ids ); |
||
333 | wp_safe_redirect( esc_url_raw( admin_url( 'admin.php?page=wc-status&tab=logs' ) ) ); |
||
334 | exit(); |
||
335 | } |
||
336 | } |
||
337 | } |
||
338 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.