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 |
||
8 | class UsersController extends BaseApiController |
||
9 | { |
||
10 | /** |
||
11 | * The name of the model that is used by the base api controller |
||
12 | * to preform actions like (add, edit ... etc). |
||
13 | * @var string |
||
14 | */ |
||
15 | protected $model = 'users'; |
||
16 | |||
17 | /** |
||
18 | * List of all route actions that the base api controller |
||
19 | * will skip permissions check for them. |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $skipPermissionCheck = ['account', 'logout']; |
||
23 | |||
24 | /** |
||
25 | * List of all route actions that the base api controller |
||
26 | * will skip login check for them. |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $skipLoginCheck = ['login', 'register']; |
||
30 | |||
31 | /** |
||
32 | * The validations rules used by the base api controller |
||
33 | * to check before add. |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $validationRules = [ |
||
37 | 'email' => 'required|email|unique:users,email,{id}', |
||
38 | 'password' => 'min:6' |
||
39 | ]; |
||
40 | |||
41 | /** |
||
42 | * Return the logged in user account. |
||
43 | * |
||
44 | * @return object |
||
45 | */ |
||
46 | public function getAccount() |
||
51 | |||
52 | /** |
||
53 | * Logout the user. |
||
54 | * |
||
55 | * @return void |
||
56 | */ |
||
57 | public function getLogout() |
||
61 | |||
62 | /** |
||
63 | * Handle a registration request. |
||
64 | * |
||
65 | * @param \Illuminate\Http\Request $request |
||
66 | * @return \Illuminate\Http\Response |
||
67 | */ |
||
68 | public function postRegister(Request $request) |
||
74 | |||
75 | /** |
||
76 | * Handle a login request to the application. |
||
77 | * |
||
78 | * @param \Illuminate\Http\Request $request |
||
79 | * @return \Illuminate\Http\Response |
||
80 | */ |
||
81 | public function postLogin(Request $request) |
||
94 | |||
95 | /** |
||
96 | * Handle an assign groups to user request. |
||
97 | * |
||
98 | * @param \Illuminate\Http\Request $request |
||
99 | * @return \Illuminate\Http\Response |
||
100 | */ |
||
101 | View Code Duplication | public function postAssigngroups(Request $request) |
|
110 | } |
||
111 |
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.