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.NotHyphenatedLowercase |
||
18 | class ManagerTest extends TestCase { |
||
19 | |||
20 | /** |
||
21 | * Temporary stack for `wp_redirect`. |
||
22 | * |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $arguments_stack = array(); |
||
26 | |||
27 | /** |
||
28 | * User ID added for the test. |
||
29 | * |
||
30 | * @var int |
||
31 | */ |
||
32 | protected $user_id; |
||
33 | |||
34 | const DEFAULT_TEST_CAPS = array( 'default_test_caps' ); |
||
35 | |||
36 | /** |
||
37 | * Initialize the object before running the test method. |
||
38 | * |
||
39 | * @before |
||
40 | */ |
||
41 | public function set_up() { |
||
54 | |||
55 | /** |
||
56 | * Clean up the testing environment. |
||
57 | * |
||
58 | * @after |
||
59 | */ |
||
60 | public function tear_down() { |
||
66 | |||
67 | /** |
||
68 | * Test the `is_active` functionality when connected. |
||
69 | * |
||
70 | * @covers Automattic\Jetpack\Connection\Manager::is_active |
||
71 | */ |
||
72 | public function test_is_active_when_connected() { |
||
83 | |||
84 | /** |
||
85 | * Test the `is_active` functionality when not connected. |
||
86 | * |
||
87 | * @covers Automattic\Jetpack\Connection\Manager::is_active |
||
88 | */ |
||
89 | public function test_is_active_when_not_connected() { |
||
96 | |||
97 | /** |
||
98 | * Test the `api_url` generation. |
||
99 | * |
||
100 | * @covers Automattic\Jetpack\Connection\Manager::api_url |
||
101 | */ |
||
102 | public function test_api_url_defaults() { |
||
116 | |||
117 | /** |
||
118 | * Testing the ability of the api_url method to follow set constants and filters. |
||
119 | * |
||
120 | * @covers Automattic\Jetpack\Connection\Manager::api_url |
||
121 | */ |
||
122 | public function test_api_url_uses_constants_and_filters() { |
||
163 | |||
164 | /** |
||
165 | * Test the `is_user_connected` functionality. |
||
166 | * |
||
167 | * @covers Automattic\Jetpack\Connection\Manager::is_user_connected |
||
168 | */ |
||
169 | public function test_is_user_connected_with_default_user_id_logged_out() { |
||
172 | |||
173 | /** |
||
174 | * Test the `is_user_connected` functionality. |
||
175 | * |
||
176 | * @covers Automattic\Jetpack\Connection\Manager::is_user_connected |
||
177 | */ |
||
178 | public function test_is_user_connected_with_false_user_id_logged_out() { |
||
181 | |||
182 | /** |
||
183 | * Test the `is_user_connected` functionality |
||
184 | * |
||
185 | * @covers Automattic\Jetpack\Connection\Manager::is_user_connected |
||
186 | */ |
||
187 | public function test_is_user_connected_with_user_id_logged_out_not_connected() { |
||
194 | |||
195 | /** |
||
196 | * Test the `is_user_connected` functionality. |
||
197 | * |
||
198 | * @covers Automattic\Jetpack\Connection\Manager::is_user_connected |
||
199 | */ |
||
200 | View Code Duplication | public function test_is_user_connected_with_default_user_id_logged_in() { |
|
213 | |||
214 | /** |
||
215 | * Test the `is_user_connected` functionality. |
||
216 | * |
||
217 | * @covers Automattic\Jetpack\Connection\Manager::is_user_connected |
||
218 | */ |
||
219 | View Code Duplication | public function test_is_user_connected_with_user_id_logged_in() { |
|
230 | |||
231 | /** |
||
232 | * Unit test for the "Delete all tokens" functionality. |
||
233 | * |
||
234 | * @covers Automattic\Jetpack\Connection\Manager::delete_all_connection_tokens |
||
235 | */ |
||
236 | View Code Duplication | public function test_delete_all_connection_tokens() { |
|
248 | |||
249 | /** |
||
250 | * Unit test for the "Disconnect from WP" functionality. |
||
251 | * |
||
252 | * @covers Automattic\Jetpack\Connection\Manager::disconnect_site_wpcom |
||
253 | */ |
||
254 | View Code Duplication | public function test_disconnect_site_wpcom() { |
|
266 | |||
267 | /** |
||
268 | * Test the `jetpack_connection_custom_caps' method. |
||
269 | * |
||
270 | * @covers Automattic\Jetpack\Connection\Manager::jetpack_connection_custom_caps |
||
271 | * @dataProvider jetpack_connection_custom_caps_data_provider |
||
272 | * |
||
273 | * @param bool $in_offline_mode Whether offline mode is active. |
||
274 | * @param string $custom_cap The custom capability that is being tested. |
||
275 | * @param array $expected_caps The expected output. |
||
276 | */ |
||
277 | public function test_jetpack_connection_custom_caps( $in_offline_mode, $custom_cap, $expected_caps ) { |
||
289 | |||
290 | /** |
||
291 | * Data provider test_jetpack_connection_custom_caps. |
||
292 | * |
||
293 | * Structure of the test data arrays: |
||
294 | * [0] => 'in_offline_mode' boolean Whether offline mode is active. |
||
295 | * [1] => 'custom_cap' string The custom capability that is being tested. |
||
296 | * [2] => 'expected_caps' array The expected output of the call to jetpack_connection_custom_caps. |
||
297 | */ |
||
298 | public function jetpack_connection_custom_caps_data_provider() { |
||
313 | |||
314 | /** |
||
315 | * Test the `get_signed_token` functionality. |
||
316 | * |
||
317 | * @covers Automattic\Jetpack\Connection\Manager::get_signed_token |
||
318 | */ |
||
319 | public function test_get_signed_token() { |
||
343 | |||
344 | /** |
||
345 | * Filter to set the default constant values. |
||
346 | * |
||
347 | * @param string $value Existing value (empty and ignored). |
||
348 | * @param string $name Constant name. |
||
349 | * |
||
350 | * @see Utils::DEFAULT_JETPACK__API_BASE |
||
351 | * @see Utils::DEFAULT_JETPACK__API_VERSION |
||
352 | * |
||
353 | * @return string |
||
354 | */ |
||
355 | public function filter_api_constant( $value, $name ) { |
||
358 | |||
359 | } |
||
360 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.