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 |
||
16 | class PostController extends Controller |
||
17 | { |
||
18 | /** |
||
19 | * Create a post for a conversation. |
||
20 | * |
||
21 | * @param \Illuminate\Http\Request $request |
||
22 | * |
||
23 | * @return \Illuminate\Http\RedirectResponse |
||
24 | */ |
||
25 | public function create(Request $request): RedirectResponse |
||
26 | { |
||
27 | $conversation = DiscussConversation::findOrFail($request->conversation_id); |
||
|
|||
28 | |||
29 | // Users that have the permission "manage.discuss" can bypass this rule. (Default to Administrator) |
||
30 | View Code Duplication | if (DiscussPost::isFlooding('xetaravel.flood.discuss.post') && !Auth::user()->hasPermission('manage.discuss')) { |
|
31 | return back() |
||
32 | ->withInput() |
||
33 | ->with('danger', 'Wow, keep calm bro, and try to not flood !'); |
||
34 | } |
||
35 | |||
36 | DiscussPostValidator::create($request->all())->validate(); |
||
37 | $post = DiscussPostRepository::create($request->all()); |
||
38 | $user = DiscussUserRepository::create($request->all()); |
||
39 | |||
40 | $parser = new MentionParser($post); |
||
41 | $content = $parser->parse($post->content); |
||
42 | |||
43 | $post->content = $content; |
||
44 | $post->save(); |
||
45 | |||
46 | return redirect() |
||
47 | ->route('discuss.post.show', ['id' => $post->getKey()]) |
||
48 | ->with('success', 'Your reply has been posted successfully !'); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Redirect an user to a conversation, page and post. |
||
53 | * |
||
54 | * @param \Illuminate\Http\Request $request |
||
55 | * @param int $id The ID of the post. |
||
56 | * |
||
57 | * @return \Illuminate\Http\RedirectResponse |
||
58 | */ |
||
59 | View Code Duplication | public function show(Request $request, int $id): RedirectResponse |
|
86 | |||
87 | /** |
||
88 | * Handle a delete action for the post. |
||
89 | * |
||
90 | * @param \Illuminate\Http\Request $request |
||
91 | * @param int $id |
||
92 | * |
||
93 | * @return \Illuminate\Http\RedirectResponse |
||
94 | */ |
||
95 | public function delete(int $id): RedirectResponse |
||
120 | |||
121 | /** |
||
122 | * Mark as solved. |
||
123 | * |
||
124 | * @param int $id |
||
125 | * |
||
126 | * @return \Illuminate\Http\RedirectResponse |
||
127 | */ |
||
128 | public function solved(int $id): RedirectResponse |
||
153 | |||
154 | /** |
||
155 | * Handle an edit action for the post. |
||
156 | * |
||
157 | * @param Request $request |
||
158 | * @param int $id The id of the post to edit. |
||
159 | * |
||
160 | * @return \Illuminate\Http\RedirectResponse |
||
161 | */ |
||
162 | public function edit(Request $request, int $id) : RedirectResponse |
||
187 | |||
188 | /** |
||
189 | * Get the edit json template. |
||
190 | * |
||
191 | * @param int $id |
||
192 | * |
||
193 | * @return \Illuminate\Http\JsonResponse|\Illuminate\View\View |
||
194 | */ |
||
195 | public function editTemplate(int $id) |
||
212 | } |
||
213 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.