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 |
||
32 | class IndexController extends Controller |
||
33 | { |
||
34 | /** |
||
35 | * Returns listing of user's servers. |
||
36 | * |
||
37 | * @param \Illuminate\Http\Request $request |
||
38 | * @return \Illuminate\View\View |
||
39 | */ |
||
40 | View Code Duplication | public function getIndex(Request $request) |
|
|
|||
41 | { |
||
42 | $servers = $request->user()->access()->with('user'); |
||
43 | |||
44 | if (! is_null($request->input('query'))) { |
||
45 | $servers->search($request->input('query')); |
||
46 | } |
||
47 | |||
48 | return view('base.index', [ |
||
49 | 'servers' => $servers->paginate(config('pterodactyl.paginate.frontend.servers')), |
||
50 | ]); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Generate a random string. |
||
55 | * |
||
56 | * @param \Illuminate\Http\Request $request |
||
57 | * @param int $length |
||
58 | * @return string |
||
59 | * @deprecated |
||
60 | */ |
||
61 | public function getPassword(Request $request, $length = 16) |
||
75 | |||
76 | /** |
||
77 | * Returns status of the server in a JSON response used for populating active status list. |
||
78 | * |
||
79 | * @param \Illuminate\Http\Request $request |
||
80 | * @param string $uuid |
||
81 | * @return \Illuminate\Http\JsonResponse |
||
82 | */ |
||
83 | public function status(Request $request, $uuid) |
||
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.