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 |
||
12 | View Code Duplication | class EmployeesController extends Controller |
|
|
|||
13 | { |
||
14 | /** |
||
15 | * @var \plunner\Company |
||
16 | */ |
||
17 | private $user; |
||
18 | |||
19 | /** |
||
20 | * ExampleController constructor. |
||
21 | */ |
||
22 | public function __construct() |
||
28 | |||
29 | |||
30 | /** |
||
31 | * Display a listing of the resource. |
||
32 | * |
||
33 | * @return \Illuminate\Http\Response |
||
34 | */ |
||
35 | public function index() |
||
44 | |||
45 | /** |
||
46 | * Store a newly created resource in storage. |
||
47 | * |
||
48 | * @param \Illuminate\Http\Request $request |
||
49 | * @return \Illuminate\Http\Response |
||
50 | */ |
||
51 | public function store(Request $request) |
||
55 | |||
56 | /** |
||
57 | * Display the specified resource. |
||
58 | * |
||
59 | * @param int $id |
||
60 | * @return \Illuminate\Http\Response |
||
61 | */ |
||
62 | public function show($id) |
||
66 | |||
67 | /** |
||
68 | * Update the specified resource in storage. |
||
69 | * |
||
70 | * @param \Illuminate\Http\Request $request |
||
71 | * @param int $id |
||
72 | * @return \Illuminate\Http\Response |
||
73 | */ |
||
74 | public function update(Request $request, $id) |
||
78 | |||
79 | /** |
||
80 | * Remove the specified resource from storage. |
||
81 | * |
||
82 | * @param int $id |
||
83 | * @return \Illuminate\Http\Response |
||
84 | */ |
||
85 | public function destroy($id) |
||
89 | } |
||
90 |
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.