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_Status extends TestCase { |
||
| 21 | /** |
||
| 22 | * Default site URL. |
||
| 23 | * |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | private $site_url = 'https://yourjetpack.blog'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Status instance. |
||
| 30 | * |
||
| 31 | * @var Automattic\Jetpack\Status |
||
| 32 | */ |
||
| 33 | private $status_obj; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Setup before running any of the tests. |
||
| 37 | */ |
||
| 38 | public static function setUpBeforeClass() { |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Test setup. |
||
| 46 | */ |
||
| 47 | public function setUp() { |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Test teardown. |
||
| 61 | */ |
||
| 62 | public function tearDown() { |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Test is_offline_mode when not using any filter |
||
| 70 | * |
||
| 71 | * @covers Automattic\Jetpack\Status::is_offline_mode |
||
| 72 | */ |
||
| 73 | public function test_is_offline_mode_default() { |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Test is_offline_mode when using the jetpack_offline_mode filter |
||
| 81 | * |
||
| 82 | * @covers Automattic\Jetpack\Status::is_offline_mode |
||
| 83 | */ |
||
| 84 | public function test_is_offline_mode_filter_true() { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Test when using a bool value for the jetpack_offline_mode filter. |
||
| 92 | * |
||
| 93 | * @covers Automattic\Jetpack\Status::is_offline_mode |
||
| 94 | */ |
||
| 95 | public function test_is_offline_mode_filter_bool() { |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Test when site url is localhost (dev mode on) |
||
| 103 | * |
||
| 104 | * @covers Automattic\Jetpack\Status::is_offline_mode |
||
| 105 | */ |
||
| 106 | public function test_is_offline_mode_localhost() { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Test when wp_get_environment_type is local. |
||
| 116 | * |
||
| 117 | * @covers Automattic\Jetpack\Status::is_local_site |
||
| 118 | */ |
||
| 119 | public function test_is_local_wp_get_environment_type_local() { |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Test when wp_get_environment_type is local. |
||
| 129 | * |
||
| 130 | * @covers Automattic\Jetpack\Status::is_staging_site |
||
| 131 | */ |
||
| 132 | View Code Duplication | public function test_is_staging_wp_get_environment_type_local() { |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Test when wp_get_environment_type is staging. |
||
| 142 | * |
||
| 143 | * @covers Automattic\Jetpack\Status::is_staging_site |
||
| 144 | */ |
||
| 145 | View Code Duplication | public function test_is_staging_wp_get_environment_type_staging() { |
|
| 152 | |||
| 153 | /** |
||
| 154 | * Test when wp_get_environment_type is production. |
||
| 155 | * |
||
| 156 | * @covers Automattic\Jetpack\Status::is_staging_site |
||
| 157 | */ |
||
| 158 | View Code Duplication | public function test_is_staging_wp_get_environment_type_production() { |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Test when wp_get_environment_type is a random value. |
||
| 168 | * |
||
| 169 | * @covers Automattic\Jetpack\Status::is_staging_site |
||
| 170 | */ |
||
| 171 | View Code Duplication | public function test_is_staging_wp_get_environment_type_random() { |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Test when using the constant to set dev mode |
||
| 181 | * |
||
| 182 | * @covers Automattic\Jetpack\Status::is_offline_mode |
||
| 183 | * |
||
| 184 | * @runInSeparateProcess |
||
| 185 | */ |
||
| 186 | public function test_is_offline_mode_constant() { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Test for is_multi_network with a single site |
||
| 207 | * |
||
| 208 | * @covers Automattic\Jetpack\Status::is_multi_network |
||
| 209 | */ |
||
| 210 | public function test_is_multi_network_not_multisite() { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Test is_multi_network with a multisite install |
||
| 218 | * |
||
| 219 | * @covers Automattic\Jetpack\Status::is_multi_network |
||
| 220 | */ |
||
| 221 | public function test_is_multi_network_when_single_network() { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Test is_multi_network when multiple networks |
||
| 232 | * |
||
| 233 | * @covers Automattic\Jetpack\Status::is_multi_network |
||
| 234 | */ |
||
| 235 | View Code Duplication | public function test_is_multi_network_when_multiple_networks() { |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Test cached is_single_user_site |
||
| 246 | * |
||
| 247 | * @covers Automattic\Jetpack\Status::is_single_user_site |
||
| 248 | */ |
||
| 249 | public function test_is_single_user_site_with_transient() { |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Test is_single_user_site |
||
| 260 | * |
||
| 261 | * @covers Automattic\Jetpack\Status::is_single_user_site |
||
| 262 | */ |
||
| 263 | View Code Duplication | public function test_is_single_user_site_with_one_user() { |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Test is_single_user_site with multiple users |
||
| 275 | * |
||
| 276 | * @covers Automattic\Jetpack\Status::is_single_user_site |
||
| 277 | */ |
||
| 278 | View Code Duplication | public function test_is_single_user_site_with_multiple_users() { |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Mock a global function with particular arguments and make it return a certain value. |
||
| 290 | * |
||
| 291 | * @param string $function_name Name of the function. |
||
| 292 | * @param array $args Array of argument sets, last value of each set is used as a return value. |
||
| 293 | * @return phpmock\Mock The mock object. |
||
| 294 | */ |
||
| 295 | protected function mock_function_with_args( $function_name, $args = array() ) { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Mock a set of constants. |
||
| 317 | * |
||
| 318 | * @param array $constants Array of sets with constants and their respective values. |
||
| 319 | * @return phpmock\Mock The mock object. |
||
| 320 | */ |
||
| 321 | protected function mock_constants( $constants = array() ) { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Mock $wpdb->get_var() and make it return a certain value. |
||
| 334 | * |
||
| 335 | * @param mixed $return_value Return value of the function. |
||
| 336 | * |
||
| 337 | * PHPUnit\Framework\MockObject\MockObject The mock object. |
||
| 338 | */ |
||
| 339 | protected function mock_wpdb_get_var( $return_value = null ) { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Clean up the existing $wpdb->get_var() mock. |
||
| 356 | */ |
||
| 357 | protected function clean_mock_wpdb_get_var() { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Tests known staging sites. |
||
| 364 | * |
||
| 365 | * @dataProvider get_is_staging_site_known_hosting_providers_data |
||
| 366 | * @covers Automattic\Jetpack\Status::is_staging_site |
||
| 367 | * |
||
| 368 | * @param string $site_url Site URL. |
||
| 369 | * @param bool $expected Expected return. |
||
| 370 | */ |
||
| 371 | public function test_is_staging_site_for_known_hosting_providers( $site_url, $expected ) { |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Known hosting providers. |
||
| 387 | * |
||
| 388 | * Including a couple of general RegEx checks (subdir, ending slash). |
||
| 389 | * |
||
| 390 | * @return array |
||
| 391 | */ |
||
| 392 | public function get_is_staging_site_known_hosting_providers_data() { |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Tests known local development sites. |
||
| 427 | * |
||
| 428 | * @dataProvider get_is_local_site_known_tld |
||
| 429 | * |
||
| 430 | * @param string $site_url Site URL. |
||
| 431 | * @param bool $expected_response Expected response. |
||
| 432 | */ |
||
| 433 | public function test_is_local_site_for_known_tld( $site_url, $expected_response ) { |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Known hosting providers. |
||
| 449 | * |
||
| 450 | * @return array |
||
| 451 | */ |
||
| 452 | public function get_is_local_site_known_tld() { |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Tests for site_suffix(). |
||
| 483 | * |
||
| 484 | * @covers Automattic\Jetpack\Status::get_site_suffix |
||
| 485 | * @dataProvider get_site_suffix_examples |
||
| 486 | * |
||
| 487 | * @param string $site Given site URL. |
||
| 488 | * @param string $expected Site suffix. |
||
| 489 | */ |
||
| 490 | public function test_jetpack_get_site_suffix( $site, $expected ) { |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Examples of sites passed to get_site_suffix |
||
| 499 | * |
||
| 500 | * @covers Automattic\Jetpack\Status::get_site_suffix |
||
| 501 | */ |
||
| 502 | public function get_site_suffix_examples() { |
||
| 534 | } |
||
| 535 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..