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 |
||
13 | class RoleController extends Controller |
||
14 | { |
||
15 | use HasResourceActions; |
||
16 | |||
17 | /** |
||
18 | * Index interface. |
||
19 | * |
||
20 | * @param Content $content |
||
21 | * @return Content |
||
22 | */ |
||
23 | public function index(Content $content) |
||
30 | |||
31 | /** |
||
32 | * Show interface. |
||
33 | * |
||
34 | * @param mixed $id |
||
35 | * @param Content $content |
||
36 | * @return Content |
||
37 | */ |
||
38 | public function show($id, Content $content) |
||
45 | |||
46 | /** |
||
47 | * Edit interface. |
||
48 | * |
||
49 | * @param mixed $id |
||
50 | * @param Content $content |
||
51 | * @return Content |
||
52 | */ |
||
53 | View Code Duplication | public function edit($id, Content $content) |
|
60 | |||
61 | /** |
||
62 | * Create interface. |
||
63 | * |
||
64 | * @param Content $content |
||
65 | * @return Content |
||
66 | */ |
||
67 | public function create(Content $content) |
||
74 | |||
75 | /** |
||
76 | * Make a grid builder. |
||
77 | * |
||
78 | * @return Grid |
||
79 | */ |
||
80 | View Code Duplication | protected function grid() |
|
107 | |||
108 | /** |
||
109 | * Make a show builder. |
||
110 | * |
||
111 | * @param mixed $id |
||
112 | * @return Show |
||
113 | */ |
||
114 | protected function detail($id) |
||
129 | |||
130 | /** |
||
131 | * Make a form builder. |
||
132 | * |
||
133 | * @return Form |
||
134 | */ |
||
135 | public function form() |
||
150 | } |
||
151 |
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.