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 | View Code Duplication | class ContainerTest extends \PHPUnit_Framework_TestCase |
|
|
|
|||
| 20 | { |
||
| 21 | use ContainerProvider; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * 正常系 |
||
| 25 | * 値を格納して取り出せること |
||
| 26 | * @test |
||
| 27 | * @dataProvider valueLazyProvider |
||
| 28 | */ |
||
| 29 | public function okContainer($value, $result) |
||
| 35 | |||
| 36 | /** |
||
| 37 | * 正常系 |
||
| 38 | * 設定した値のカウントが取得できること |
||
| 39 | * @test |
||
| 40 | */ |
||
| 41 | public function okLength() |
||
| 48 | |||
| 49 | /** |
||
| 50 | * 正常系 |
||
| 51 | * 値を削除できること |
||
| 52 | * 削除後の要素にアクセスすると例外が発生すること |
||
| 53 | * @test |
||
| 54 | * @expectedException WebStream\Exception\Extend\InvalidArgumentException |
||
| 55 | */ |
||
| 56 | public function okRemove() |
||
| 63 | |||
| 64 | /** |
||
| 65 | * 正常系 |
||
| 66 | * プリミティブ値を登録できること |
||
| 67 | * 値はそのまま保存される |
||
| 68 | * @test |
||
| 69 | * @dataProvider valueDynamicProvider |
||
| 70 | */ |
||
| 71 | public function okRegisterPrimitive($value, $result) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * 正常系 |
||
| 80 | * クロージャを登録できること |
||
| 81 | * 値はそのまま保存される |
||
| 82 | * @test |
||
| 83 | */ |
||
| 84 | public function okRegisterClosure() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * 正常系 |
||
| 98 | * 即時実行状態で登録されること |
||
| 99 | * @test |
||
| 100 | */ |
||
| 101 | public function okRegisterAsDynamic() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * 正常系 |
||
| 115 | * 遅延実行状態で登録されること |
||
| 116 | * @test |
||
| 117 | */ |
||
| 118 | public function okRegisterAsLazy() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * 正常系 |
||
| 133 | * コンテナから取得するとき、値はキャッシュされていること |
||
| 134 | * @test |
||
| 135 | */ |
||
| 136 | public function okRegisterCached() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * 正常系 |
||
| 161 | * コンテナから取得するとき、値はキャッシュされていないこと |
||
| 162 | * @test |
||
| 163 | */ |
||
| 164 | public function okRegisterUnCached() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * 正常系 |
||
| 187 | * 未定義の値にアクセスしても例外が発生しないこと |
||
| 188 | * @test |
||
| 189 | */ |
||
| 190 | public function okUnStrictContainer() |
||
| 195 | } |
||
| 196 |
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.