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:
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
||
| 20 | class Test_REST_Endpoints extends TestCase { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * REST Server object. |
||
| 24 | * |
||
| 25 | * @var WP_REST_Server |
||
| 26 | */ |
||
| 27 | private $server; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The original hostname to restore after tests are finished. |
||
| 31 | * |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | private $api_host_original; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Setting up the test. |
||
| 38 | * |
||
| 39 | * @before |
||
| 40 | */ |
||
| 41 | public function set_up() { |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Returning the environment into its initial state. |
||
| 62 | * |
||
| 63 | * @after |
||
| 64 | */ |
||
| 65 | public function tear_down() { |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Testing the `/jetpack/v4/identity-crisis/confirm-safe-mode` endpoint. |
||
| 76 | */ |
||
| 77 | public function test_confirm_safe_mode() { |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Testing the `/jetpack/v4/identity-crisis/confirm-safe-mode` endpoint returns an error when user does not have permissions. |
||
| 99 | */ |
||
| 100 | public function test_confirm_safe_mode_no_access() { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Testing the `/jetpack/v4/identity-crisis/migrate` endpoint. |
||
| 115 | */ |
||
| 116 | public function test_migrate_stats_and_subscribers() { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Testing the `/jetpack/v4/identity-crisis/migrate` endpoint returns an error when user does not have permissions. |
||
| 140 | */ |
||
| 141 | public function test_migrate_stats_and_subscribers_no_access() { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Testing the `/jetpack/v4/identity-crisis/start-fresh` endpoint. |
||
| 159 | */ |
||
| 160 | View Code Duplication | public function test_start_fresh() { |
|
| 161 | |||
| 162 | $user = wp_get_current_user(); |
||
| 163 | $user->add_cap( 'jetpack_disconnect' ); |
||
| 164 | |||
| 165 | $request = new WP_REST_Request( 'POST', '/jetpack/v4/identity-crisis/start-fresh' ); |
||
| 166 | $request->set_header( 'Content-Type', 'application/json' ); |
||
| 167 | |||
| 168 | $response = $this->server->dispatch( $request ); |
||
| 169 | $data = $response->get_data(); |
||
| 170 | |||
| 171 | $user->remove_cap( 'jetpack_disconnect' ); |
||
| 172 | |||
| 173 | $this->assertEquals( 200, $response->get_status() ); |
||
| 174 | $this->assertSame( '', $data ); |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Testing the `/jetpack/v4/identity-crisis/start-fresh` endpoint returns an error when user does not have permissions. |
||
| 179 | */ |
||
| 180 | public function test_start_fresh_no_access() { |
||
| 188 | |||
| 189 | } |
||
| 190 |