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 |
||
| 24 | class Wordlift_Configuration_Service { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The entity base path option name. |
||
| 28 | * |
||
| 29 | * @since 3.6.0 |
||
| 30 | */ |
||
| 31 | const ENTITY_BASE_PATH_KEY = 'wl_entity_base_path'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The skip wizard (admin installation wizard) option name. |
||
| 35 | * |
||
| 36 | * @since 3.9.0 |
||
| 37 | */ |
||
| 38 | const SKIP_WIZARD = 'wl_skip_wizard'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * WordLift's key option name. |
||
| 42 | * |
||
| 43 | * @since 3.9.0 |
||
| 44 | */ |
||
| 45 | const KEY = 'key'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * WordLift's configured language option name. |
||
| 49 | * |
||
| 50 | * @since 3.9.0 |
||
| 51 | */ |
||
| 52 | const LANGUAGE = 'site_language'; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * WordLift's configured country code. |
||
| 56 | * |
||
| 57 | * @since 3.18.0 |
||
| 58 | */ |
||
| 59 | const COUNTRY_CODE = 'country_code'; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The publisher entity post ID option name. |
||
| 63 | * |
||
| 64 | * @since 3.9.0 |
||
| 65 | */ |
||
| 66 | const PUBLISHER_ID = 'publisher_id'; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * The dataset URI option name |
||
| 70 | * |
||
| 71 | * @since 3.10.0 |
||
| 72 | */ |
||
| 73 | const DATASET_URI = 'redlink_dataset_uri'; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * The link by default option name. |
||
| 77 | * |
||
| 78 | * @since 3.11.0 |
||
| 79 | */ |
||
| 80 | const LINK_BY_DEFAULT = 'link_by_default'; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * The analytics enable option. |
||
| 84 | * |
||
| 85 | * @since 3.21.0 |
||
| 86 | */ |
||
| 87 | const ANALYTICS_ENABLE = 'analytics_enable'; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * The analytics entity uri dimension option. |
||
| 91 | * |
||
| 92 | * @since 3.21.0 |
||
| 93 | */ |
||
| 94 | const ANALYTICS_ENTITY_URI_DIMENSION = 'analytics_entity_uri_dimension'; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * The analytics entity type dimension option. |
||
| 98 | * |
||
| 99 | * @since 3.21.0 |
||
| 100 | */ |
||
| 101 | const ANALYTICS_ENTITY_TYPE_DIMENSION = 'analytics_entity_type_dimension'; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * The user preferences about sharing data option. |
||
| 105 | * |
||
| 106 | * @since 3.19.0 |
||
| 107 | */ |
||
| 108 | const SEND_DIAGNOSTIC = 'send_diagnostic'; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * The package type configuration key. |
||
| 112 | * |
||
| 113 | * @since 3.20.0 |
||
| 114 | */ |
||
| 115 | const PACKAGE_TYPE = 'package_type'; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * The {@link Wordlift_Log_Service} instance. |
||
| 119 | * |
||
| 120 | * @since 3.16.0 |
||
| 121 | * |
||
| 122 | * @var \Wordlift_Log_Service $log The {@link Wordlift_Log_Service} instance. |
||
| 123 | */ |
||
| 124 | private $log; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * The Wordlift_Configuration_Service's singleton instance. |
||
| 128 | * |
||
| 129 | * @since 3.6.0 |
||
| 130 | * |
||
| 131 | * @access private |
||
| 132 | * @var \Wordlift_Configuration_Service $instance Wordlift_Configuration_Service's singleton instance. |
||
| 133 | */ |
||
| 134 | private static $instance; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Create a Wordlift_Configuration_Service's instance. |
||
| 138 | * |
||
| 139 | * @since 3.6.0 |
||
| 140 | */ |
||
| 141 | public function __construct() { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Get the singleton instance. |
||
| 151 | * |
||
| 152 | * @return \Wordlift_Configuration_Service |
||
| 153 | * @since 3.6.0 |
||
| 154 | * |
||
| 155 | */ |
||
| 156 | public static function get_instance() { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Get a configuration given the option name and a key. The option value is |
||
| 163 | * expected to be an array. |
||
| 164 | * |
||
| 165 | * @param string $option The option name. |
||
| 166 | * @param string $key A key in the option value array. |
||
| 167 | * @param string $default The default value in case the key is not found (by default an empty string). |
||
| 168 | * |
||
| 169 | * @return mixed The configuration value or the default value if not found. |
||
| 170 | * @since 3.6.0 |
||
| 171 | * |
||
| 172 | */ |
||
| 173 | private function get( $option, $key, $default = '' ) { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Set a configuration parameter. |
||
| 182 | * |
||
| 183 | * @param string $option Name of option to retrieve. Expected to not be SQL-escaped. |
||
| 184 | * @param string $key The value key. |
||
| 185 | * @param mixed $value The value. |
||
| 186 | * |
||
| 187 | * @since 3.9.0 |
||
| 188 | * |
||
| 189 | */ |
||
| 190 | private function set( $option, $key, $value ) { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Get the entity base path, by default 'entity'. |
||
| 201 | * |
||
| 202 | * @return string The entity base path. |
||
| 203 | * @since 3.6.0 |
||
| 204 | * |
||
| 205 | */ |
||
| 206 | public function get_entity_base_path() { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Get the entity base path. |
||
| 213 | * |
||
| 214 | * @param string $value The entity base path. |
||
| 215 | * |
||
| 216 | * @since 3.9.0 |
||
| 217 | * |
||
| 218 | */ |
||
| 219 | public function set_entity_base_path( $value ) { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Whether the installation skip wizard should be skipped. |
||
| 227 | * |
||
| 228 | * @return bool True if it should be skipped otherwise false. |
||
| 229 | * @since 3.9.0 |
||
| 230 | * |
||
| 231 | */ |
||
| 232 | public function is_skip_wizard() { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Set the skip wizard parameter. |
||
| 239 | * |
||
| 240 | * @param bool $value True to skip the wizard. We expect a boolean value. |
||
| 241 | * |
||
| 242 | * @since 3.9.0 |
||
| 243 | * |
||
| 244 | */ |
||
| 245 | public function set_skip_wizard( $value ) { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Get WordLift's key. |
||
| 253 | * |
||
| 254 | * @return string WordLift's key or an empty string if not set. |
||
| 255 | * @since 3.9.0 |
||
| 256 | * |
||
| 257 | */ |
||
| 258 | public function get_key() { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Set WordLift's key. |
||
| 265 | * |
||
| 266 | * @param string $value WordLift's key. |
||
| 267 | * |
||
| 268 | * @since 3.9.0 |
||
| 269 | * |
||
| 270 | */ |
||
| 271 | public function set_key( $value ) { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Get WordLift's configured language, by default 'en'. |
||
| 278 | * |
||
| 279 | * Note that WordLift's language is used when writing strings to the Linked Data dataset, not for the analysis. |
||
| 280 | * |
||
| 281 | * @return string WordLift's configured language code ('en' by default). |
||
| 282 | * @since 3.9.0 |
||
| 283 | * |
||
| 284 | */ |
||
| 285 | public function get_language_code() { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Set WordLift's language code, used when storing strings to the Linked Data dataset. |
||
| 292 | * |
||
| 293 | * @param string $value WordLift's language code. |
||
| 294 | * |
||
| 295 | * @since 3.9.0 |
||
| 296 | * |
||
| 297 | */ |
||
| 298 | public function set_language_code( $value ) { |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Set the user preferences about sharing diagnostic with us. |
||
| 306 | * |
||
| 307 | * @param string $value The user preferences(yes/no). |
||
| 308 | * |
||
| 309 | * @since 3.19.0 |
||
| 310 | * |
||
| 311 | */ |
||
| 312 | public function set_diagnostic_preferences( $value ) { |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Get the user preferences about sharing diagnostic. |
||
| 320 | * |
||
| 321 | * @since 3.19.0 |
||
| 322 | */ |
||
| 323 | public function get_diagnostic_preferences() { |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Get WordLift's configured country code, by default 'us'. |
||
| 330 | * |
||
| 331 | * @return string WordLift's configured country code ('us' by default). |
||
| 332 | * @since 3.18.0 |
||
| 333 | * |
||
| 334 | */ |
||
| 335 | public function get_country_code() { |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Set WordLift's country code. |
||
| 342 | * |
||
| 343 | * @param string $value WordLift's country code. |
||
| 344 | * |
||
| 345 | * @since 3.18.0 |
||
| 346 | * |
||
| 347 | */ |
||
| 348 | public function set_country_code( $value ) { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Get the publisher entity post id. |
||
| 356 | * |
||
| 357 | * The publisher entity post id points to an entity post which contains the data for the publisher used in schema.org |
||
| 358 | * Article markup. |
||
| 359 | * |
||
| 360 | * @return int|NULL The publisher entity post id or NULL if not set. |
||
| 361 | * @since 3.9.0 |
||
| 362 | * |
||
| 363 | */ |
||
| 364 | public function get_publisher_id() { |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Set the publisher entity post id. |
||
| 371 | * |
||
| 372 | * @param int $value The publisher entity post id. |
||
| 373 | * |
||
| 374 | * @since 3.9.0 |
||
| 375 | * |
||
| 376 | */ |
||
| 377 | public function set_publisher_id( $value ) { |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Get the dataset URI. |
||
| 385 | * |
||
| 386 | * @return string The dataset URI or an empty string if not set. |
||
| 387 | * @since 3.10.0 |
||
| 388 | * @since 3.27.7 Always return null if `wl_features__enable__dataset` is disabled. |
||
| 389 | * |
||
| 390 | */ |
||
| 391 | public function get_dataset_uri() { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Set the dataset URI. |
||
| 402 | * |
||
| 403 | * @param string $value The dataset URI. |
||
| 404 | * |
||
| 405 | * @since 3.10.0 |
||
| 406 | * |
||
| 407 | */ |
||
| 408 | public function set_dataset_uri( $value ) { |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Get the package type. |
||
| 415 | * |
||
| 416 | * @return string The package type or an empty string if not set. |
||
| 417 | * @since 3.20.0 |
||
| 418 | * |
||
| 419 | */ |
||
| 420 | public function get_package_type() { |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Set the package type. |
||
| 427 | * |
||
| 428 | * @param string $value The package type. |
||
| 429 | * |
||
| 430 | * @since 3.20.0 |
||
| 431 | * |
||
| 432 | */ |
||
| 433 | public function set_package_type( $value ) { |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Intercept the change of the WordLift key in order to set the dataset URI. |
||
| 440 | * |
||
| 441 | * |
||
| 442 | * @since 3.20.0 as of #761, we save settings every time a key is set, not only when the key changes, so to |
||
| 443 | * store the configuration parameters such as country or language. |
||
| 444 | * @since 3.11.0 |
||
| 445 | * |
||
| 446 | * @see https://github.com/insideout10/wordlift-plugin/issues/761 |
||
| 447 | * |
||
| 448 | * @param array $old_value The old settings. |
||
| 449 | * @param array $new_value The new settings. |
||
| 450 | */ |
||
| 451 | public function update_key( $old_value, $new_value ) { |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Handle retrieving the dataset uri from the remote server. |
||
| 475 | * |
||
| 476 | * If a valid dataset uri is returned it is stored in the appropriate option, |
||
| 477 | * otherwise the option is set to empty string. |
||
| 478 | * |
||
| 479 | * @param string $key The key to be used. |
||
| 480 | * |
||
| 481 | * @since 3.12.0 |
||
| 482 | * |
||
| 483 | * @since 3.17.0 send the site URL and get the dataset URI. |
||
| 484 | */ |
||
| 485 | public function get_remote_dataset_uri( $key ) { |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Handle the edge case where a user submits the same key again |
||
| 560 | * when he does not have the dataset uri to regain it. |
||
| 561 | * |
||
| 562 | * This can not be handled in the normal option update hook because |
||
| 563 | * it is not being triggered when the save value equals to the one already |
||
| 564 | * in the DB. |
||
| 565 | * |
||
| 566 | * @param mixed $value The new, unserialized option value. |
||
| 567 | * @param mixed $old_value The old option value. |
||
| 568 | * |
||
| 569 | * @return mixed The same value in the $value parameter |
||
| 570 | * @since 3.12.0 |
||
| 571 | * |
||
| 572 | */ |
||
| 573 | function maybe_update_dataset_uri( $value, $old_value ) { |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Get the API URI to retrieve the dataset URI using the WordLift Key. |
||
| 594 | * |
||
| 595 | * @param string $key The WordLift key to use. |
||
| 596 | * |
||
| 597 | * @return string The API URI. |
||
| 598 | * @since 3.11.0 |
||
| 599 | * |
||
| 600 | */ |
||
| 601 | public function get_accounts_by_key_dataset_uri( $key ) { |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Get the `accounts` end point. |
||
| 608 | * |
||
| 609 | * @return string The `accounts` end point. |
||
| 610 | * @since 3.16.0 |
||
| 611 | * |
||
| 612 | */ |
||
| 613 | public function get_accounts() { |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Get the `link by default` option. |
||
| 620 | * |
||
| 621 | * @return bool True if entities must be linked by default otherwise false. |
||
| 622 | * @since 3.13.0 |
||
| 623 | * |
||
| 624 | */ |
||
| 625 | public function is_link_by_default() { |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Set the `link by default` option. |
||
| 632 | * |
||
| 633 | * @param bool $value True to enabling linking by default, otherwise false. |
||
| 634 | * |
||
| 635 | * @since 3.13.0 |
||
| 636 | * |
||
| 637 | */ |
||
| 638 | public function set_link_by_default( $value ) { |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Get the 'analytics-enable' option. |
||
| 645 | * |
||
| 646 | * @return string 'no' or 'yes' representing bool. |
||
| 647 | * @since 3.21.0 |
||
| 648 | * |
||
| 649 | */ |
||
| 650 | public function is_analytics_enable() { |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Set the `analytics-enable` option. |
||
| 656 | * |
||
| 657 | * @param bool $value True to enabling analytics, otherwise false. |
||
| 658 | * |
||
| 659 | * @since 3.21.0 |
||
| 660 | * |
||
| 661 | */ |
||
| 662 | public function set_is_analytics_enable( $value ) { |
||
| 666 | |||
| 667 | /** |
||
| 668 | * Get the 'analytics-entity-uri-dimention' option. |
||
| 669 | * |
||
| 670 | * @return int |
||
| 671 | * @since 3.21.0 |
||
| 672 | * |
||
| 673 | */ |
||
| 674 | public function get_analytics_entity_uri_dimension() { |
||
| 677 | |||
| 678 | /** |
||
| 679 | * Get the 'analytics-entity-type-dimension' option. |
||
| 680 | * |
||
| 681 | * @return int |
||
| 682 | * @since 3.21.0 |
||
| 683 | * |
||
| 684 | */ |
||
| 685 | public function get_analytics_entity_type_dimension() { |
||
| 688 | |||
| 689 | /** |
||
| 690 | * Get the URL to perform autocomplete request. |
||
| 691 | * |
||
| 692 | * @return string The URL to call to perform the autocomplete request. |
||
| 693 | * @since 3.15.0 |
||
| 694 | * |
||
| 695 | */ |
||
| 696 | public function get_autocomplete_url() { |
||
| 701 | |||
| 702 | /** |
||
| 703 | * Get the URL to perform feedback deactivation request. |
||
| 704 | * |
||
| 705 | * @return string The URL to call to perform the feedback deactivation request. |
||
| 706 | * @since 3.19.0 |
||
| 707 | * |
||
| 708 | */ |
||
| 709 | public function get_deactivation_feedback_url() { |
||
| 714 | |||
| 715 | /** |
||
| 716 | * Get the base API URL. |
||
| 717 | * |
||
| 718 | * @return string The base API URL. |
||
| 719 | * @since 3.20.0 |
||
| 720 | * |
||
| 721 | */ |
||
| 722 | public function get_api_url() { |
||
| 726 | |||
| 727 | } |
||
| 728 |
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: