Complex classes like Wordlift_Configuration_Service 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 Wordlift_Configuration_Service, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Wordlift_Configuration_Service { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The entity base path option name. |
||
| 26 | * |
||
| 27 | * @since 3.6.0 |
||
| 28 | */ |
||
| 29 | const ENTITY_BASE_PATH_KEY = 'wl_entity_base_path'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The skip wizard (admin installation wizard) option name. |
||
| 33 | * |
||
| 34 | * @since 3.9.0 |
||
| 35 | */ |
||
| 36 | const SKIP_WIZARD = 'wl_skip_wizard'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * WordLift's key option name. |
||
| 40 | * |
||
| 41 | * @since 3.9.0 |
||
| 42 | */ |
||
| 43 | const KEY = 'key'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * WordLift's configured language option name. |
||
| 47 | * |
||
| 48 | * @since 3.9.0 |
||
| 49 | */ |
||
| 50 | const LANGUAGE = 'site_language'; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * WordLift's configured country code. |
||
| 54 | * |
||
| 55 | * @since 3.18.0 |
||
| 56 | */ |
||
| 57 | const COUNTRY_CODE = 'country_code'; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The publisher entity post ID option name. |
||
| 61 | * |
||
| 62 | * @since 3.9.0 |
||
| 63 | */ |
||
| 64 | const PUBLISHER_ID = 'publisher_id'; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The dataset URI option name |
||
| 68 | * |
||
| 69 | * @since 3.10.0 |
||
| 70 | */ |
||
| 71 | const DATASET_URI = 'redlink_dataset_uri'; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * The link by default option name. |
||
| 75 | * |
||
| 76 | * @since 3.11.0 |
||
| 77 | */ |
||
| 78 | const LINK_BY_DEFAULT = 'link_by_default'; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * The user preferences about sharing data option. |
||
| 82 | * |
||
| 83 | * @since 3.19.0 |
||
| 84 | */ |
||
| 85 | const SEND_DIAGNOSTIC = 'send_diagnostic'; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * The package type configuration key. |
||
| 89 | * |
||
| 90 | * @since 3.20.0 |
||
| 91 | */ |
||
| 92 | const PACKAGE_TYPE = 'package_type'; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * The {@link Wordlift_Log_Service} instance. |
||
| 96 | * |
||
| 97 | * @since 3.16.0 |
||
| 98 | * |
||
| 99 | * @var \Wordlift_Log_Service $log The {@link Wordlift_Log_Service} instance. |
||
| 100 | */ |
||
| 101 | private $log; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * The Wordlift_Configuration_Service's singleton instance. |
||
| 105 | * |
||
| 106 | * @since 3.6.0 |
||
| 107 | * |
||
| 108 | * @access private |
||
| 109 | * @var \Wordlift_Configuration_Service $instance Wordlift_Configuration_Service's singleton instance. |
||
| 110 | */ |
||
| 111 | private static $instance; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Create a Wordlift_Configuration_Service's instance. |
||
| 115 | * |
||
| 116 | * @since 3.6.0 |
||
| 117 | */ |
||
| 118 | public function __construct() { |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Get the singleton instance. |
||
| 128 | * |
||
| 129 | * @since 3.6.0 |
||
| 130 | * |
||
| 131 | * @return \Wordlift_Configuration_Service |
||
| 132 | */ |
||
| 133 | public static function get_instance() { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Get a configuration given the option name and a key. The option value is |
||
| 140 | * expected to be an array. |
||
| 141 | * |
||
| 142 | * @since 3.6.0 |
||
| 143 | * |
||
| 144 | * @param string $option The option name. |
||
| 145 | * @param string $key A key in the option value array. |
||
| 146 | * @param string $default The default value in case the key is not found (by default an empty string). |
||
| 147 | * |
||
| 148 | * @return mixed The configuration value or the default value if not found. |
||
| 149 | */ |
||
| 150 | private function get( $option, $key, $default = '' ) { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Set a configuration parameter. |
||
| 159 | * |
||
| 160 | * @since 3.9.0 |
||
| 161 | * |
||
| 162 | * @param string $option Name of option to retrieve. Expected to not be SQL-escaped. |
||
| 163 | * @param string $key The value key. |
||
| 164 | * @param mixed $value The value. |
||
| 165 | */ |
||
| 166 | private function set( $option, $key, $value ) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Get the entity base path, by default 'entity'. |
||
| 177 | * |
||
| 178 | * @since 3.6.0 |
||
| 179 | * |
||
| 180 | * @return string The entity base path. |
||
| 181 | */ |
||
| 182 | public function get_entity_base_path() { |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Get the entity base path. |
||
| 189 | * |
||
| 190 | * @since 3.9.0 |
||
| 191 | * |
||
| 192 | * @param string $value The entity base path. |
||
| 193 | */ |
||
| 194 | public function set_entity_base_path( $value ) { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Whether the installation skip wizard should be skipped. |
||
| 202 | * |
||
| 203 | * @since 3.9.0 |
||
| 204 | * |
||
| 205 | * @return bool True if it should be skipped otherwise false. |
||
| 206 | */ |
||
| 207 | public function is_skip_wizard() { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Set the skip wizard parameter. |
||
| 214 | * |
||
| 215 | * @since 3.9.0 |
||
| 216 | * |
||
| 217 | * @param bool $value True to skip the wizard. We expect a boolean value. |
||
| 218 | */ |
||
| 219 | public function set_skip_wizard( $value ) { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Get WordLift's key. |
||
| 227 | * |
||
| 228 | * @since 3.9.0 |
||
| 229 | * |
||
| 230 | * @return string WordLift's key or an empty string if not set. |
||
| 231 | */ |
||
| 232 | public function get_key() { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Set WordLift's key. |
||
| 239 | * |
||
| 240 | * @since 3.9.0 |
||
| 241 | * |
||
| 242 | * @param string $value WordLift's key. |
||
| 243 | */ |
||
| 244 | public function set_key( $value ) { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Get WordLift's configured language, by default 'en'. |
||
| 251 | * |
||
| 252 | * Note that WordLift's language is used when writing strings to the Linked Data dataset, not for the analysis. |
||
| 253 | * |
||
| 254 | * @since 3.9.0 |
||
| 255 | * |
||
| 256 | * @return string WordLift's configured language code ('en' by default). |
||
| 257 | */ |
||
| 258 | public function get_language_code() { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Set WordLift's language code, used when storing strings to the Linked Data dataset. |
||
| 265 | * |
||
| 266 | * @since 3.9.0 |
||
| 267 | * |
||
| 268 | * @param string $value WordLift's language code. |
||
| 269 | */ |
||
| 270 | public function set_language_code( $value ) { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Set the user preferences about sharing diagnostic with us. |
||
| 278 | * |
||
| 279 | * @since 3.19.0 |
||
| 280 | * |
||
| 281 | * @param string $value The user preferences(yes/no). |
||
| 282 | */ |
||
| 283 | public function set_diagnostic_preferences( $value ) { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Get the user preferences about sharing diagnostic. |
||
| 291 | * |
||
| 292 | * @since 3.19.0 |
||
| 293 | */ |
||
| 294 | public function get_diagnostic_preferences() { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Get WordLift's configured country code, by default 'us'. |
||
| 301 | * |
||
| 302 | * @since 3.18.0 |
||
| 303 | * |
||
| 304 | * @return string WordLift's configured country code ('us' by default). |
||
| 305 | */ |
||
| 306 | public function get_country_code() { |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Set WordLift's country code. |
||
| 313 | * |
||
| 314 | * @since 3.18.0 |
||
| 315 | * |
||
| 316 | * @param string $value WordLift's country code. |
||
| 317 | */ |
||
| 318 | public function set_country_code( $value ) { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Get the publisher entity post id. |
||
| 326 | * |
||
| 327 | * The publisher entity post id points to an entity post which contains the data for the publisher used in schema.org |
||
| 328 | * Article markup. |
||
| 329 | * |
||
| 330 | * @since 3.9.0 |
||
| 331 | * |
||
| 332 | * @return int|NULL The publisher entity post id or NULL if not set. |
||
| 333 | */ |
||
| 334 | public function get_publisher_id() { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Set the publisher entity post id. |
||
| 341 | * |
||
| 342 | * @since 3.9.0 |
||
| 343 | * |
||
| 344 | * @param int $value The publisher entity post id. |
||
| 345 | */ |
||
| 346 | public function set_publisher_id( $value ) { |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Get the dataset URI. |
||
| 354 | * |
||
| 355 | * @since 3.10.0 |
||
| 356 | * |
||
| 357 | * @return string The dataset URI or an empty string if not set. |
||
| 358 | */ |
||
| 359 | public function get_dataset_uri() { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Set the dataset URI. |
||
| 366 | * |
||
| 367 | * @since 3.10.0 |
||
| 368 | * |
||
| 369 | * @param string $value The dataset URI. |
||
| 370 | */ |
||
| 371 | public function set_dataset_uri( $value ) { |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Get the package type. |
||
| 378 | * |
||
| 379 | * @since 3.20.0 |
||
| 380 | * |
||
| 381 | * @return string The package type or an empty string if not set. |
||
| 382 | */ |
||
| 383 | public function get_package_type() { |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Set the package type. |
||
| 390 | * |
||
| 391 | * @since 3.20.0 |
||
| 392 | * |
||
| 393 | * @param string $value The package type. |
||
| 394 | */ |
||
| 395 | public function set_package_type( $value ) { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Intercept the change of the WordLift key in order to set the dataset URI. |
||
| 402 | * |
||
| 403 | * |
||
| 404 | * @since 3.20.0 as of #761, we save settings every time a key is set, not only when the key changes, so to |
||
| 405 | * store the configuration parameters such as country or language. |
||
| 406 | * @since 3.11.0 |
||
| 407 | * |
||
| 408 | * @see https://github.com/insideout10/wordlift-plugin/issues/761 |
||
| 409 | * |
||
| 410 | * @param array $old_value The old settings. |
||
| 411 | * @param array $new_value The new settings. |
||
| 412 | */ |
||
| 413 | public function update_key( $old_value, $new_value ) { |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Handle retrieving the dataset uri from the remote server. |
||
| 436 | * |
||
| 437 | * If a valid dataset uri is returned it is stored in the appropriate option, |
||
| 438 | * otherwise the option is set to empty string. |
||
| 439 | * |
||
| 440 | * @since 3.17.0 send the site URL and get the dataset URI. |
||
| 441 | * @since 3.12.0 |
||
| 442 | * |
||
| 443 | * @param string $key The key to be used. |
||
| 444 | */ |
||
| 445 | public function get_remote_dataset_uri( $key ) { |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Handle the edge case where a user submits the same key again |
||
| 510 | * when he does not have the dataset uri to regain it. |
||
| 511 | * |
||
| 512 | * This can not be handled in the normal option update hook because |
||
| 513 | * it is not being triggered when the save value equals to the one already |
||
| 514 | * in the DB. |
||
| 515 | * |
||
| 516 | * @since 3.12.0 |
||
| 517 | * |
||
| 518 | * @param mixed $value The new, unserialized option value. |
||
| 519 | * @param mixed $old_value The old option value. |
||
| 520 | * |
||
| 521 | * @return mixed The same value in the $value parameter |
||
| 522 | */ |
||
| 523 | function maybe_update_dataset_uri( $value, $old_value ) { |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Get the API URI to retrieve the dataset URI using the WordLift Key. |
||
| 544 | * |
||
| 545 | * @since 3.11.0 |
||
| 546 | * |
||
| 547 | * @param string $key The WordLift key to use. |
||
| 548 | * |
||
| 549 | * @return string The API URI. |
||
| 550 | */ |
||
| 551 | public function get_accounts_by_key_dataset_uri( $key ) { |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Get the `accounts` end point. |
||
| 558 | * |
||
| 559 | * @since 3.16.0 |
||
| 560 | * |
||
| 561 | * @return string The `accounts` end point. |
||
| 562 | */ |
||
| 563 | public function get_accounts() { |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Get the `link by default` option. |
||
| 570 | * |
||
| 571 | * @since 3.13.0 |
||
| 572 | * |
||
| 573 | * @return bool True if entities must be linked by default otherwise false. |
||
| 574 | */ |
||
| 575 | public function is_link_by_default() { |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Set the `link by default` option. |
||
| 582 | * |
||
| 583 | * @since 3.13.0 |
||
| 584 | * |
||
| 585 | * @param bool $value True to enabling linking by default, otherwise false. |
||
| 586 | */ |
||
| 587 | public function set_link_by_default( $value ) { |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Get the URL to perform batch analyses. |
||
| 594 | * |
||
| 595 | * @since 3.14.0 |
||
| 596 | * |
||
| 597 | * @return string The URL to call to perform the batch analyzes. |
||
| 598 | */ |
||
| 599 | public function get_batch_analysis_url() { |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Get the URL to perform autocomplete request. |
||
| 607 | * |
||
| 608 | * @since 3.15.0 |
||
| 609 | * |
||
| 610 | * @return string The URL to call to perform the batch analyzes. |
||
| 611 | */ |
||
| 612 | public function get_autocomplete_url() { |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Get the URL to perform feedback deactivation request. |
||
| 620 | * |
||
| 621 | * @since 3.19.0 |
||
| 622 | * |
||
| 623 | * @return string The URL to call to perform the feedback deactivation request. |
||
| 624 | */ |
||
| 625 | public function get_deactivation_feedback_url() { |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Get the base API URL. |
||
| 633 | * |
||
| 634 | * @since 3.20.0 |
||
| 635 | * |
||
| 636 | * @return string The base API URL. |
||
| 637 | */ |
||
| 638 | public function get_api_url() { |
||
| 642 | |||
| 643 | } |
||
| 644 |
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: