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 | ); |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Whether WordPress is currently running an import. |
||
| 60 | * |
||
| 61 | * @access public |
||
| 62 | * @static |
||
| 63 | * |
||
| 64 | * @var null|boolean |
||
| 65 | */ |
||
| 66 | public static $is_importing; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Whether WordPress is currently running a WP cron request. |
||
| 70 | * |
||
| 71 | * @access public |
||
| 72 | * @static |
||
| 73 | * |
||
| 74 | * @var null|boolean |
||
| 75 | */ |
||
| 76 | public static $is_doing_cron; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Whether we're currently syncing. |
||
| 80 | * |
||
| 81 | * @access public |
||
| 82 | * @static |
||
| 83 | * |
||
| 84 | * @var null|boolean |
||
| 85 | */ |
||
| 86 | public static $is_syncing; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Whether we're currently sending sync items. |
||
| 90 | * |
||
| 91 | * @access public |
||
| 92 | * @static |
||
| 93 | * |
||
| 94 | * @var null|boolean |
||
| 95 | */ |
||
| 96 | public static $is_sending; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Retrieve all settings with their current values. |
||
| 100 | * |
||
| 101 | * @access public |
||
| 102 | * @static |
||
| 103 | * |
||
| 104 | * @return array All current settings. |
||
| 105 | */ |
||
| 106 | public static function get_settings() { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Fetches the setting. It saves it if the setting doesn't exist, so that it gets |
||
| 117 | * autoloaded on page load rather than re-queried every time. |
||
| 118 | * |
||
| 119 | * @access public |
||
| 120 | * @static |
||
| 121 | * |
||
| 122 | * @param string $setting The setting name. |
||
| 123 | * @return mixed The setting value. |
||
| 124 | */ |
||
| 125 | public static function get_setting( $setting ) { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Change multiple settings in the same time. |
||
| 186 | * |
||
| 187 | * @access public |
||
| 188 | * @static |
||
| 189 | * |
||
| 190 | * @param array $new_settings The new settings. |
||
| 191 | */ |
||
| 192 | public static function update_settings( $new_settings ) { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Whether the specified setting is a network setting. |
||
| 215 | * |
||
| 216 | * @access public |
||
| 217 | * @static |
||
| 218 | * |
||
| 219 | * @param string $setting Setting name. |
||
| 220 | * @return boolean Whether the setting is a network setting. |
||
| 221 | */ |
||
| 222 | public static function is_network_setting( $setting ) { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Returns escaped SQL for blacklisted post types. |
||
| 228 | * Can be injected directly into a WHERE clause. |
||
| 229 | * |
||
| 230 | * @access public |
||
| 231 | * @static |
||
| 232 | * |
||
| 233 | * @return string SQL WHERE clause. |
||
| 234 | */ |
||
| 235 | public static function get_blacklisted_post_types_sql() { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Returns escaped SQL for blacklisted taxonomies. |
||
| 241 | * Can be injected directly into a WHERE clause. |
||
| 242 | * |
||
| 243 | * @access public |
||
| 244 | * @static |
||
| 245 | * |
||
| 246 | * @return string SQL WHERE clause. |
||
| 247 | */ |
||
| 248 | public static function get_blacklisted_taxonomies_sql() { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Returns escaped SQL for blacklisted post meta. |
||
| 254 | * Can be injected directly into a WHERE clause. |
||
| 255 | * |
||
| 256 | * @access public |
||
| 257 | * @static |
||
| 258 | * |
||
| 259 | * @return string SQL WHERE clause. |
||
| 260 | */ |
||
| 261 | public static function get_whitelisted_post_meta_sql() { |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Returns escaped SQL for blacklisted comment meta. |
||
| 267 | * Can be injected directly into a WHERE clause. |
||
| 268 | * |
||
| 269 | * @access public |
||
| 270 | * @static |
||
| 271 | * |
||
| 272 | * @return string SQL WHERE clause. |
||
| 273 | */ |
||
| 274 | public static function get_whitelisted_comment_meta_sql() { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Returns escaped SQL for comments, excluding any spam comments. |
||
| 280 | * Can be injected directly into a WHERE clause. |
||
| 281 | * |
||
| 282 | * @access public |
||
| 283 | * @static |
||
| 284 | * |
||
| 285 | * @return string SQL WHERE clause. |
||
| 286 | */ |
||
| 287 | public static function get_comments_filter_sql() { |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Delete any settings options and clean up the current settings state. |
||
| 293 | * |
||
| 294 | * @access public |
||
| 295 | * @static |
||
| 296 | */ |
||
| 297 | public static function reset_data() { |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Set the importing state. |
||
| 310 | * |
||
| 311 | * @access public |
||
| 312 | * @static |
||
| 313 | * |
||
| 314 | * @param boolean $is_importing Whether WordPress is currently importing. |
||
| 315 | */ |
||
| 316 | public static function set_importing( $is_importing ) { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Whether WordPress is currently importing. |
||
| 323 | * |
||
| 324 | * @access public |
||
| 325 | * @static |
||
| 326 | * |
||
| 327 | * @return boolean Whether WordPress is currently importing. |
||
| 328 | */ |
||
| 329 | public static function is_importing() { |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Whether sync is enabled. |
||
| 339 | * |
||
| 340 | * @access public |
||
| 341 | * @static |
||
| 342 | * |
||
| 343 | * @return boolean Whether sync is enabled. |
||
| 344 | */ |
||
| 345 | public static function is_sync_enabled() { |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Set the WP cron state. |
||
| 351 | * |
||
| 352 | * @access public |
||
| 353 | * @static |
||
| 354 | * |
||
| 355 | * @param boolean $is_doing_cron Whether WordPress is currently doing WP cron. |
||
| 356 | */ |
||
| 357 | public static function set_doing_cron( $is_doing_cron ) { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Whether WordPress is currently doing WP cron. |
||
| 364 | * |
||
| 365 | * @access public |
||
| 366 | * @static |
||
| 367 | * |
||
| 368 | * @return boolean Whether WordPress is currently doing WP cron. |
||
| 369 | */ |
||
| 370 | public static function is_doing_cron() { |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Whether we are currently syncing. |
||
| 380 | * |
||
| 381 | * @access public |
||
| 382 | * @static |
||
| 383 | * |
||
| 384 | * @return boolean Whether we are currently syncing. |
||
| 385 | */ |
||
| 386 | public static function is_syncing() { |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Set the syncing state. |
||
| 392 | * |
||
| 393 | * @access public |
||
| 394 | * @static |
||
| 395 | * |
||
| 396 | * @param boolean $is_syncing Whether we are currently syncing. |
||
| 397 | */ |
||
| 398 | public static function set_is_syncing( $is_syncing ) { |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Whether we are currently sending sync items. |
||
| 404 | * |
||
| 405 | * @access public |
||
| 406 | * @static |
||
| 407 | * |
||
| 408 | * @return boolean Whether we are currently sending sync items. |
||
| 409 | */ |
||
| 410 | public static function is_sending() { |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Set the sending state. |
||
| 416 | * |
||
| 417 | * @access public |
||
| 418 | * @static |
||
| 419 | * |
||
| 420 | * @param boolean $is_sending Whether we are currently sending sync items. |
||
| 421 | */ |
||
| 422 | public static function set_is_sending( $is_sending ) { |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Whether should send from the queue |
||
| 428 | * |
||
| 429 | * @access public |
||
| 430 | * @static |
||
| 431 | * |
||
| 432 | * @param string $queue_id The queue identifier. |
||
| 433 | * |
||
| 434 | * @return boolean Whether sync is enabled. |
||
| 435 | */ |
||
| 436 | public static function is_sender_enabled( $queue_id ) { |
||
| 439 | |||
| 440 | } |
||
| 441 |
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: