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 |
||
| 10 | class Test_Status extends TestCase { |
||
| 11 | /** |
||
| 12 | * Default site URL. |
||
| 13 | * |
||
| 14 | * @var string |
||
| 15 | */ |
||
| 16 | private $site_url = 'https://yourjetpack.blog'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Setup before running any of the tests. |
||
| 20 | */ |
||
| 21 | public static function setUpBeforeClass() { |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Test setup. |
||
| 29 | */ |
||
| 30 | public function setUp() { |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Test teardown. |
||
| 36 | */ |
||
| 37 | public function tearDown() { |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @covers Automattic\Jetpack\Status::is_development_mode |
||
| 43 | */ |
||
| 44 | View Code Duplication | public function test_is_development_mode_default() { |
|
| 55 | |||
| 56 | /** |
||
| 57 | * @covers Automattic\Jetpack\Status::is_development_mode |
||
| 58 | */ |
||
| 59 | View Code Duplication | public function test_is_development_mode_filter_true() { |
|
| 69 | |||
| 70 | /** |
||
| 71 | * @covers Automattic\Jetpack\Status::is_development_mode |
||
| 72 | */ |
||
| 73 | View Code Duplication | public function test_is_development_mode_filter_bool() { |
|
| 83 | |||
| 84 | /** |
||
| 85 | * @covers Automattic\Jetpack\Status::is_development_mode |
||
| 86 | */ |
||
| 87 | View Code Duplication | public function test_is_development_mode_localhost() { |
|
| 99 | |||
| 100 | /** |
||
| 101 | * @covers Automattic\Jetpack\Status::is_development_mode |
||
| 102 | * |
||
| 103 | * @runInSeparateProcess |
||
| 104 | */ |
||
| 105 | public function test_is_development_mode_constant() { |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @covers Automattic\Jetpack\Status::is_multi_network |
||
| 125 | */ |
||
| 126 | public function test_is_multi_network_not_multisite() { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @covers Automattic\Jetpack\Status::is_multi_network |
||
| 134 | */ |
||
| 135 | public function test_is_multi_network_when_single_network() { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @covers Automattic\Jetpack\Status::is_multi_network |
||
| 146 | */ |
||
| 147 | public function test_is_multi_network_when_multiple_networks() { |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @covers Automattic\Jetpack\Status::is_single_user_site |
||
| 158 | */ |
||
| 159 | public function test_is_single_user_site_with_transient() { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @covers Automattic\Jetpack\Status::is_single_user_site |
||
| 170 | */ |
||
| 171 | View Code Duplication | public function test_is_single_user_site_with_one_user() { |
|
| 180 | |||
| 181 | /** |
||
| 182 | * @covers Automattic\Jetpack\Status::is_single_user_site |
||
| 183 | */ |
||
| 184 | View Code Duplication | public function test_is_single_user_site_with_multiple_users() { |
|
| 193 | |||
| 194 | |||
| 195 | /** |
||
| 196 | * Mock a global function with particular arguments and make it return a certain value. |
||
| 197 | * |
||
| 198 | * @param string $function_name Name of the function. |
||
| 199 | * @param array $args Array of argument sets, last value of each set is used as a return value. |
||
| 200 | * @return phpmock\Mock The mock object. |
||
| 201 | */ |
||
| 202 | protected function mock_function_with_args( $function_name, $args = array() ) { |
||
| 203 | $builder = new MockBuilder(); |
||
| 204 | $builder->setNamespace( __NAMESPACE__ ) |
||
| 205 | ->setName( $function_name ) |
||
| 206 | ->setFunction( |
||
| 207 | function() use ( &$args ) { |
||
| 208 | $current_args = func_get_args(); |
||
| 209 | |||
| 210 | foreach ( $args as $arg ) { |
||
| 211 | if ( array_slice( $arg, 0, -1 ) === $current_args ) { |
||
| 212 | return array_pop( $arg ); |
||
| 213 | } |
||
| 214 | } |
||
| 215 | } |
||
| 216 | ); |
||
| 217 | |||
| 218 | $mock = $builder->build(); |
||
| 219 | $mock->enable(); |
||
| 220 | |||
| 221 | return $mock; |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Mock a set of filters. |
||
| 226 | * |
||
| 227 | * @param array $args Array of filters with their arguments. |
||
|
|
|||
| 228 | * @return phpmock\Mock The mock object. |
||
| 229 | */ |
||
| 230 | protected function mock_filters( $filters = array() ) { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Mock a set of constants. |
||
| 236 | * |
||
| 237 | * @param array $args Array of sets with constants and their respective values. |
||
| 238 | * @return phpmock\Mock The mock object. |
||
| 239 | */ |
||
| 240 | protected function mock_constants( $constants = array() ) { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Mock a global function and make it return a certain value. |
||
| 253 | * |
||
| 254 | * @param string $function_name Name of the function. |
||
| 255 | * @param mixed $return_value Return value of the function. |
||
| 256 | * @return phpmock\Mock The mock object. |
||
| 257 | */ |
||
| 258 | View Code Duplication | protected function mock_function( $function_name, $return_value = null ) { |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Mock $wpdb->get_var() and make it return a certain value. |
||
| 270 | * |
||
| 271 | * @param mixed $return_value Return value of the function. |
||
| 272 | * @return PHPUnit\Framework\MockObject\MockObject The mock object. |
||
| 273 | */ |
||
| 274 | protected function mock_wpdb_get_var( $return_value = null ) { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Clean up the existing $wpdb->get_var() mock. |
||
| 291 | */ |
||
| 292 | protected function clean_mock_wpdb_get_var() { |
||
| 296 | } |
||
| 297 |
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.