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 | abstract 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 | abstract public function get_option_names( $type ); |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Returns the requested option. Looks in jetpack_options or jetpack_$name as appropriate. |
||
| 37 | * |
||
| 38 | * @param string $name Option name. It must come _without_ `jetpack_%` prefix. The method will prefix the option name. |
||
| 39 | * @param mixed $default (optional) the default value. |
||
| 40 | * |
||
| 41 | * @return mixed |
||
| 42 | */ |
||
| 43 | public function get_option( $name, $default = false ) { |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Returns a single value from a grouped option. |
||
| 65 | * |
||
| 66 | * @param String $group name of the group, i.e., 'private'. |
||
| 67 | * @param String $name the name of the option to return. |
||
| 68 | * @param Mixed $default a default value in case the option is not found. |
||
| 69 | * @return Mixed the option value or default if not found. |
||
| 70 | */ |
||
| 71 | View Code Duplication | protected function get_grouped_option( $group, $name, $default ) { |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Updates the single given option. Updates jetpack_options or jetpack_$name as appropriate. |
||
| 82 | * |
||
| 83 | * @param string $name Option name. It must come _without_ `jetpack_%` prefix. The method will prefix the option name. |
||
| 84 | * @param mixed $value Option value. |
||
| 85 | * @param string $autoload If not compact option, allows specifying whether to autoload or not. |
||
|
|
|||
| 86 | * |
||
| 87 | * @return bool Was the option successfully updated? |
||
| 88 | */ |
||
| 89 | public function update_option( $name, $value, $autoload = null ) { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Updates a single value from a grouped option. |
||
| 121 | * |
||
| 122 | * @param String $group name of the group, i.e., 'private'. |
||
| 123 | * @param String $name the name of the option to update. |
||
| 124 | * @param Mixed $value the to update the option with. |
||
| 125 | * @return Boolean was the update successful? |
||
| 126 | */ |
||
| 127 | protected function update_grouped_option( $group, $name, $value ) { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Deletes the given option. May be passed multiple option names as an array. |
||
| 139 | * Updates jetpack_options and/or deletes jetpack_$name as appropriate. |
||
| 140 | * |
||
| 141 | * @param string|array $names Option names. They must come _without_ `jetpack_%` prefix. The method will prefix the option names. |
||
| 142 | * |
||
| 143 | * @return bool Was the option successfully deleted? |
||
| 144 | */ |
||
| 145 | public function delete_option( $names ) { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Deletes a single value from a grouped option. |
||
| 173 | * |
||
| 174 | * @param String $group name of the group, i.e., 'private'. |
||
| 175 | * @param Array $names the names of the option to delete. |
||
| 176 | * @return Mixed the option value or default if not found. |
||
| 177 | */ |
||
| 178 | protected function delete_grouped_option( $group, $names ) { |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Is the option name valid? |
||
| 195 | * |
||
| 196 | * @param string $name The name of the option. |
||
| 197 | * @param string|null $group The name of the group that the option is in. Default to null, which will search non_compact. |
||
| 198 | * |
||
| 199 | * @return bool Is the option name valid? |
||
| 200 | */ |
||
| 201 | public function is_valid( $name, $group = null ) { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Checks if an option must be saved for the whole network in WP Multisite |
||
| 232 | * |
||
| 233 | * @param string $option_name Option name. It must come _without_ `jetpack_%` prefix. The method will prefix the option name. |
||
| 234 | * |
||
| 235 | * @return bool |
||
| 236 | */ |
||
| 237 | public function is_network_option( $option_name ) { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Gets an option via $wpdb query. |
||
| 246 | * |
||
| 247 | * @since 5.4.0 |
||
| 248 | * |
||
| 249 | * @param string $name Option name. |
||
| 250 | * @param mixed $default Default option value if option is not found. |
||
| 251 | * |
||
| 252 | * @return mixed Option value, or null if option is not found and default is not specified. |
||
| 253 | */ |
||
| 254 | View Code Duplication | function get_raw_option( $name, $default = null ) { |
|
| 271 | /** |
||
| 272 | * 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. |
||
| 273 | * Options can be selectively managed via a blacklist by filtering option names via the jetpack_disabled_raw_option filter. |
||
| 274 | * |
||
| 275 | * @param $name Option name |
||
| 276 | * |
||
| 277 | * @return bool |
||
| 278 | */ |
||
| 279 | View Code Duplication | function bypass_raw_option( $name ) { |
|
| 293 | } |
||
| 294 |
This check looks for
@paramannotations 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.