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 Jetpack_JSON_API_Plugins_Endpoint 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 Jetpack_JSON_API_Plugins_Endpoint, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | abstract class Jetpack_JSON_API_Plugins_Endpoint extends Jetpack_JSON_API_Endpoint { |
||
10 | |||
11 | protected $plugins = array(); |
||
12 | |||
13 | protected $network_wide = false; |
||
14 | |||
15 | protected $bulk = true; |
||
16 | protected $log; |
||
17 | |||
18 | static $_response_format = array( |
||
19 | 'id' => '(safehtml) The plugin\'s ID', |
||
20 | 'slug' => '(safehtml) The plugin\'s .org slug', |
||
21 | 'active' => '(boolean) The plugin status.', |
||
22 | 'update' => '(object) The plugin update info.', |
||
23 | 'name' => '(safehtml) The name of the plugin.', |
||
24 | 'plugin_url' => '(url) Link to the plugin\'s web site.', |
||
25 | 'version' => '(safehtml) The plugin version number.', |
||
26 | 'description' => '(safehtml) Description of what the plugin does and/or notes from the author', |
||
27 | 'author' => '(safehtml) The author\'s name', |
||
28 | 'author_url' => '(url) The authors web site address', |
||
29 | 'network' => '(boolean) Whether the plugin can only be activated network wide.', |
||
30 | 'autoupdate' => '(boolean) Whether the plugin is automatically updated', |
||
31 | 'autoupdate_translation' => '(boolean) Whether the plugin is automatically updating translations', |
||
32 | 'next_autoupdate' => '(string) Y-m-d H:i:s for next scheduled update event', |
||
33 | 'log' => '(array:safehtml) An array of update log strings.', |
||
34 | 'uninstallable' => '(boolean) Whether the plugin is unistallable.', |
||
35 | 'action_links' => '(array) An array of action links that the plugin uses.', |
||
36 | ); |
||
37 | |||
38 | static $_response_format_v1_2 = array( |
||
39 | 'slug' => '(safehtml) The plugin\'s .org slug', |
||
40 | 'active' => '(boolean) The plugin status.', |
||
41 | 'update' => '(object) The plugin update info.', |
||
42 | 'name' => '(safehtml) The plugin\'s ID', |
||
43 | 'display_name' => '(safehtml) The name of the plugin.', |
||
44 | 'version' => '(safehtml) The plugin version number.', |
||
45 | 'description' => '(safehtml) Description of what the plugin does and/or notes from the author', |
||
46 | 'author' => '(safehtml) The author\'s name', |
||
47 | 'author_url' => '(url) The authors web site address', |
||
48 | 'plugin_url' => '(url) Link to the plugin\'s web site.', |
||
49 | 'network' => '(boolean) Whether the plugin can only be activated network wide.', |
||
50 | 'autoupdate' => '(boolean) Whether the plugin is automatically updated', |
||
51 | 'autoupdate_translation' => '(boolean) Whether the plugin is automatically updating translations', |
||
52 | 'uninstallable' => '(boolean) Whether the plugin is unistallable.', |
||
53 | 'action_links' => '(array) An array of action links that the plugin uses.', |
||
54 | 'log' => '(array:safehtml) An array of update log strings.', |
||
55 | ); |
||
56 | |||
57 | protected function result() { |
||
68 | |||
69 | protected function validate_input( $plugin ) { |
||
70 | |||
71 | if ( is_wp_error( $error = parent::validate_input( $plugin ) ) ) { |
||
72 | return $error; |
||
73 | } |
||
74 | |||
75 | if ( is_wp_error( $error = $this->validate_network_wide() ) ) { |
||
76 | return $error; |
||
77 | } |
||
78 | |||
79 | $args = $this->input(); |
||
80 | // find out what plugin, or plugins we are dealing with |
||
81 | // validate the requested plugins |
||
82 | if ( ! isset( $plugin ) || empty( $plugin ) ) { |
||
83 | if ( ! $args['plugins'] || empty( $args['plugins'] ) ) { |
||
84 | return new WP_Error( 'missing_plugin', __( 'You are required to specify a plugin.', 'jetpack' ), 400 ); |
||
|
|||
85 | } |
||
86 | if ( is_array( $args['plugins'] ) ) { |
||
87 | $this->plugins = $args['plugins']; |
||
88 | } else { |
||
89 | $this->plugins[] = $args['plugins']; |
||
90 | } |
||
91 | } else { |
||
92 | $this->bulk = false; |
||
93 | $this->plugins[] = urldecode( $plugin ); |
||
94 | } |
||
95 | |||
96 | if ( is_wp_error( $error = $this->validate_plugins() ) ) { |
||
97 | return $error; |
||
98 | }; |
||
99 | |||
100 | return true; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Walks through submitted plugins to make sure they are valid |
||
105 | * @return bool|WP_Error |
||
106 | */ |
||
107 | protected function validate_plugins() { |
||
124 | |||
125 | protected function format_plugin( $plugin_file, $plugin_data ) { |
||
164 | |||
165 | protected function format_plugin_v1_2( $plugin_file, $plugin_data ) { |
||
200 | |||
201 | protected function plugin_has_autoupdates_enabled( $plugin_file ) { |
||
204 | |||
205 | protected function plugin_has_translations_autoupdates_enabled( $plugin_file ) { |
||
208 | |||
209 | |||
210 | protected function get_file_mod_capabilities() { |
||
254 | |||
255 | protected function get_plugins() { |
||
292 | |||
293 | protected function validate_network_wide() { |
||
306 | |||
307 | |||
308 | View Code Duplication | protected function validate_plugin( $plugin ) { |
|
319 | |||
320 | protected function get_plugin_updates( $plugin_file ) { |
||
344 | |||
345 | protected function get_plugin_action_links( $plugin_file ) { |
||
348 | } |
||
349 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.