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 |
||
10 | class AdminController extends Controller |
||
11 | { |
||
12 | public function index(ComponentInterface $component) |
||
13 | { |
||
14 | if (!$component->isView()) { |
||
15 | throw new AuthorizationException(); |
||
16 | } |
||
17 | |||
18 | return view('admin::app'); |
||
19 | } |
||
20 | |||
21 | public function getList(ComponentInterface $component) |
||
22 | { |
||
23 | if (!$component->isView()) { |
||
24 | throw new AuthorizationException(); |
||
25 | } |
||
26 | |||
27 | return $component->get(); |
||
28 | } |
||
29 | |||
30 | |||
31 | public function config(ComponentInterface $component) |
||
32 | { |
||
33 | if (!$component->isView()) { |
||
34 | throw new AuthorizationException(); |
||
35 | } |
||
36 | |||
37 | return $component->getConfigs(); |
||
38 | } |
||
39 | |||
40 | public function getCreateInfo(ComponentInterface $component) |
||
41 | { |
||
42 | if (!$component->isCreate()) { |
||
43 | throw new AuthorizationException(); |
||
44 | } |
||
45 | |||
46 | $form = $component->fireCreate(); |
||
47 | return $form; |
||
48 | } |
||
49 | |||
50 | public function create(ComponentInterface $component) |
||
51 | { |
||
52 | if (!$component->isCreate()) { |
||
53 | throw new AuthorizationException(); |
||
54 | } |
||
55 | |||
56 | return view('admin::app'); |
||
57 | } |
||
58 | |||
59 | public function store(ComponentInterface $component, Request $request) |
||
|
|||
60 | { |
||
61 | if (!$component->isCreate()) { |
||
62 | throw new AuthorizationException(); |
||
63 | } |
||
64 | |||
65 | $component->store(); |
||
66 | } |
||
67 | |||
68 | public function getEditInfo(ComponentInterface $component, $id) |
||
69 | { |
||
70 | if (!$component->isEdit()) { |
||
71 | throw new AuthorizationException(); |
||
72 | } |
||
73 | |||
74 | $form = $component->fireEdit($id); |
||
75 | return $form; |
||
76 | } |
||
77 | |||
78 | public function edit(ComponentInterface $component, $id) |
||
79 | { |
||
80 | if (!$component->isEdit()) { |
||
81 | throw new AuthorizationException(); |
||
82 | } |
||
83 | |||
84 | return view('admin::app'); |
||
85 | } |
||
86 | |||
87 | public function update(ComponentInterface $component, Request $request, $id) |
||
88 | { |
||
89 | if (!$component->isEdit()) { |
||
90 | throw new AuthorizationException(); |
||
91 | } |
||
92 | |||
93 | $component->update($id); |
||
94 | return response()->json(['message' => 'ok']); |
||
95 | } |
||
96 | |||
97 | View Code Duplication | public function delete(ComponentInterface $component, $id) |
|
98 | { |
||
99 | if (!$component->isDelete()) { |
||
100 | throw new AuthorizationException(); |
||
101 | } |
||
102 | |||
103 | $component->delete($id); |
||
104 | return response()->json(['message' => 'ok']); |
||
105 | } |
||
106 | |||
107 | public function forceDelete(ComponentInterface $component, $id) |
||
116 | |||
117 | public function restore(ComponentInterface $component, $id) |
||
125 | |||
126 | View Code Duplication | public function reorder(ComponentInterface $component) |
|
127 | { |
||
128 | if (!$component->isEdit()) { |
||
129 | throw new AuthorizationException(); |
||
130 | } |
||
131 | |||
132 | return response()->json(['message' => 'ok']); |
||
133 | } |
||
134 | } |
||
135 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.