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 |
||
| 16 | class SignatureTest extends TestCase { |
||
| 17 | /** |
||
| 18 | * Tests the Jetpack_Signature->join_with_equal_sign() method. |
||
| 19 | * |
||
| 20 | * @covers Automattic\Jetpack\Connection\Jetpack_Signature->join_with_equal_sign |
||
| 21 | * @dataProvider join_with_equal_sign_data_provider |
||
| 22 | * |
||
| 23 | * @param string $name Query string key value. |
||
| 24 | * @param string|array $value Associated value for query string key. |
||
| 25 | * @param string|array $expected_output The expected output of $signature->join_with_equal_sign. |
||
| 26 | */ |
||
| 27 | public function test_join_with_equal_sign( $name, $value, $expected_output ) { |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Data provider for test_join_with_equal_sign. |
||
| 34 | * |
||
| 35 | * The test data arrays have the format: |
||
| 36 | * 'name' => The value that the constant will be set to. Null if the constant will not be set. |
||
| 37 | * 'value' => The name of the constant. |
||
| 38 | * 'expected_output' => The expected output of $signature->join_with_equal_sign. |
||
| 39 | */ |
||
| 40 | public function join_with_equal_sign_data_provider() { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Tests the Jetpack_Signature->normalized_query_parameters() method. |
||
| 106 | * |
||
| 107 | * @covers Automattic\Jetpack\Connection\Jetpack_Signature->normalized_query_parameters |
||
| 108 | * @dataProvider normalized_query_parameters_data_provider |
||
| 109 | * |
||
| 110 | * @param string $query_string Query string key value. |
||
| 111 | * @param string|array $expected_output The expected output of $signature->normalized_query_parameters. |
||
| 112 | */ |
||
| 113 | public function test_normalized_query_parameters( $query_string, $expected_output ) { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Data provider for test_join_with_equal_sign. |
||
| 120 | * |
||
| 121 | * The test data arrays have the format: |
||
| 122 | * 'name' => The value that the constant will be set to. Null if the constant will not be set. |
||
| 123 | * 'value' => The name of the constant. |
||
| 124 | * 'expected_output' => The expected output of $signature->normalized_query_parameters(). |
||
| 125 | */ |
||
| 126 | public function normalized_query_parameters_data_provider() { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Test sanitize_host_post method |
||
| 162 | * |
||
| 163 | * @group leo |
||
| 164 | * @dataProvider sanitize_host_post_data_provider |
||
| 165 | * |
||
| 166 | * @param mixed $input the input to the method. |
||
| 167 | * @param string $expected the expected output. |
||
| 168 | * @return void |
||
| 169 | */ |
||
| 170 | public function test_sanitize_host_post( $input, $expected ) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Data provider for test_sanitize_host_post |
||
| 177 | * |
||
| 178 | * @return array |
||
| 179 | */ |
||
| 180 | public function sanitize_host_post_data_provider() { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Tests the get_current_request_port method. |
||
| 219 | * |
||
| 220 | * Also used by @see self::test_request_port_constants |
||
| 221 | * |
||
| 222 | * @param mixed $http_x_forwarded_port value of $_SERVER[ 'HTTP_X_FORWARDED_PORT' ]. |
||
| 223 | * @param mixed $server_port value of $_SERVER[ 'SERVER_PORT' ]. Null will unset the value. |
||
| 224 | * @param string $expeceted The expected output. Null will unset the value. |
||
| 225 | * @param boolean $ssl Whether to consider current request using SSL or not. |
||
| 226 | * |
||
| 227 | * @dataProvider get_request_port_data_provider |
||
| 228 | */ |
||
| 229 | public function test_get_request_port( $http_x_forwarded_port, $server_port, $expeceted, $ssl = false ) { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Data provider for test_get_request_port |
||
| 258 | * |
||
| 259 | * @return array |
||
| 260 | */ |
||
| 261 | public function get_request_port_data_provider() { |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Runs isolated tests to check the behavior of constants |
||
| 387 | * |
||
| 388 | * Uses @see self::test_get_request_port |
||
| 389 | * |
||
| 390 | * @runInSeparateProcess |
||
| 391 | * @preserveGlobalState disabled |
||
| 392 | */ |
||
| 393 | View Code Duplication | public function test_request_port_constants() { |
|
| 408 | |||
| 409 | /** |
||
| 410 | * Runs isolated tests to check the behavior of constants when declared as strings |
||
| 411 | * |
||
| 412 | * Uses @see self::test_get_request_port |
||
| 413 | * |
||
| 414 | * @runInSeparateProcess |
||
| 415 | * @preserveGlobalState disabled |
||
| 416 | */ |
||
| 417 | View Code Duplication | public function test_request_port_constants_as_strings() { |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Runs isolated tests to check the behavior of constants with invalid values |
||
| 435 | * |
||
| 436 | * Uses @see self::test_get_request_port |
||
| 437 | * |
||
| 438 | * @runInSeparateProcess |
||
| 439 | * @preserveGlobalState disabled |
||
| 440 | */ |
||
| 441 | public function test_request_port_constants_invalid_valies() { |
||
| 450 | |||
| 451 | } |
||
| 452 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: