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 | * The publisher entity post ID option name. |
||
| 54 | * |
||
| 55 | * @since 3.9.0 |
||
| 56 | */ |
||
| 57 | const PUBLISHER_ID = 'publisher_id'; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The dataset URI option name |
||
| 61 | * |
||
| 62 | * @since 3.10.0 |
||
| 63 | */ |
||
| 64 | const DATASET_URI = 'redlink_dataset_uri'; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The link by default option name. |
||
| 68 | * |
||
| 69 | * @since 3.11.0 |
||
| 70 | */ |
||
| 71 | const LINK_BY_DEFAULT = 'link_by_default'; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * The user preferences about sharing data option. |
||
| 75 | * |
||
| 76 | * @since 3.19.0 |
||
| 77 | */ |
||
| 78 | const SEND_DIAGNOSTIC = 'send_diagnostic'; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * The {@link Wordlift_Log_Service} instance. |
||
| 82 | * |
||
| 83 | * @since 3.16.0 |
||
| 84 | * |
||
| 85 | * @var \Wordlift_Log_Service $log The {@link Wordlift_Log_Service} instance. |
||
| 86 | */ |
||
| 87 | private $log; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * The Wordlift_Configuration_Service's singleton instance. |
||
| 91 | * |
||
| 92 | * @since 3.6.0 |
||
| 93 | * |
||
| 94 | * @access private |
||
| 95 | * @var \Wordlift_Configuration_Service $instance Wordlift_Configuration_Service's singleton instance. |
||
| 96 | */ |
||
| 97 | private static $instance; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Create a Wordlift_Configuration_Service's instance. |
||
| 101 | * |
||
| 102 | * @since 3.6.0 |
||
| 103 | */ |
||
| 104 | public function __construct() { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Get the singleton instance. |
||
| 114 | * |
||
| 115 | * @since 3.6.0 |
||
| 116 | * |
||
| 117 | * @return \Wordlift_Configuration_Service |
||
| 118 | */ |
||
| 119 | public static function get_instance() { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Get a configuration given the option name and a key. The option value is |
||
| 126 | * expected to be an array. |
||
| 127 | * |
||
| 128 | * @since 3.6.0 |
||
| 129 | * |
||
| 130 | * @param string $option The option name. |
||
| 131 | * @param string $key A key in the option value array. |
||
| 132 | * @param string $default The default value in case the key is not found (by default an empty string). |
||
| 133 | * |
||
| 134 | * @return mixed The configuration value or the default value if not found. |
||
| 135 | */ |
||
| 136 | private function get( $option, $key, $default = '' ) { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Set a configuration parameter. |
||
| 145 | * |
||
| 146 | * @since 3.9.0 |
||
| 147 | * |
||
| 148 | * @param string $option Name of option to retrieve. Expected to not be SQL-escaped. |
||
| 149 | * @param string $key The value key. |
||
| 150 | * @param mixed $value The value. |
||
| 151 | */ |
||
| 152 | private function set( $option, $key, $value ) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Get the entity base path, by default 'entity'. |
||
| 163 | * |
||
| 164 | * @since 3.6.0 |
||
| 165 | * |
||
| 166 | * @return string The entity base path. |
||
| 167 | */ |
||
| 168 | public function get_entity_base_path() { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Get the entity base path. |
||
| 175 | * |
||
| 176 | * @since 3.9.0 |
||
| 177 | * |
||
| 178 | * @param string $value The entity base path. |
||
| 179 | */ |
||
| 180 | public function set_entity_base_path( $value ) { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Whether the installation skip wizard should be skipped. |
||
| 188 | * |
||
| 189 | * @since 3.9.0 |
||
| 190 | * |
||
| 191 | * @return bool True if it should be skipped otherwise false. |
||
| 192 | */ |
||
| 193 | public function is_skip_wizard() { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Set the skip wizard parameter. |
||
| 200 | * |
||
| 201 | * @since 3.9.0 |
||
| 202 | * |
||
| 203 | * @param bool $value True to skip the wizard. We expect a boolean value. |
||
| 204 | */ |
||
| 205 | public function set_skip_wizard( $value ) { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Get WordLift's key. |
||
| 213 | * |
||
| 214 | * @since 3.9.0 |
||
| 215 | * |
||
| 216 | * @return string WordLift's key or an empty string if not set. |
||
| 217 | */ |
||
| 218 | public function get_key() { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Set WordLift's key. |
||
| 225 | * |
||
| 226 | * @since 3.9.0 |
||
| 227 | * |
||
| 228 | * @param string $value WordLift's key. |
||
| 229 | */ |
||
| 230 | public function set_key( $value ) { |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Get WordLift's configured language, by default 'en'. |
||
| 237 | * |
||
| 238 | * Note that WordLift's language is used when writing strings to the Linked Data dataset, not for the analysis. |
||
| 239 | * |
||
| 240 | * @since 3.9.0 |
||
| 241 | * |
||
| 242 | * @return string WordLift's configured language code ('en' by default). |
||
| 243 | */ |
||
| 244 | public function get_language_code() { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Set WordLift's language code, used when storing strings to the Linked Data dataset. |
||
| 251 | * |
||
| 252 | * @since 3.9.0 |
||
| 253 | * |
||
| 254 | * @param string $value WordLift's language code. |
||
| 255 | */ |
||
| 256 | public function set_language_code( $value ) { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Set the user preferences about sharing diagnostic with us. |
||
| 264 | * |
||
| 265 | * @since 3.19.0 |
||
| 266 | * |
||
| 267 | * @param string $value The user preferences(yes/no). |
||
| 268 | */ |
||
| 269 | public function set_diagnostic_preferences( $value ) { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Get the user preferences about sharing diagnostic. |
||
| 277 | * |
||
| 278 | * @since 3.19.0 |
||
| 279 | */ |
||
| 280 | public function get_diagnostic_preferences() { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Get the publisher entity post id. |
||
| 288 | * |
||
| 289 | * The publisher entity post id points to an entity post which contains the data for the publisher used in schema.org |
||
| 290 | * Article markup. |
||
| 291 | * |
||
| 292 | * @since 3.9.0 |
||
| 293 | * |
||
| 294 | * @return int|NULL The publisher entity post id or NULL if not set. |
||
| 295 | */ |
||
| 296 | public function get_publisher_id() { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Set the publisher entity post id. |
||
| 303 | * |
||
| 304 | * @since 3.9.0 |
||
| 305 | * |
||
| 306 | * @param int $value The publisher entity post id. |
||
| 307 | */ |
||
| 308 | public function set_publisher_id( $value ) { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Get the dataset URI. |
||
| 316 | * |
||
| 317 | * @since 3.10.0 |
||
| 318 | * |
||
| 319 | * @return string The dataset URI or an empty string if not set. |
||
| 320 | */ |
||
| 321 | public function get_dataset_uri() { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Set the dataset URI. |
||
| 328 | * |
||
| 329 | * @since 3.10.0 |
||
| 330 | * |
||
| 331 | * @param string $value The dataset URI. |
||
| 332 | */ |
||
| 333 | public function set_dataset_uri( $value ) { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Intercept the change of the WordLift key in order to set the dataset URI. |
||
| 340 | * |
||
| 341 | * @since 3.11.0 |
||
| 342 | * |
||
| 343 | * @param array $old_value The old settings. |
||
| 344 | * @param array $new_value The new settings. |
||
| 345 | */ |
||
| 346 | public function update_key( $old_value, $new_value ) { |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Handle retrieving the dataset uri from the remote server. |
||
| 369 | * |
||
| 370 | * If a valid dataset uri is returned it is stored in the appropriate option, |
||
| 371 | * otherwise the option is set to empty string. |
||
| 372 | * |
||
| 373 | * @since 3.17.0 send the site URL and get the dataset URI. |
||
| 374 | * @since 3.12.0 |
||
| 375 | * |
||
| 376 | * @param string $key The key to be used. |
||
| 377 | */ |
||
| 378 | public function get_remote_dataset_uri( $key ) { |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Handle the edge case where a user submits the same key again |
||
| 421 | * when he does not have the dataset uri to regain it. |
||
| 422 | * |
||
| 423 | * This can not be handled in the normal option update hook because |
||
| 424 | * it is not being triggered when the save value equals to the one already |
||
| 425 | * in the DB. |
||
| 426 | * |
||
| 427 | * @since 3.12.0 |
||
| 428 | * |
||
| 429 | * @param mixed $value The new, unserialized option value. |
||
| 430 | * @param mixed $old_value The old option value. |
||
| 431 | * |
||
| 432 | * @return mixed The same value in the $value parameter |
||
| 433 | */ |
||
| 434 | function maybe_update_dataset_uri( $value, $old_value ) { |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Get the API URI to retrieve the dataset URI using the WordLift Key. |
||
| 455 | * |
||
| 456 | * @since 3.11.0 |
||
| 457 | * |
||
| 458 | * @param string $key The WordLift key to use. |
||
| 459 | * |
||
| 460 | * @return string The API URI. |
||
| 461 | */ |
||
| 462 | public function get_accounts_by_key_dataset_uri( $key ) { |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Get the `accounts` end point. |
||
| 469 | * |
||
| 470 | * @since 3.16.0 |
||
| 471 | * |
||
| 472 | * @return string The `accounts` end point. |
||
| 473 | */ |
||
| 474 | public function get_accounts() { |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Get the `link by default` option. |
||
| 481 | * |
||
| 482 | * @since 3.13.0 |
||
| 483 | * |
||
| 484 | * @return bool True if entities must be linked by default otherwise false. |
||
| 485 | */ |
||
| 486 | public function is_link_by_default() { |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Set the `link by default` option. |
||
| 493 | * |
||
| 494 | * @since 3.13.0 |
||
| 495 | * |
||
| 496 | * @param bool $value True to enabling linking by default, otherwise false. |
||
| 497 | */ |
||
| 498 | public function set_link_by_default( $value ) { |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Get the URL to perform batch analyses. |
||
| 505 | * |
||
| 506 | * @since 3.14.0 |
||
| 507 | * |
||
| 508 | * @return string The URL to call to perform the batch analyzes. |
||
| 509 | */ |
||
| 510 | public function get_batch_analysis_url() { |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Get the URL to perform autocomplete request. |
||
| 518 | * |
||
| 519 | * @since 3.15.0 |
||
| 520 | * |
||
| 521 | * @return string The URL to call to perform the batch analyzes. |
||
| 522 | */ |
||
| 523 | public function get_autocomplete_url() { |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Get the URL to perform feedback deactivation request. |
||
| 531 | * |
||
| 532 | * @since 3.19.0 |
||
| 533 | * |
||
| 534 | * @return string The URL to call to perform the feedback deactivation request. |
||
| 535 | */ |
||
| 536 | public function get_deactivation_feedback_url() { |
||
| 541 | |||
| 542 | } |
||
| 543 |
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: