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_Options 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_Options, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
3 | class Jetpack_Options { |
||
4 | |||
5 | private static $grouped_options = array( |
||
6 | 'compact' => 'jetpack_options', |
||
7 | 'private' => 'jetpack_private_options' |
||
8 | ); |
||
9 | |||
10 | public static function get_option_names( $type = 'compact' ) { |
||
11 | switch ( $type ) { |
||
12 | case 'non-compact' : |
||
13 | case 'non_compact' : |
||
14 | return array( |
||
15 | 'activated', |
||
16 | 'active_modules', |
||
17 | 'available_modules', |
||
18 | 'do_activate', |
||
19 | 'log', |
||
20 | 'publicize', |
||
21 | 'slideshow_background_color', |
||
22 | 'widget_twitter', |
||
23 | 'wpcc_options', |
||
24 | 'relatedposts', |
||
25 | 'file_data', |
||
26 | 'security_report', |
||
27 | 'autoupdate_plugins', // (array) An array of plugin ids ( eg. jetpack/jetpack ) that should be autoupdated |
||
28 | 'autoupdate_themes', // (array) An array of theme ids ( eg. twentyfourteen ) that should be autoupdated |
||
29 | 'autoupdate_core', // (bool) Whether or not to autoupdate core |
||
30 | 'json_api_full_management', // (bool) Allow full management (eg. Activate, Upgrade plugins) of the site via the JSON API. |
||
31 | 'sync_non_public_post_stati', // (bool) Allow synchronisation of posts and pages with non-public status. |
||
32 | 'site_icon_url', // (string) url to the full site icon |
||
33 | 'site_icon_id', // (int) Attachment id of the site icon file |
||
34 | 'dismissed_manage_banner', // (bool) Dismiss Jetpack manage banner allows the user to dismiss the banner permanently |
||
35 | 'restapi_stats_cache', // (array) Stats Cache data. |
||
36 | 'unique_connection', // (array) A flag to determine a unique connection to wordpress.com two values "connected" and "disconnected" with values for how many times each has occured |
||
37 | 'protect_whitelist' // (array) IP Address for the Protect module to ignore |
||
38 | ); |
||
39 | |||
40 | case 'private' : |
||
41 | return array( |
||
42 | 'register', |
||
43 | 'authorize', |
||
44 | 'activate_manage', |
||
45 | 'blog_token', // (string) The Client Secret/Blog Token of this site. |
||
46 | 'user_token', // (string) The User Token of this site. (deprecated) |
||
47 | 'user_tokens' // (array) User Tokens for each user of this site who has connected to jetpack.wordpress.com. |
||
48 | ); |
||
49 | } |
||
50 | |||
51 | return array( |
||
52 | 'id', // (int) The Client ID/WP.com Blog ID of this site. |
||
53 | 'publicize_connections', // (array) An array of Publicize connections from WordPress.com |
||
54 | 'master_user', // (int) The local User ID of the user who connected this site to jetpack.wordpress.com. |
||
55 | 'version', // (string) Used during upgrade procedure to auto-activate new modules. version:time |
||
56 | 'old_version', // (string) Used to determine which modules are the most recently added. previous_version:time |
||
57 | 'fallback_no_verify_ssl_certs', // (int) Flag for determining if this host must skip SSL Certificate verification due to misconfigured SSL. |
||
58 | 'time_diff', // (int) Offset between Jetpack server's clocks and this server's clocks. Jetpack Server Time = time() + (int) Jetpack_Options::get_option( 'time_diff' ) |
||
59 | 'public', // (int|bool) If we think this site is public or not (1, 0), false if we haven't yet tried to figure it out. |
||
60 | 'videopress', // (array) VideoPress options array. |
||
61 | 'is_network_site', // (int|bool) If we think this site is a network or a single blog (1, 0), false if we haven't yet tried to figue it out. |
||
62 | 'social_links', // (array) The specified links for each social networking site. |
||
63 | 'identity_crisis_whitelist', // (array) An array of options, each having an array of the values whitelisted for it. |
||
64 | 'gplus_authors', // (array) The Google+ authorship information for connected users. |
||
65 | 'last_heartbeat', // (int) The timestamp of the last heartbeat that fired. |
||
66 | 'last_security_report', // (int) The timestamp of the last security report that was run. |
||
67 | 'sync_bulk_reindexing', // (bool) If a bulk reindex is currently underway. |
||
68 | 'jumpstart', // (string) A flag for whether or not to show the Jump Start. Accepts: new_connection, jumpstart_activated, jetpack_action_taken, jumpstart_dismissed. |
||
69 | 'hide_jitm' // (array) A list of just in time messages that we should not show because they have been dismissed by the user |
||
70 | ); |
||
71 | } |
||
72 | |||
73 | public static function is_valid( $name, $group = null ) { |
||
101 | |||
102 | /** |
||
103 | * Returns the requested option. Looks in jetpack_options or jetpack_$name as appropriate. |
||
104 | * |
||
105 | * @param string $name Option name |
||
106 | * @param mixed $default (optional) |
||
107 | */ |
||
108 | public static function get_option( $name, $default = false ) { |
||
123 | |||
124 | private static function update_grouped_option( $group, $name, $value ) { |
||
133 | |||
134 | /** |
||
135 | * Updates the single given option. Updates jetpack_options or jetpack_$name as appropriate. |
||
136 | * |
||
137 | * @param string $name Option name |
||
138 | * @param mixed $value Option value |
||
139 | * @param string $autoload If not compact option, allows specifying whether to autoload or not. |
||
|
|||
140 | */ |
||
141 | public static function update_option( $name, $value, $autoload = null ) { |
||
172 | |||
173 | /** |
||
174 | * Updates the multiple given options. Updates jetpack_options and/or jetpack_$name as appropriate. |
||
175 | * |
||
176 | * @param array $array array( option name => option value, ... ) |
||
177 | */ |
||
178 | public static function update_options( $array ) { |
||
190 | |||
191 | /** |
||
192 | * Deletes the given option. May be passed multiple option names as an array. |
||
193 | * Updates jetpack_options and/or deletes jetpack_$name as appropriate. |
||
194 | * |
||
195 | * @param string|array $names |
||
196 | */ |
||
197 | public static function delete_option( $names ) { |
||
221 | |||
222 | private static function get_grouped_option( $group, $name, $default ) { |
||
230 | |||
231 | private static function delete_grouped_option( $group, $names ) { |
||
245 | |||
246 | } |
||
247 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.