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 |
||
| 9 | class CommentAPITest extends TestCase |
||
|
|
|||
| 10 | { |
||
| 11 | use DatabaseMigrations; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Create fake user. |
||
| 15 | * |
||
| 16 | * @return mixed |
||
| 17 | */ |
||
| 18 | public function createUser() |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @param User $user |
||
| 28 | * |
||
| 29 | * @return mixed |
||
| 30 | */ |
||
| 31 | private function createUserApiKey(User $user) |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Create fake video. |
||
| 39 | * |
||
| 40 | * @return \App\Video |
||
| 41 | */ |
||
| 42 | View Code Duplication | private function createFakeVideo($user) |
|
| 53 | |||
| 54 | private function createFakeComment($user_id, $video_id) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Test comments in database are listed by API. |
||
| 69 | * |
||
| 70 | * @return void |
||
| 71 | */ |
||
| 72 | public function testCommentsInDatabaseAreListedByAPI() |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Test store comments and see in DB. |
||
| 90 | * |
||
| 91 | * @return void |
||
| 92 | */ |
||
| 93 | View Code Duplication | public function testCanBePostCommentAndSeeInDB() |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Test videos can be update and see changes on database. |
||
| 110 | * |
||
| 111 | * @return void |
||
| 112 | */ |
||
| 113 | public function testCommentsCanBeUpdatedAndSeeChangesInDatabase() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Test comments can be deleted and not see on database. |
||
| 134 | * |
||
| 135 | * @return void |
||
| 136 | */ |
||
| 137 | public function testCommentsCanBeDeletedAndNotSeenOnDatabase() |
||
| 157 | } |
||
| 158 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.