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 |
||
| 18 | class Test_Status extends TestCase { |
||
| 19 | /** |
||
| 20 | * Default site URL. |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | private $site_url = 'https://yourjetpack.blog'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Status instance. |
||
| 28 | * |
||
| 29 | * @var Automattic\Jetpack\Status |
||
| 30 | */ |
||
| 31 | private $status_obj; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Mocked constants. |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | private $mocked_constants = array(); |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Setup before running any of the tests. |
||
| 42 | * |
||
| 43 | * @beforeClass |
||
| 44 | */ |
||
| 45 | public static function set_up_before_class() { |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Test setup. |
||
| 53 | * |
||
| 54 | * @before |
||
| 55 | */ |
||
| 56 | public function set_up() { |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Test teardown. |
||
| 79 | * |
||
| 80 | * @after |
||
| 81 | */ |
||
| 82 | public function tear_down() { |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Test is_offline_mode when not using any filter |
||
| 88 | * |
||
| 89 | * @covers Automattic\Jetpack\Status::is_offline_mode |
||
| 90 | */ |
||
| 91 | public function test_is_offline_mode_default() { |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Test is_offline_mode when using the jetpack_offline_mode filter |
||
| 99 | * |
||
| 100 | * @covers Automattic\Jetpack\Status::is_offline_mode |
||
| 101 | */ |
||
| 102 | public function test_is_offline_mode_filter_true() { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Test when using a bool value for the jetpack_offline_mode filter. |
||
| 110 | * |
||
| 111 | * @covers Automattic\Jetpack\Status::is_offline_mode |
||
| 112 | */ |
||
| 113 | public function test_is_offline_mode_filter_bool() { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Test when site url is localhost (dev mode on) |
||
| 121 | * |
||
| 122 | * @covers Automattic\Jetpack\Status::is_offline_mode |
||
| 123 | */ |
||
| 124 | public function test_is_offline_mode_localhost() { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Test when wp_get_environment_type is local. |
||
| 134 | * |
||
| 135 | * @covers Automattic\Jetpack\Status::is_local_site |
||
| 136 | */ |
||
| 137 | public function test_is_local_wp_get_environment_type_local() { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Test when wp_get_environment_type is local. |
||
| 147 | * |
||
| 148 | * @covers Automattic\Jetpack\Status::is_staging_site |
||
| 149 | */ |
||
| 150 | View Code Duplication | public function test_is_staging_wp_get_environment_type_local() { |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Test when wp_get_environment_type is staging. |
||
| 160 | * |
||
| 161 | * @covers Automattic\Jetpack\Status::is_staging_site |
||
| 162 | */ |
||
| 163 | View Code Duplication | public function test_is_staging_wp_get_environment_type_staging() { |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Test when wp_get_environment_type is production. |
||
| 173 | * |
||
| 174 | * @covers Automattic\Jetpack\Status::is_staging_site |
||
| 175 | */ |
||
| 176 | View Code Duplication | public function test_is_staging_wp_get_environment_type_production() { |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Test when wp_get_environment_type is a random value. |
||
| 186 | * |
||
| 187 | * @covers Automattic\Jetpack\Status::is_staging_site |
||
| 188 | */ |
||
| 189 | View Code Duplication | public function test_is_staging_wp_get_environment_type_random() { |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Test when using the constant to set dev mode |
||
| 199 | * |
||
| 200 | * @covers Automattic\Jetpack\Status::is_offline_mode |
||
| 201 | * |
||
| 202 | * @runInSeparateProcess |
||
| 203 | */ |
||
| 204 | public function test_is_offline_mode_constant() { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Test for is_multi_network with a single site |
||
| 213 | * |
||
| 214 | * @covers Automattic\Jetpack\Status::is_multi_network |
||
| 215 | */ |
||
| 216 | public function test_is_multi_network_not_multisite() { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Test is_multi_network with a multisite install |
||
| 224 | * |
||
| 225 | * @covers Automattic\Jetpack\Status::is_multi_network |
||
| 226 | */ |
||
| 227 | public function test_is_multi_network_when_single_network() { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Test is_multi_network when multiple networks |
||
| 238 | * |
||
| 239 | * @covers Automattic\Jetpack\Status::is_multi_network |
||
| 240 | */ |
||
| 241 | View Code Duplication | public function test_is_multi_network_when_multiple_networks() { |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Test cached is_single_user_site |
||
| 252 | * |
||
| 253 | * @covers Automattic\Jetpack\Status::is_single_user_site |
||
| 254 | */ |
||
| 255 | public function test_is_single_user_site_with_transient() { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Test is_single_user_site |
||
| 266 | * |
||
| 267 | * @covers Automattic\Jetpack\Status::is_single_user_site |
||
| 268 | */ |
||
| 269 | View Code Duplication | public function test_is_single_user_site_with_one_user() { |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Test is_single_user_site with multiple users |
||
| 281 | * |
||
| 282 | * @covers Automattic\Jetpack\Status::is_single_user_site |
||
| 283 | */ |
||
| 284 | View Code Duplication | public function test_is_single_user_site_with_multiple_users() { |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Mock $wpdb->get_var() and make it return a certain value. |
||
| 296 | * |
||
| 297 | * @param mixed $return_value Return value of the function. |
||
| 298 | * |
||
| 299 | * PHPUnit\Framework\MockObject\MockObject The mock object. |
||
| 300 | */ |
||
| 301 | protected function mock_wpdb_get_var( $return_value = null ) { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Clean up the existing $wpdb->get_var() mock. |
||
| 318 | */ |
||
| 319 | protected function clean_mock_wpdb_get_var() { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Tests known staging sites. |
||
| 326 | * |
||
| 327 | * @dataProvider get_is_staging_site_known_hosting_providers_data |
||
| 328 | * @covers Automattic\Jetpack\Status::is_staging_site |
||
| 329 | * |
||
| 330 | * @param string $site_url Site URL. |
||
| 331 | * @param bool $expected Expected return. |
||
| 332 | */ |
||
| 333 | public function test_is_staging_site_for_known_hosting_providers( $site_url, $expected ) { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Known hosting providers. |
||
| 349 | * |
||
| 350 | * Including a couple of general RegEx checks (subdir, ending slash). |
||
| 351 | * |
||
| 352 | * @return array |
||
| 353 | */ |
||
| 354 | public function get_is_staging_site_known_hosting_providers_data() { |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Tests known local development sites. |
||
| 389 | * |
||
| 390 | * @dataProvider get_is_local_site_known_tld |
||
| 391 | * |
||
| 392 | * @param string $site_url Site URL. |
||
| 393 | * @param bool $expected_response Expected response. |
||
| 394 | */ |
||
| 395 | public function test_is_local_site_for_known_tld( $site_url, $expected_response ) { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Known hosting providers. |
||
| 411 | * |
||
| 412 | * @return array |
||
| 413 | */ |
||
| 414 | public function get_is_local_site_known_tld() { |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Tests for site_suffix(). |
||
| 445 | * |
||
| 446 | * @covers Automattic\Jetpack\Status::get_site_suffix |
||
| 447 | * @dataProvider get_site_suffix_examples |
||
| 448 | * |
||
| 449 | * @param string $site Given site URL. |
||
| 450 | * @param string $expected Site suffix. |
||
| 451 | */ |
||
| 452 | public function test_jetpack_get_site_suffix( $site, $expected ) { |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Examples of sites passed to get_site_suffix |
||
| 461 | * |
||
| 462 | * @covers Automattic\Jetpack\Status::get_site_suffix |
||
| 463 | */ |
||
| 464 | public function get_site_suffix_examples() { |
||
| 496 | } |
||
| 497 |
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..