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 |
||
17 | class Test_Abtest extends TestCase { |
||
18 | /** |
||
19 | * Test setup. |
||
20 | * |
||
21 | * @before |
||
22 | */ |
||
23 | public function set_up() { |
||
28 | |||
29 | /** |
||
30 | * Tests with no test name provided. |
||
31 | * |
||
32 | * @covers Automattic\Jetpack\Abtest::get_variation |
||
33 | */ |
||
34 | public function test_with_no_test_name_provided() { |
||
38 | |||
39 | /** |
||
40 | * Tests when incorrect test name is provided. |
||
41 | * |
||
42 | * @covers Automattic\Jetpack\Abtest::get_variation |
||
43 | */ |
||
44 | public function test_with_incorrect_test_name_provided() { |
||
48 | |||
49 | /** |
||
50 | * Tests when a test is inactive or does not exist. |
||
51 | * |
||
52 | * @covers Automattic\Jetpack\Abtest::get_variation |
||
53 | */ |
||
54 | public function test_when_test_inactive_or_does_not_exist() { |
||
55 | $this->abtest->expects( $this->once() ) |
||
56 | ->method( 'request_variation' ) |
||
57 | ->willReturn( |
||
58 | array( |
||
59 | 'body' => json_encode( // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode |
||
60 | array( |
||
61 | 'code' => 'incorrect_test_name', |
||
62 | 'message' => 'This A/B test does not exist or is currently inactive.', |
||
63 | ) |
||
64 | ), |
||
65 | ) |
||
66 | ); |
||
67 | |||
68 | $result = $this->abtest->get_variation( 'example_test' ); |
||
69 | $this->assertNull( $result ); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Tests an error or malformed response. |
||
74 | * |
||
75 | * @covers Automattic\Jetpack\Abtest::get_variation |
||
76 | */ |
||
77 | View Code Duplication | public function test_when_error_or_malformed_response() { |
|
89 | |||
90 | /** |
||
91 | * Tests when the response is in an unexpected format. |
||
92 | * |
||
93 | * @covers Automattic\Jetpack\Abtest::get_variation |
||
94 | */ |
||
95 | View Code Duplication | public function test_when_response_in_unexpected_format() { |
|
111 | |||
112 | /** |
||
113 | * Test with an active test. |
||
114 | * |
||
115 | * @covers Automattic\Jetpack\Abtest::get_variation |
||
116 | */ |
||
117 | public function test_with_valid_active_test() { |
||
138 | } |
||
139 |