Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Test_REST_Endpoints 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 Test_REST_Endpoints, and based on these observations, apply Extract Interface, too.
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName | ||
| 21 | class Test_REST_Endpoints extends TestCase { | ||
| 22 | |||
| 23 | const BLOG_TOKEN = 'new.blogtoken'; | ||
| 24 | const BLOG_ID = 42; | ||
| 25 | const USER_ID = 111; | ||
| 26 | |||
| 27 | /** | ||
| 28 | * REST Server object. | ||
| 29 | * | ||
| 30 | * @var WP_REST_Server | ||
| 31 | */ | ||
| 32 | private $server; | ||
| 33 | |||
| 34 | /** | ||
| 35 | * The original hostname to restore after tests are finished. | ||
| 36 | * | ||
| 37 | * @var string | ||
| 38 | */ | ||
| 39 | private $api_host_original; | ||
| 40 | |||
| 41 | /** | ||
| 42 | * Setting up the test. | ||
| 43 | * | ||
| 44 | * @before | ||
| 45 | */ | ||
| 46 | 	public function set_up() { | ||
| 67 | |||
| 68 | /** | ||
| 69 | * Returning the environment into its initial state. | ||
| 70 | * | ||
| 71 | * @after | ||
| 72 | */ | ||
| 73 | 	public function tear_down() { | ||
| 85 | |||
| 86 | /** | ||
| 87 | * Testing the `/jetpack/v4/remote_authorize` endpoint. | ||
| 88 | */ | ||
| 89 | 	public function test_remote_authorize() { | ||
| 147 | |||
| 148 | /** | ||
| 149 | * Testing the `/jetpack/v4/connection` endpoint. | ||
| 150 | */ | ||
| 151 | 	public function test_connection() { | ||
| 166 | |||
| 167 | /** | ||
| 168 | * Testing the `/jetpack/v4/connection/plugins` endpoint. | ||
| 169 | */ | ||
| 170 | 	public function test_connection_plugins() { | ||
| 202 | |||
| 203 | /** | ||
| 204 | * Testing the `connection/reconnect` endpoint, full reconnect. | ||
| 205 | */ | ||
| 206 | 	public function test_connection_reconnect_full() { | ||
| 222 | |||
| 223 | /** | ||
| 224 | * Testing the `connection/reconnect` endpoint, successful partial reconnect (blog token). | ||
| 225 | */ | ||
| 226 | View Code Duplication | 	public function test_connection_reconnect_partial_blog_token_success() { | |
| 239 | |||
| 240 | /** | ||
| 241 | * Testing the `connection/reconnect` endpoint, failed partial reconnect (blog token). | ||
| 242 | */ | ||
| 243 | View Code Duplication | 	public function test_connection_reconnect_partial_blog_token_fail() { | |
| 256 | |||
| 257 | /** | ||
| 258 | * Testing the `connection/reconnect` endpoint, successful partial reconnect (user token). | ||
| 259 | */ | ||
| 260 | 	public function test_connection_reconnect_partial_user_token_success() { | ||
| 272 | |||
| 273 | /** | ||
| 274 | * Testing the `connection/reconnect` endpoint, userless (full reconnect). | ||
| 275 | */ | ||
| 276 | 	public function test_connection_reconnect_userless() { | ||
| 291 | |||
| 292 | /** | ||
| 293 | * This filter callback allow us to skip the database query by `Jetpack_Options` to retrieve the option. | ||
| 294 | * | ||
| 295 | * @param array $options List of options already skipping the database request. | ||
| 296 | * | ||
| 297 | * @return array | ||
| 298 | */ | ||
| 299 | 	public function bypass_raw_options( array $options ) { | ||
| 304 | |||
| 305 | /** | ||
| 306 | * Intercept the `jetpack.register` API request sent to WP.com, and mock the response. | ||
| 307 | * | ||
| 308 | * @param bool|array $response The existing response. | ||
| 309 | * @param array $args The request arguments. | ||
| 310 | * @param string $url The request URL. | ||
| 311 | * | ||
| 312 | * @return array | ||
| 313 | */ | ||
| 314 | 	public function intercept_register_request( $response, $args, $url ) { | ||
| 333 | |||
| 334 | /** | ||
| 335 | * Intercept the `jetpack-token-health` API request sent to WP.com, and mock the "invalid blog token" response. | ||
| 336 | * | ||
| 337 | * @param bool|array $response The existing response. | ||
| 338 | * @param array $args The request arguments. | ||
| 339 | * @param string $url The request URL. | ||
| 340 | * | ||
| 341 | * @return array | ||
| 342 | */ | ||
| 343 | 	public function intercept_validate_tokens_request_invalid_blog_token( $response, $args, $url ) { | ||
| 350 | |||
| 351 | /** | ||
| 352 | * Intercept the `jetpack-token-health` API request sent to WP.com, and mock the "invalid user token" response. | ||
| 353 | * | ||
| 354 | * @param bool|array $response The existing response. | ||
| 355 | * @param array $args The request arguments. | ||
| 356 | * @param string $url The request URL. | ||
| 357 | * | ||
| 358 | * @return array | ||
| 359 | */ | ||
| 360 | 	public function intercept_validate_tokens_request_invalid_user_token( $response, $args, $url ) { | ||
| 367 | |||
| 368 | /** | ||
| 369 | * Intercept the `jetpack-token-health` API request sent to WP.com, and mock the "valid tokens" response. | ||
| 370 | * | ||
| 371 | * @param bool|array $response The existing response. | ||
| 372 | * @param array $args The request arguments. | ||
| 373 | * @param string $url The request URL. | ||
| 374 | * | ||
| 375 | * @return array | ||
| 376 | */ | ||
| 377 | 	public function intercept_validate_tokens_request_valid_tokens( $response, $args, $url ) { | ||
| 384 | |||
| 385 | /** | ||
| 386 | * Build the response for a tokens validation request | ||
| 387 | * | ||
| 388 | * @param string $invalid_token Accepted values: 'blog_token', 'user_token'. | ||
| 389 | * | ||
| 390 | * @return array | ||
| 391 | */ | ||
| 392 | 	private function build_validate_tokens_response( $invalid_token ) { | ||
| 427 | |||
| 428 | /** | ||
| 429 | * Intercept the `jetpack-refresh-blog-token` API request sent to WP.com, and mock the success response. | ||
| 430 | * | ||
| 431 | * @param bool|array $response The existing response. | ||
| 432 | * @param array $args The request arguments. | ||
| 433 | * @param string $url The request URL. | ||
| 434 | * | ||
| 435 | * @return array | ||
| 436 | */ | ||
| 437 | View Code Duplication | 	public function intercept_refresh_blog_token_request( $response, $args, $url ) { | |
| 451 | |||
| 452 | /** | ||
| 453 | * Intercept the `jetpack-refresh-blog-token` API request sent to WP.com, and mock the failure response. | ||
| 454 | * | ||
| 455 | * @param bool|array $response The existing response. | ||
| 456 | * @param array $args The request arguments. | ||
| 457 | * @param string $url The request URL. | ||
| 458 | * | ||
| 459 | * @return array | ||
| 460 | */ | ||
| 461 | View Code Duplication | 	public function intercept_refresh_blog_token_request_fail( $response, $args, $url ) { | |
| 475 | |||
| 476 | /** | ||
| 477 | * Intercept the `jetpack-token-health` API request sent to WP.com, and mock the "invalid blog token" response. | ||
| 478 | * | ||
| 479 | * @param bool|array $response The existing response. | ||
| 480 | * @param array $args The request arguments. | ||
| 481 | * @param string $url The request URL. | ||
| 482 | * | ||
| 483 | * @return array | ||
| 484 | */ | ||
| 485 | 	public function intercept_auth_token_request( $response, $args, $url ) { | ||
| 505 | |||
| 506 | /** | ||
| 507 | * Intercept the `Jetpack_Options` call and mock the values. | ||
| 508 | * Site level / user-less connection set-up. | ||
| 509 | * | ||
| 510 | * @param mixed $value The current option value. | ||
| 511 | * @param string $name Option name. | ||
| 512 | * | ||
| 513 | * @return mixed | ||
| 514 | */ | ||
| 515 | 	public function mock_jetpack_userless_options( $value, $name ) { | ||
| 525 | |||
| 526 | /** | ||
| 527 | * Intercept the `Jetpack_Options` call and mock the values. | ||
| 528 | * Full connection set-up. | ||
| 529 | * | ||
| 530 | * @param mixed $value The current option value. | ||
| 531 | * @param string $name Option name. | ||
| 532 | * | ||
| 533 | * @return mixed | ||
| 534 | */ | ||
| 535 | 	public function mock_jetpack_options( $value, $name ) { | ||
| 551 | |||
| 552 | /** | ||
| 553 | * Build the `connection/reconnect` request object. | ||
| 554 | * | ||
| 555 | * @return WP_REST_Request | ||
| 556 | */ | ||
| 557 | 	private function build_reconnect_request() { | ||
| 563 | |||
| 564 | /** | ||
| 565 | * Setup the environment to test the reconnection process. | ||
| 566 | * | ||
| 567 | * @param string|null $invalid_token The invalid token to be returned in the response. Null if the tokens should be valid. | ||
| 568 | */ | ||
| 569 | View Code Duplication | 	private function setup_reconnect_test( $invalid_token ) { | |
| 608 | |||
| 609 | /** | ||
| 610 | * Restore the environment after the `reconnect` test has been run. | ||
| 611 | * | ||
| 612 | * @param string|null $invalid_token The invalid token to be returned in the response. Null if the tokens should be valid. | ||
| 613 | */ | ||
| 614 | View Code Duplication | 	private function shutdown_reconnect_test( $invalid_token ) { | |
| 651 | |||
| 652 | } | ||
| 653 |