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 |
||
| 13 | class Test_Status extends TestCase { |
||
| 14 | /** |
||
| 15 | * Default site URL. |
||
| 16 | * |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | private $site_url = 'https://yourjetpack.blog'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Setup before running any of the tests. |
||
| 23 | */ |
||
| 24 | public static function setUpBeforeClass() { |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Test setup. |
||
| 32 | */ |
||
| 33 | public function setUp() { |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Test teardown. |
||
| 40 | */ |
||
| 41 | public function tearDown() { |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @covers Automattic\Jetpack\Status::is_development_mode |
||
| 48 | */ |
||
| 49 | public function test_is_development_mode_default() { |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @covers Automattic\Jetpack\Status::is_development_mode |
||
| 58 | */ |
||
| 59 | public function test_is_development_mode_filter_true() { |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @covers Automattic\Jetpack\Status::is_development_mode |
||
| 68 | */ |
||
| 69 | public function test_is_development_mode_filter_bool() { |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @covers Automattic\Jetpack\Status::is_development_mode |
||
| 78 | */ |
||
| 79 | public function test_is_development_mode_localhost() { |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @covers Automattic\Jetpack\Status::is_development_mode |
||
| 89 | * |
||
| 90 | * @runInSeparateProcess |
||
| 91 | */ |
||
| 92 | public function test_is_development_mode_constant() { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @covers Automattic\Jetpack\Status::is_multi_network |
||
| 109 | */ |
||
| 110 | public function test_is_multi_network_not_multisite() { |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @covers Automattic\Jetpack\Status::is_multi_network |
||
| 118 | */ |
||
| 119 | public function test_is_multi_network_when_single_network() { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @covers Automattic\Jetpack\Status::is_multi_network |
||
| 130 | */ |
||
| 131 | View Code Duplication | public function test_is_multi_network_when_multiple_networks() { |
|
| 139 | |||
| 140 | /** |
||
| 141 | * @covers Automattic\Jetpack\Status::is_single_user_site |
||
| 142 | */ |
||
| 143 | public function test_is_single_user_site_with_transient() { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @covers Automattic\Jetpack\Status::is_single_user_site |
||
| 154 | */ |
||
| 155 | View Code Duplication | public function test_is_single_user_site_with_one_user() { |
|
| 164 | |||
| 165 | /** |
||
| 166 | * @covers Automattic\Jetpack\Status::is_single_user_site |
||
| 167 | */ |
||
| 168 | View Code Duplication | public function test_is_single_user_site_with_multiple_users() { |
|
| 177 | |||
| 178 | |||
| 179 | /** |
||
| 180 | * Mock a global function with particular arguments and make it return a certain value. |
||
| 181 | * |
||
| 182 | * @param string $function_name Name of the function. |
||
| 183 | * @param array $args Array of argument sets, last value of each set is used as a return value. |
||
| 184 | * @return phpmock\Mock The mock object. |
||
| 185 | */ |
||
| 186 | protected function mock_function_with_args( $function_name, $args = array() ) { |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Mock a set of constants. |
||
| 208 | * |
||
| 209 | * @param array $args Array of sets with constants and their respective values. |
||
|
|
|||
| 210 | * @return phpmock\Mock The mock object. |
||
| 211 | */ |
||
| 212 | protected function mock_constants( $constants = array() ) { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Mock $wpdb->get_var() and make it return a certain value. |
||
| 225 | * |
||
| 226 | * @param mixed $return_value Return value of the function. |
||
| 227 | * @return PHPUnit\Framework\MockObject\MockObject The mock object. |
||
| 228 | */ |
||
| 229 | protected function mock_wpdb_get_var( $return_value = null ) { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Clean up the existing $wpdb->get_var() mock. |
||
| 246 | */ |
||
| 247 | protected function clean_mock_wpdb_get_var() { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Tests a WP Engine staging site URL. |
||
| 254 | * |
||
| 255 | * @author kraftbj |
||
| 256 | * @covers is_staging_site |
||
| 257 | * @since 3.9.0 |
||
| 258 | */ |
||
| 259 | public function test_is_staging_site_will_report_staging_for_wpengine_sites_by_url() { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Tests known staging sites. |
||
| 266 | * |
||
| 267 | * @dataProvider get_is_staging_site_known_hosting_providers_data |
||
| 268 | * |
||
| 269 | * @param string $site_url Site URL. |
||
| 270 | */ |
||
| 271 | public function test_is_staging_site_for_known_hosting_providers( $site_url ) { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Known hosting providers. |
||
| 282 | * |
||
| 283 | * @return array |
||
| 284 | */ |
||
| 285 | public function get_is_staging_site_known_hosting_providers_data() { |
||
| 298 | } |
||
| 299 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.