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 namespace Modules\Blog\Http\Controllers\Admin; |
||
9 | class TagController extends AdminBaseController |
||
10 | { |
||
11 | /** |
||
12 | * @var TagRepository |
||
13 | */ |
||
14 | private $tag; |
||
15 | |||
16 | public function __construct(TagRepository $tag) |
||
22 | |||
23 | /** |
||
24 | * Display a listing of the resource. |
||
25 | * |
||
26 | * @return Response |
||
27 | */ |
||
28 | public function index() |
||
34 | |||
35 | /** |
||
36 | * Show the form for creating a new resource. |
||
37 | * |
||
38 | * @return Response |
||
39 | */ |
||
40 | public function create() |
||
44 | |||
45 | /** |
||
46 | * Store a newly created resource in storage. |
||
47 | * |
||
48 | * @param Request $request |
||
49 | * @return Response |
||
50 | */ |
||
51 | View Code Duplication | public function store(Request $request) |
|
59 | |||
60 | /** |
||
61 | * Show the form for editing the specified resource. |
||
62 | * |
||
63 | * @param Tag $tag |
||
64 | * @return Response |
||
65 | */ |
||
66 | public function edit(Tag $tag) |
||
70 | |||
71 | /** |
||
72 | * Update the specified resource in storage. |
||
73 | * |
||
74 | * @param Tag $tag |
||
75 | * @param Request $request |
||
76 | * @return Response |
||
77 | */ |
||
78 | View Code Duplication | public function update(Tag $tag, Request $request) |
|
86 | |||
87 | /** |
||
88 | * Remove the specified resource from storage. |
||
89 | * |
||
90 | * @param Tag $tag |
||
91 | * @return Response |
||
92 | */ |
||
93 | View Code Duplication | public function destroy(Tag $tag) |
|
101 | } |
||
102 |
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.