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 |
||
7 | class UserAPITest extends TestCase |
||
|
|||
8 | { |
||
9 | use DatabaseMigrations; |
||
10 | |||
11 | /** |
||
12 | * Create fake user. |
||
13 | * |
||
14 | * @return mixed |
||
15 | */ |
||
16 | public function createUser() |
||
23 | |||
24 | /** |
||
25 | * @param User $user |
||
26 | * |
||
27 | * @return mixed |
||
28 | */ |
||
29 | private function createUserApiKey(User $user) |
||
34 | |||
35 | /** |
||
36 | * Test user in database are listed by API. |
||
37 | * |
||
38 | * @return void |
||
39 | */ |
||
40 | public function testUserInDatabaseAreListedByAPI() |
||
52 | |||
53 | /** |
||
54 | * Test user is an api then returns JSON. |
||
55 | * |
||
56 | * @return void |
||
57 | */ |
||
58 | public function testCreateUserAndShowUseJson() |
||
63 | |||
64 | /** |
||
65 | * Test upate user and see in DB. |
||
66 | * |
||
67 | * @return void |
||
68 | */ |
||
69 | public function testCanBeUpdateAndSeeInDB() |
||
90 | |||
91 | /** |
||
92 | * Test upate user without avatar and see in DB. |
||
93 | * |
||
94 | * @return void |
||
95 | */ |
||
96 | public function testCanBeUpdateWithoutAvatarAndSeeInDB() |
||
108 | |||
109 | /** |
||
110 | * Test delete user and not see in DB. |
||
111 | * |
||
112 | * @return void |
||
113 | */ |
||
114 | View Code Duplication | public function testCanBeDeleteAndNotSeeInDB() |
|
122 | } |
||
123 |
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.