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 |
||
| 25 | class RuleTest extends AbstractTest |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * testGetIntPropertyReturnsValueOfTypeInteger |
||
| 29 | * |
||
| 30 | * @return void |
||
| 31 | */ |
||
| 32 | View Code Duplication | public function testGetIntPropertyReturnsValueOfTypeInteger() |
|
| 39 | |||
| 40 | /** |
||
| 41 | * testGetBooleanPropertyReturnsTrueForStringValue1 |
||
| 42 | * |
||
| 43 | * @return void |
||
| 44 | */ |
||
| 45 | public function testGetBooleanPropertyReturnsTrueForStringValue1() |
||
| 52 | |||
| 53 | /** |
||
| 54 | * testGetBooleanPropertyReturnsTrueForStringValueOn |
||
| 55 | * |
||
| 56 | * @return void |
||
| 57 | */ |
||
| 58 | public function testGetBooleanPropertyReturnsTrueForStringValueOn() |
||
| 65 | |||
| 66 | /** |
||
| 67 | * testGetBooleanPropertyReturnsTrueForStringValueTrue |
||
| 68 | * |
||
| 69 | * @return void |
||
| 70 | */ |
||
| 71 | public function testGetBooleanPropertyReturnsTrueForStringValueTrue() |
||
| 78 | |||
| 79 | /** |
||
| 80 | * testGetBooleanPropertyReturnsTrueForDifferentStringValue |
||
| 81 | * |
||
| 82 | * @return void |
||
| 83 | */ |
||
| 84 | public function testGetBooleanPropertyReturnsTrueForDifferentStringValue() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * testGetIntPropertyThrowsExceptionWhenNoPropertyForNameExists |
||
| 94 | * |
||
| 95 | * @return void |
||
| 96 | * @expectedException \OutOfBoundsException |
||
| 97 | */ |
||
| 98 | public function testGetIntPropertyThrowsExceptionWhenNoPropertyForNameExists() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * testGetBooleanPropertyThrowsExceptionWhenNoPropertyForNameExists |
||
| 106 | * |
||
| 107 | * @return void |
||
| 108 | * @expectedException \OutOfBoundsException |
||
| 109 | */ |
||
| 110 | public function testGetBooleanPropertyThrowsExceptionWhenNoPropertyForNameExists() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * testStringPropertyThrowsExceptionWhenNoPropertyForNameExists |
||
| 118 | * |
||
| 119 | * @return void |
||
| 120 | * @expectedException \OutOfBoundsException |
||
| 121 | */ |
||
| 122 | public function testGetStringPropertyThrowsExceptionWhenNoPropertyForNameExists() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * testGetStringPropertyReturnsStringValue |
||
| 130 | * |
||
| 131 | * @return void |
||
| 132 | */ |
||
| 133 | View Code Duplication | public function testGetStringPropertyReturnsString() |
|
| 140 | } |
||
| 141 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.