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 |
||
6 | abstract class Jetpack_JSON_API_Plugins_Endpoint extends Jetpack_JSON_API_Endpoint { |
||
7 | |||
8 | protected $plugins = array(); |
||
9 | |||
10 | protected $network_wide = false; |
||
11 | |||
12 | protected $bulk = true; |
||
13 | protected $log; |
||
14 | |||
15 | static $_response_format = array( |
||
|
|||
16 | 'id' => '(safehtml) The plugin\'s ID', |
||
17 | 'slug' => '(safehtml) The plugin\'s .org slug', |
||
18 | 'active' => '(boolean) The plugin status.', |
||
19 | 'update' => '(object) The plugin update info.', |
||
20 | 'name' => '(safehtml) The name of the plugin.', |
||
21 | 'plugin_url' => '(url) Link to the plugin\'s web site.', |
||
22 | 'version' => '(safehtml) The plugin version number.', |
||
23 | 'description' => '(safehtml) Description of what the plugin does and/or notes from the author', |
||
24 | 'author' => '(safehtml) The author\'s name', |
||
25 | 'author_url' => '(url) The authors web site address', |
||
26 | 'network' => '(boolean) Whether the plugin can only be activated network wide.', |
||
27 | 'autoupdate' => '(boolean) Whether the plugin is automatically updated', |
||
28 | 'next_autoupdate' => '(string) Y-m-d H:i:s for next scheduled update event', |
||
29 | 'log' => '(array:safehtml) An array of update log strings.', |
||
30 | 'uninstallable' => '(boolean) Whether the plugin is unistallable.', |
||
31 | ); |
||
32 | |||
33 | protected function result() { |
||
44 | |||
45 | protected function validate_input( $plugin ) { |
||
78 | |||
79 | /** |
||
80 | * Walks through submitted plugins to make sure they are valid |
||
81 | * @return bool|WP_Error |
||
82 | */ |
||
83 | protected function validate_plugins() { |
||
98 | |||
99 | protected function format_plugin( $plugin_file, $plugin_data ) { |
||
120 | |||
121 | protected function get_plugins() { |
||
122 | $plugins = array(); |
||
123 | /** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */ |
||
124 | $installed_plugins = apply_filters( 'all_plugins', get_plugins() ); |
||
125 | foreach( $this->plugins as $plugin ) { |
||
126 | if ( ! isset( $installed_plugins[ $plugin ] ) ) |
||
127 | continue; |
||
128 | $plugins[] = $this->format_plugin( $plugin, $installed_plugins[ $plugin ] ); |
||
129 | } |
||
130 | $args = $this->query_args(); |
||
131 | |||
132 | if ( isset( $args['offset'] ) ) { |
||
133 | $plugins = array_slice( $plugins, (int) $args['offset'] ); |
||
134 | } |
||
135 | if ( isset( $args['limit'] ) ) { |
||
136 | $plugins = array_slice( $plugins, 0, (int) $args['limit'] ); |
||
137 | } |
||
138 | |||
139 | return $plugins; |
||
140 | } |
||
141 | |||
142 | protected function validate_network_wide() { |
||
155 | |||
156 | |||
157 | protected function validate_plugin( $plugin ) { |
||
168 | |||
169 | protected function get_plugin_updates( $plugin_file ) { |
||
176 | |||
177 | protected function get_plugin_slug( $plugin_file ) { |
||
200 | } |
||
201 |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.