Complex classes like Settings 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 Settings, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Settings { |
||
| 14 | /** |
||
| 15 | * Prefix, used for the sync settings option names. |
||
| 16 | * |
||
| 17 | * @access public |
||
| 18 | * |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | const SETTINGS_OPTION_PREFIX = 'jetpack_sync_settings_'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * A whitelist of valid settings. |
||
| 25 | * |
||
| 26 | * @access public |
||
| 27 | * @static |
||
| 28 | * |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | public static $valid_settings = array( |
||
| 32 | 'dequeue_max_bytes' => true, |
||
| 33 | 'upload_max_bytes' => true, |
||
| 34 | 'upload_max_rows' => true, |
||
| 35 | 'sync_wait_time' => true, |
||
| 36 | 'sync_wait_threshold' => true, |
||
| 37 | 'enqueue_wait_time' => true, |
||
| 38 | 'max_queue_size' => true, |
||
| 39 | 'max_queue_lag' => true, |
||
| 40 | 'queue_max_writes_sec' => true, |
||
| 41 | 'post_types_blacklist' => true, |
||
| 42 | 'taxonomies_blacklist' => true, |
||
| 43 | 'disable' => true, |
||
| 44 | 'network_disable' => true, |
||
| 45 | 'render_filtered_content' => true, |
||
| 46 | 'post_meta_whitelist' => true, |
||
| 47 | 'comment_meta_whitelist' => true, |
||
| 48 | 'max_enqueue_full_sync' => true, |
||
| 49 | 'max_queue_size_full_sync' => true, |
||
| 50 | 'sync_via_cron' => true, |
||
| 51 | 'cron_sync_time_limit' => true, |
||
| 52 | 'known_importers' => true, |
||
| 53 | 'term_relationships_full_sync_item_size' => true, |
||
| 54 | 'sync_sender_enabled' => true, |
||
| 55 | 'full_sync_sender_enabled' => true, |
||
| 56 | 'full_sync_send_duration' => true, |
||
| 57 | 'full_sync_limits' => true, |
||
| 58 | ); |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Whether WordPress is currently running an import. |
||
| 62 | * |
||
| 63 | * @access public |
||
| 64 | * @static |
||
| 65 | * |
||
| 66 | * @var null|boolean |
||
| 67 | */ |
||
| 68 | public static $is_importing; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Whether WordPress is currently running a WP cron request. |
||
| 72 | * |
||
| 73 | * @access public |
||
| 74 | * @static |
||
| 75 | * |
||
| 76 | * @var null|boolean |
||
| 77 | */ |
||
| 78 | public static $is_doing_cron; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Whether we're currently syncing. |
||
| 82 | * |
||
| 83 | * @access public |
||
| 84 | * @static |
||
| 85 | * |
||
| 86 | * @var null|boolean |
||
| 87 | */ |
||
| 88 | public static $is_syncing; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Whether we're currently sending sync items. |
||
| 92 | * |
||
| 93 | * @access public |
||
| 94 | * @static |
||
| 95 | * |
||
| 96 | * @var null|boolean |
||
| 97 | */ |
||
| 98 | public static $is_sending; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Retrieve all settings with their current values. |
||
| 102 | * |
||
| 103 | * @access public |
||
| 104 | * @static |
||
| 105 | * |
||
| 106 | * @return array All current settings. |
||
| 107 | */ |
||
| 108 | public static function get_settings() { |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Fetches the setting. It saves it if the setting doesn't exist, so that it gets |
||
| 119 | * autoloaded on page load rather than re-queried every time. |
||
| 120 | * |
||
| 121 | * @access public |
||
| 122 | * @static |
||
| 123 | * |
||
| 124 | * @param string $setting The setting name. |
||
| 125 | * @return mixed The setting value. |
||
| 126 | */ |
||
| 127 | public static function get_setting( $setting ) { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Change multiple settings in the same time. |
||
| 188 | * |
||
| 189 | * @access public |
||
| 190 | * @static |
||
| 191 | * |
||
| 192 | * @param array $new_settings The new settings. |
||
| 193 | */ |
||
| 194 | public static function update_settings( $new_settings ) { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Whether the specified setting is a network setting. |
||
| 217 | * |
||
| 218 | * @access public |
||
| 219 | * @static |
||
| 220 | * |
||
| 221 | * @param string $setting Setting name. |
||
| 222 | * @return boolean Whether the setting is a network setting. |
||
| 223 | */ |
||
| 224 | public static function is_network_setting( $setting ) { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Returns escaped SQL for blacklisted post types. |
||
| 230 | * Can be injected directly into a WHERE clause. |
||
| 231 | * |
||
| 232 | * @access public |
||
| 233 | * @static |
||
| 234 | * |
||
| 235 | * @return string SQL WHERE clause. |
||
| 236 | */ |
||
| 237 | public static function get_blacklisted_post_types_sql() { |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Returns escaped SQL for blacklisted taxonomies. |
||
| 243 | * Can be injected directly into a WHERE clause. |
||
| 244 | * |
||
| 245 | * @access public |
||
| 246 | * @static |
||
| 247 | * |
||
| 248 | * @return string SQL WHERE clause. |
||
| 249 | */ |
||
| 250 | public static function get_blacklisted_taxonomies_sql() { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Returns escaped SQL for blacklisted post meta. |
||
| 256 | * Can be injected directly into a WHERE clause. |
||
| 257 | * |
||
| 258 | * @access public |
||
| 259 | * @static |
||
| 260 | * |
||
| 261 | * @return string SQL WHERE clause. |
||
| 262 | */ |
||
| 263 | public static function get_whitelisted_post_meta_sql() { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Returns escaped SQL for blacklisted comment meta. |
||
| 269 | * Can be injected directly into a WHERE clause. |
||
| 270 | * |
||
| 271 | * @access public |
||
| 272 | * @static |
||
| 273 | * |
||
| 274 | * @return string SQL WHERE clause. |
||
| 275 | */ |
||
| 276 | public static function get_whitelisted_comment_meta_sql() { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Returns escaped SQL for comments, excluding any spam comments. |
||
| 282 | * Can be injected directly into a WHERE clause. |
||
| 283 | * |
||
| 284 | * @access public |
||
| 285 | * @static |
||
| 286 | * |
||
| 287 | * @return string SQL WHERE clause. |
||
| 288 | */ |
||
| 289 | public static function get_comments_filter_sql() { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Delete any settings options and clean up the current settings state. |
||
| 295 | * |
||
| 296 | * @access public |
||
| 297 | * @static |
||
| 298 | */ |
||
| 299 | public static function reset_data() { |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Set the importing state. |
||
| 312 | * |
||
| 313 | * @access public |
||
| 314 | * @static |
||
| 315 | * |
||
| 316 | * @param boolean $is_importing Whether WordPress is currently importing. |
||
| 317 | */ |
||
| 318 | public static function set_importing( $is_importing ) { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Whether WordPress is currently importing. |
||
| 325 | * |
||
| 326 | * @access public |
||
| 327 | * @static |
||
| 328 | * |
||
| 329 | * @return boolean Whether WordPress is currently importing. |
||
| 330 | */ |
||
| 331 | public static function is_importing() { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Whether sync is enabled. |
||
| 341 | * |
||
| 342 | * @access public |
||
| 343 | * @static |
||
| 344 | * |
||
| 345 | * @return boolean Whether sync is enabled. |
||
| 346 | */ |
||
| 347 | public static function is_sync_enabled() { |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Set the WP cron state. |
||
| 353 | * |
||
| 354 | * @access public |
||
| 355 | * @static |
||
| 356 | * |
||
| 357 | * @param boolean $is_doing_cron Whether WordPress is currently doing WP cron. |
||
| 358 | */ |
||
| 359 | public static function set_doing_cron( $is_doing_cron ) { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Whether WordPress is currently doing WP cron. |
||
| 366 | * |
||
| 367 | * @access public |
||
| 368 | * @static |
||
| 369 | * |
||
| 370 | * @return boolean Whether WordPress is currently doing WP cron. |
||
| 371 | */ |
||
| 372 | public static function is_doing_cron() { |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Whether we are currently syncing. |
||
| 382 | * |
||
| 383 | * @access public |
||
| 384 | * @static |
||
| 385 | * |
||
| 386 | * @return boolean Whether we are currently syncing. |
||
| 387 | */ |
||
| 388 | public static function is_syncing() { |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Set the syncing state. |
||
| 394 | * |
||
| 395 | * @access public |
||
| 396 | * @static |
||
| 397 | * |
||
| 398 | * @param boolean $is_syncing Whether we are currently syncing. |
||
| 399 | */ |
||
| 400 | public static function set_is_syncing( $is_syncing ) { |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Whether we are currently sending sync items. |
||
| 406 | * |
||
| 407 | * @access public |
||
| 408 | * @static |
||
| 409 | * |
||
| 410 | * @return boolean Whether we are currently sending sync items. |
||
| 411 | */ |
||
| 412 | public static function is_sending() { |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Set the sending state. |
||
| 418 | * |
||
| 419 | * @access public |
||
| 420 | * @static |
||
| 421 | * |
||
| 422 | * @param boolean $is_sending Whether we are currently sending sync items. |
||
| 423 | */ |
||
| 424 | public static function set_is_sending( $is_sending ) { |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Whether should send from the queue |
||
| 430 | * |
||
| 431 | * @access public |
||
| 432 | * @static |
||
| 433 | * |
||
| 434 | * @param string $queue_id The queue identifier. |
||
| 435 | * |
||
| 436 | * @return boolean Whether sync is enabled. |
||
| 437 | */ |
||
| 438 | public static function is_sender_enabled( $queue_id ) { |
||
| 441 | |||
| 442 | } |
||
| 443 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: