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 |
||
14 | class VideoAPITest extends TestCase |
||
|
|||
15 | { |
||
16 | use DatabaseMigrations; |
||
17 | |||
18 | /** |
||
19 | * Create fake user. |
||
20 | * |
||
21 | * @return mixed |
||
22 | */ |
||
23 | public function createUser() |
||
30 | |||
31 | /** |
||
32 | * @param User $user |
||
33 | * |
||
34 | * @return mixed |
||
35 | */ |
||
36 | private function createUserApiKey(User $user) |
||
41 | |||
42 | /** |
||
43 | * Create fake video. |
||
44 | * |
||
45 | * @return \App\Video |
||
46 | */ |
||
47 | View Code Duplication | private function createFakeVideo($user) |
|
58 | |||
59 | /** |
||
60 | * Create fake videos. |
||
61 | * |
||
62 | * @param int $count |
||
63 | * |
||
64 | * @return \App\Video |
||
65 | */ |
||
66 | private function createFakeVideos($count = 10) |
||
73 | |||
74 | /** |
||
75 | * Test video is an api then returns JSON. |
||
76 | * |
||
77 | * @return void |
||
78 | */ |
||
79 | public function testVideoUseJson() |
||
83 | |||
84 | /** |
||
85 | * Test videos in database are listed by API. |
||
86 | * |
||
87 | * @return void |
||
88 | */ |
||
89 | public function testVideosInDatabaseAreListedByAPI() |
||
101 | |||
102 | /** |
||
103 | * Test Video Return 404 on video not exists. |
||
104 | * |
||
105 | * @return void |
||
106 | */ |
||
107 | public function testVideoReturn404OnVideoNotExists() |
||
111 | |||
112 | /** |
||
113 | * Test best videos is an api then returns JSON. |
||
114 | * |
||
115 | * @return void |
||
116 | */ |
||
117 | public function testBestVideosUseJson() |
||
123 | |||
124 | /** |
||
125 | * Test videos user is an api then returns JSON. |
||
126 | * |
||
127 | * @return void |
||
128 | */ |
||
129 | View Code Duplication | public function testVideosUserUseJson() |
|
136 | |||
137 | /** |
||
138 | * Test videos for category is an api then returns JSON. |
||
139 | * |
||
140 | * @return void |
||
141 | */ |
||
142 | View Code Duplication | public function testVideosForCategory() |
|
149 | |||
150 | /** |
||
151 | * Test video in database is shown by API. |
||
152 | * |
||
153 | * @return void |
||
154 | */ |
||
155 | View Code Duplication | public function testVideoInDatabaseAreShownByAPI() |
|
162 | |||
163 | /** |
||
164 | * Test videos Unauthorized posted without apikey. |
||
165 | * |
||
166 | * @return void |
||
167 | */ |
||
168 | public function testVideosUnauthorizedPostedWithoutApiKey() |
||
173 | |||
174 | /** |
||
175 | * Test videos can be posted and saved to database. |
||
176 | * |
||
177 | * @return void |
||
178 | */ |
||
179 | public function testVideosCanBePostedAndSavedIntoDatabase() |
||
197 | |||
198 | /** |
||
199 | * Test videos can be update and see changes on database. |
||
200 | * |
||
201 | * @return void |
||
202 | */ |
||
203 | public function testVideosCanBeUpdatedAndSeeChangesInDatabase() |
||
211 | |||
212 | /** |
||
213 | * Test videos can be deleted and not see on database. |
||
214 | * |
||
215 | * @return void |
||
216 | */ |
||
217 | View Code Duplication | public function testVideosCanBeDeletedAndNotSeenOnDatabase() |
|
225 | |||
226 | /** |
||
227 | * Test videos can be search and see result. |
||
228 | * |
||
229 | * @return void |
||
230 | */ |
||
231 | View Code Duplication | public function testVideosCanBeSearchAndSeenResult() |
|
238 | } |
||
239 |
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.