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 Manager 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 Manager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class Manager { |
||
15 | |||
16 | /** |
||
17 | * An array that maps a grouped option type to an option name. |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $grouped_options = array( |
||
22 | 'compact' => 'jetpack_options', |
||
23 | 'private' => 'jetpack_private_options', |
||
24 | ); |
||
25 | |||
26 | /** |
||
27 | * Returns an array of option names for a given type. |
||
28 | * |
||
29 | * @param string $type The type of option to return. Defaults to 'compact'. |
||
30 | * |
||
31 | * @return array |
||
32 | */ |
||
33 | public function get_option_names( $type ) { |
||
34 | return array(); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Returns the requested option. Looks in jetpack_options or jetpack_$name as appropriate. |
||
39 | * |
||
40 | * @param string $name Option name. It must come _without_ `jetpack_%` prefix. The method will prefix the option name. |
||
41 | * @param mixed $default (optional) the default value. |
||
42 | * |
||
43 | * @return mixed |
||
44 | */ |
||
45 | public function get_option( $name, $default = false ) { |
||
64 | |||
65 | /** |
||
66 | * Returns a single value from a grouped option. |
||
67 | * |
||
68 | * @param String $group name of the group, i.e., 'private'. |
||
69 | * @param String $name the name of the option to return. |
||
70 | * @param Mixed $default a default value in case the option is not found. |
||
71 | * @return Mixed the option value or default if not found. |
||
72 | */ |
||
73 | View Code Duplication | protected function get_grouped_option( $group, $name, $default ) { |
|
81 | |||
82 | /** |
||
83 | * Updates the single given option. Updates jetpack_options or jetpack_$name as appropriate. |
||
84 | * |
||
85 | * @param string $name Option name. It must come _without_ `jetpack_%` prefix. The method will prefix the option name. |
||
86 | * @param mixed $value Option value. |
||
87 | * @param string $autoload If not compact option, allows specifying whether to autoload or not. |
||
|
|||
88 | * |
||
89 | * @return bool Was the option successfully updated? |
||
90 | */ |
||
91 | public function update_option( $name, $value, $autoload = null ) { |
||
120 | |||
121 | /** |
||
122 | * Updates a single value from a grouped option. |
||
123 | * |
||
124 | * @param String $group name of the group, i.e., 'private'. |
||
125 | * @param String $name the name of the option to update. |
||
126 | * @param Mixed $value the to update the option with. |
||
127 | * @return Boolean was the update successful? |
||
128 | */ |
||
129 | protected function update_grouped_option( $group, $name, $value ) { |
||
138 | |||
139 | /** |
||
140 | * Deletes the given option. May be passed multiple option names as an array. |
||
141 | * Updates jetpack_options and/or deletes jetpack_$name as appropriate. |
||
142 | * |
||
143 | * @param string|array $names Option names. They must come _without_ `jetpack_%` prefix. The method will prefix the option names. |
||
144 | * |
||
145 | * @return bool Was the option successfully deleted? |
||
146 | */ |
||
147 | public function delete_option( $names ) { |
||
172 | |||
173 | /** |
||
174 | * Deletes a single value from a grouped option. |
||
175 | * |
||
176 | * @param String $group name of the group, i.e., 'private'. |
||
177 | * @param Array $names the names of the option to delete. |
||
178 | * @return Mixed the option value or default if not found. |
||
179 | */ |
||
180 | protected function delete_grouped_option( $group, $names ) { |
||
194 | |||
195 | /** |
||
196 | * Is the option name valid? |
||
197 | * |
||
198 | * @param string $name The name of the option. |
||
199 | * @param string|null $group The name of the group that the option is in. Default to null, which will search non_compact. |
||
200 | * |
||
201 | * @return bool Is the option name valid? |
||
202 | */ |
||
203 | public function is_valid( $name, $group = null ) { |
||
231 | |||
232 | /** |
||
233 | * Checks if an option must be saved for the whole network in WP Multisite |
||
234 | * |
||
235 | * @param string $option_name Option name. It must come _without_ `jetpack_%` prefix. The method will prefix the option name. |
||
236 | * |
||
237 | * @return bool |
||
238 | */ |
||
239 | public function is_network_option( $option_name ) { |
||
245 | |||
246 | /** |
||
247 | * Gets an option via $wpdb query. |
||
248 | * |
||
249 | * @since 5.4.0 |
||
250 | * |
||
251 | * @param string $name Option name. |
||
252 | * @param mixed $default Default option value if option is not found. |
||
253 | * |
||
254 | * @return mixed Option value, or null if option is not found and default is not specified. |
||
255 | */ |
||
256 | View Code Duplication | function get_raw_option( $name, $default = null ) { |
|
273 | /** |
||
274 | * This function checks for a constant that, if present, will disable direct DB queries Jetpack uses to manage certain options and force Jetpack to always use Options API instead. |
||
275 | * Options can be selectively managed via a blacklist by filtering option names via the jetpack_disabled_raw_option filter. |
||
276 | * |
||
277 | * @param $name Option name |
||
278 | * |
||
279 | * @return bool |
||
280 | */ |
||
281 | View Code Duplication | function bypass_raw_option( $name ) { |
|
295 | } |
||
296 |
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.