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` endpoint jetpack_connection_status filter. |
||
169 | */ |
||
170 | public function test_connection_jetpack_connection_status_filter() { |
||
189 | |||
190 | /** |
||
191 | * Testing the `/jetpack/v4/connection/plugins` endpoint. |
||
192 | */ |
||
193 | public function test_connection_plugins() { |
||
225 | |||
226 | /** |
||
227 | * Testing the `connection/reconnect` endpoint, full reconnect. |
||
228 | */ |
||
229 | public function test_connection_reconnect_full() { |
||
245 | |||
246 | /** |
||
247 | * Testing the `connection/reconnect` endpoint, successful partial reconnect (blog token). |
||
248 | */ |
||
249 | View Code Duplication | public function test_connection_reconnect_partial_blog_token_success() { |
|
262 | |||
263 | /** |
||
264 | * Testing the `connection/reconnect` endpoint, failed partial reconnect (blog token). |
||
265 | */ |
||
266 | View Code Duplication | public function test_connection_reconnect_partial_blog_token_fail() { |
|
279 | |||
280 | /** |
||
281 | * Testing the `connection/reconnect` endpoint, successful partial reconnect (user token). |
||
282 | */ |
||
283 | public function test_connection_reconnect_partial_user_token_success() { |
||
295 | |||
296 | /** |
||
297 | * Testing the `connection/reconnect` endpoint, userless (full reconnect). |
||
298 | */ |
||
299 | public function test_connection_reconnect_userless() { |
||
314 | |||
315 | /** |
||
316 | * Testing the `connection/reconnect` endpoint when the token validation request fails. |
||
317 | */ |
||
318 | public function test_connection_reconnect_when_token_validation_request_fails() { |
||
334 | |||
335 | /** |
||
336 | * This filter callback allow us to skip the database query by `Jetpack_Options` to retrieve the option. |
||
337 | * |
||
338 | * @param array $options List of options already skipping the database request. |
||
339 | * |
||
340 | * @return array |
||
341 | */ |
||
342 | public function bypass_raw_options( array $options ) { |
||
347 | |||
348 | /** |
||
349 | * Intercept the `jetpack.register` API request sent to WP.com, and mock the response. |
||
350 | * |
||
351 | * @param bool|array $response The existing response. |
||
352 | * @param array $args The request arguments. |
||
353 | * @param string $url The request URL. |
||
354 | * |
||
355 | * @return array |
||
356 | */ |
||
357 | public function intercept_register_request( $response, $args, $url ) { |
||
376 | |||
377 | /** |
||
378 | * Intercept the `jetpack-token-health` API request sent to WP.com, and mock the "invalid blog token" response. |
||
379 | * |
||
380 | * @param bool|array $response The existing response. |
||
381 | * @param array $args The request arguments. |
||
382 | * @param string $url The request URL. |
||
383 | * |
||
384 | * @return array |
||
385 | */ |
||
386 | public function intercept_validate_tokens_request_invalid_blog_token( $response, $args, $url ) { |
||
393 | |||
394 | /** |
||
395 | * Intercept the `jetpack-token-health` API request sent to WP.com, and mock the "invalid user token" response. |
||
396 | * |
||
397 | * @param bool|array $response The existing response. |
||
398 | * @param array $args The request arguments. |
||
399 | * @param string $url The request URL. |
||
400 | * |
||
401 | * @return array |
||
402 | */ |
||
403 | public function intercept_validate_tokens_request_invalid_user_token( $response, $args, $url ) { |
||
410 | |||
411 | /** |
||
412 | * Intercept the `jetpack-token-health` API request sent to WP.com, and mock the "valid tokens" response. |
||
413 | * |
||
414 | * @param bool|array $response The existing response. |
||
415 | * @param array $args The request arguments. |
||
416 | * @param string $url The request URL. |
||
417 | * |
||
418 | * @return array |
||
419 | */ |
||
420 | public function intercept_validate_tokens_request_valid_tokens( $response, $args, $url ) { |
||
427 | |||
428 | /** |
||
429 | * Intercept the `jetpack-token-health` API request sent to WP.com, and mock failed 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_validate_tokens_request_failed( $response, $args, $url ) { |
|
451 | |||
452 | /** |
||
453 | * Build the response for a tokens validation request |
||
454 | * |
||
455 | * @param string $invalid_token Accepted values: 'blog_token', 'user_token'. |
||
456 | * |
||
457 | * @return array |
||
458 | */ |
||
459 | private function build_validate_tokens_response( $invalid_token ) { |
||
494 | |||
495 | /** |
||
496 | * Intercept the `jetpack-refresh-blog-token` API request sent to WP.com, and mock the success response. |
||
497 | * |
||
498 | * @param bool|array $response The existing response. |
||
499 | * @param array $args The request arguments. |
||
500 | * @param string $url The request URL. |
||
501 | * |
||
502 | * @return array |
||
503 | */ |
||
504 | View Code Duplication | public function intercept_refresh_blog_token_request( $response, $args, $url ) { |
|
518 | |||
519 | /** |
||
520 | * Intercept the `jetpack-refresh-blog-token` API request sent to WP.com, and mock the failure response. |
||
521 | * |
||
522 | * @param bool|array $response The existing response. |
||
523 | * @param array $args The request arguments. |
||
524 | * @param string $url The request URL. |
||
525 | * |
||
526 | * @return array |
||
527 | */ |
||
528 | View Code Duplication | public function intercept_refresh_blog_token_request_fail( $response, $args, $url ) { |
|
542 | |||
543 | /** |
||
544 | * Intercept the `jetpack-token-health` API request sent to WP.com, and mock the "invalid blog token" response. |
||
545 | * |
||
546 | * @param bool|array $response The existing response. |
||
547 | * @param array $args The request arguments. |
||
548 | * @param string $url The request URL. |
||
549 | * |
||
550 | * @return array |
||
551 | */ |
||
552 | public function intercept_auth_token_request( $response, $args, $url ) { |
||
572 | |||
573 | /** |
||
574 | * Intercept the `Jetpack_Options` call and mock the values. |
||
575 | * Site level / user-less connection set-up. |
||
576 | * |
||
577 | * @param mixed $value The current option value. |
||
578 | * @param string $name Option name. |
||
579 | * |
||
580 | * @return mixed |
||
581 | */ |
||
582 | public function mock_jetpack_userless_options( $value, $name ) { |
||
592 | |||
593 | /** |
||
594 | * Intercept the `Jetpack_Options` call and mock the values. |
||
595 | * Full connection set-up. |
||
596 | * |
||
597 | * @param mixed $value The current option value. |
||
598 | * @param string $name Option name. |
||
599 | * |
||
600 | * @return mixed |
||
601 | */ |
||
602 | public function mock_jetpack_options( $value, $name ) { |
||
618 | |||
619 | /** |
||
620 | * Build the `connection/reconnect` request object. |
||
621 | * |
||
622 | * @return WP_REST_Request |
||
623 | */ |
||
624 | private function build_reconnect_request() { |
||
630 | |||
631 | /** |
||
632 | * Setup the environment to test the reconnection process. |
||
633 | * |
||
634 | * @param string|null $invalid_token The invalid token to be returned in the response. Null if the tokens should be valid. |
||
635 | */ |
||
636 | private function setup_reconnect_test( $invalid_token ) { |
||
686 | |||
687 | /** |
||
688 | * Restore the environment after the `reconnect` test has been run. |
||
689 | * |
||
690 | * @param string|null $invalid_token The invalid token to be returned in the response. Null if the tokens should be valid. |
||
691 | */ |
||
692 | private function shutdown_reconnect_test( $invalid_token ) { |
||
739 | |||
740 | } |
||
741 |