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:
Complex classes like ApiController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ApiController, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Taskforcedev\LaravelForum\Http\Controllers; |
||
21 | class ApiController extends BaseController |
||
22 | { |
||
23 | public function forumCategoryStore(Request $request) |
||
24 | { |
||
25 | $data = [ |
||
26 | "name" => $request->input('name'), |
||
27 | ]; |
||
28 | |||
29 | $response = $this->adminCheck(); |
||
30 | if (isset($response)) { |
||
31 | return $response; |
||
32 | } |
||
33 | |||
34 | /* If data invalid return bad request */ |
||
35 | if (!ForumCategory::valid($data)) { |
||
36 | return response('Bad Request', 400); |
||
37 | } |
||
38 | |||
39 | ForumCategory::create($data); |
||
40 | |||
41 | /* After creating a category lets redirect back to the category list. */ |
||
42 | return redirect()->route('laravel-forum.admin.categories'); |
||
43 | } |
||
44 | |||
45 | public function forumStore(Request $request) |
||
46 | { |
||
47 | $data = [ |
||
48 | "name" => $request->input('name'), |
||
49 | "description" => $request->input('description'), |
||
50 | "category_id" => $request->input('category'), |
||
51 | ]; |
||
52 | |||
53 | $response = $this->adminCheck(); |
||
54 | if (isset($response)) { |
||
55 | return $response; |
||
56 | } |
||
57 | |||
58 | if (!Forum::valid($data)) { |
||
59 | return response('Bad Request', 400); |
||
60 | } |
||
61 | |||
62 | Forum::create($data); |
||
63 | |||
64 | /* After creating a forum lets redirect back to the forum list. */ |
||
65 | return redirect()->route('laravel-forum.admin.forums'); |
||
66 | } |
||
67 | |||
68 | public function forumPostStore(Request $request) |
||
69 | { |
||
70 | if (!Auth::check()) { |
||
71 | return response('Unauthorized', 401); |
||
72 | } |
||
73 | |||
74 | $user = Auth::user(); |
||
75 | |||
76 | $forum_id = $request->input('forum_id'); |
||
77 | |||
78 | $data = [ |
||
79 | "author_id" => $user->id, |
||
80 | "title" => $request->input('title'), |
||
81 | "body" => $this->sanitizeData($request->input('body')), |
||
82 | "forum_id" => $forum_id |
||
83 | ]; |
||
84 | |||
85 | if (!ForumPost::valid($data)) { |
||
86 | return response('Bad Request', 400); |
||
87 | } |
||
88 | |||
89 | $post = ForumPost::create($data); |
||
90 | |||
91 | event(new PostCreated($post, $user)); |
||
92 | return redirect()->route('laravel-forum.view.post', [$forum_id , $post->id]); |
||
93 | } |
||
94 | |||
95 | public function forumReplyStore(Request $request) |
||
96 | { |
||
97 | if (!Auth::check()) { |
||
98 | return response('Unauthorized', 401); |
||
99 | } |
||
100 | |||
101 | $user = Auth::user(); |
||
102 | |||
103 | $forum_id = $request->input('forum_id'); |
||
104 | $post_id = $request->input('post_id'); |
||
105 | |||
106 | $data = [ |
||
107 | 'author_id' => $user->id, |
||
108 | 'body' => $this->sanitizeData($request->input('body')), |
||
109 | 'post_id' => $post_id, |
||
110 | ]; |
||
111 | |||
112 | if (!ForumReply::valid($data)) { |
||
113 | return redirect()->route('laravel-forum.view.post', [$forum_id, $post_id]); |
||
114 | } |
||
115 | |||
116 | $reply = ForumReply::create($data); |
||
117 | |||
118 | event(new PostReply($reply, $user)); |
||
119 | return redirect()->route('laravel-forum.view.post', [$forum_id, $post_id]); |
||
120 | } |
||
121 | |||
122 | private function adminCheck() |
||
128 | |||
129 | private function sanitizeData($data) |
||
141 | |||
142 | View Code Duplication | public function lockPost(Request $request, $id) |
|
143 | { |
||
157 | |||
158 | View Code Duplication | public function unlockPost(Request $request, $id) |
|
173 | |||
174 | View Code Duplication | public function stickyPost(Request $request, $id) |
|
189 | |||
190 | View Code Duplication | public function unstickyPost(Request $request, $id) |
|
205 | |||
206 | private function postExists($post_id) |
||
215 | |||
216 | View Code Duplication | public function postDelete($forum_id, $post_id) |
|
217 | { |
||
218 | if (!$this->canAdministrate() && !$this->canModerate()) { |
||
219 | return response('Unauthorised', 401); |
||
220 | } |
||
221 | |||
222 | $post = $this->postExists($post_id); |
||
223 | if (!$post) { |
||
224 | return response('Post not found', 404); |
||
225 | } |
||
226 | |||
227 | $post->delete(); |
||
228 | return response('Post Deleted', 200); |
||
229 | } |
||
230 | |||
231 | View Code Duplication | public function forumDelete(Request $request) |
|
247 | |||
248 | private function forumExists($id) |
||
257 | |||
258 | View Code Duplication | public function forumCategoryDelete(Request $request) |
|
274 | |||
275 | private function forumCategoryExists(Request $request, $id) |
||
284 | } |
||
285 |
This check looks for function calls that miss required arguments.