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 |
||
| 19 | class Test_Terms_Of_Service extends TestCase { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Test setup. |
||
| 23 | */ |
||
| 24 | public function setUp() { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Test teardown. |
||
| 33 | */ |
||
| 34 | public function tearDown() { |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Tests the agree function. |
||
| 40 | * |
||
| 41 | * @covers Automattic\Jetpack\Terms_Of_Service->agree |
||
| 42 | */ |
||
| 43 | public function test_agree() { |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Tests the revoke function. |
||
| 52 | * |
||
| 53 | * @covers Automattic\Jetpack\Terms_Of_Service->revoke |
||
| 54 | */ |
||
| 55 | public function test_revoke() { |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Tests if has_agreed returns correctly if TOS not agreed to. |
||
| 64 | * |
||
| 65 | * @covers Automattic\Jetpack\Terms_Of_Service->has_agreed |
||
| 66 | */ |
||
| 67 | public function test_returns_false_if_not_agreed() { |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Tests if has_agreed returns corrected if agreed but in dev mode. |
||
| 74 | * |
||
| 75 | * @covers Automattic\Jetpack\Terms_Of_Service->has_agreed |
||
| 76 | */ |
||
| 77 | public function test_returns_false_if_has_agreed_but_is_development_mode() { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Tests has_agreed if active but not agreed. |
||
| 86 | * |
||
| 87 | * @covers Automattic\Jetpack\Terms_Of_Service->has_agreed |
||
| 88 | */ |
||
| 89 | public function test_returns_true_if_active_even_if_not_agreed() { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Mock a global function and make it return a certain value. |
||
| 101 | * |
||
| 102 | * @param string $function_name Name of the function. |
||
| 103 | * @param mixed $return_value Return value of the function. |
||
| 104 | * @param string $called_with Value called with. |
||
|
|
|||
| 105 | * |
||
| 106 | * @return phpmock\Mock The mock object. |
||
| 107 | */ |
||
| 108 | View Code Duplication | protected function mock_function( $function_name, $return_value = null, $called_with = null ) { |
|
| 122 | } |
||
| 123 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.