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 AppsController |
||
10 | { |
||
11 | /** |
||
12 | * Display a listing of the resource. |
||
13 | * |
||
14 | * @param Request $request |
||
15 | * @return \Illuminate\Http\Response |
||
16 | */ |
||
17 | public function index(Request $request) |
||
27 | |||
28 | /** |
||
29 | * Show the form for creating a new resource. |
||
30 | * |
||
31 | * @return \Illuminate\Http\Response |
||
32 | */ |
||
33 | public function create() |
||
39 | |||
40 | /** |
||
41 | * Store a newly created resource in storage. |
||
42 | * |
||
43 | * @param StoreWebSocketsApp $request |
||
44 | * @return \Illuminate\Http\Response |
||
45 | */ |
||
46 | View Code Duplication | public function store(StoreWebSocketsApp $request) |
|
59 | |||
60 | /** |
||
61 | * Show the form for editing the specified resource. |
||
62 | * |
||
63 | * @param App $app |
||
64 | * @return \Illuminate\Http\Response |
||
65 | */ |
||
66 | public function edit(App $app) |
||
72 | |||
73 | /** |
||
74 | * Update the specified resource in storage. |
||
75 | * |
||
76 | * @param StoreWebSocketsApp $request |
||
77 | * @param App $app |
||
78 | * @return \Illuminate\Http\Response |
||
79 | */ |
||
80 | View Code Duplication | public function update(StoreWebSocketsApp $request, App $app) |
|
91 | |||
92 | /** |
||
93 | * Remove the specified resource from storage. |
||
94 | * |
||
95 | * @param App $app |
||
96 | * @return \Illuminate\Http\Response |
||
97 | */ |
||
98 | public function destroy(App $app) |
||
104 | } |
||
105 |
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.