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 |
||
32 | class IssueController extends Controller |
||
33 | { |
||
34 | /** |
||
35 | * Project issue index page (List project issues). |
||
36 | * |
||
37 | * @param Project $project |
||
38 | * @param Issue $issue |
||
39 | * @param CommentForm $form |
||
40 | * |
||
41 | * @return \Illuminate\View\View |
||
42 | */ |
||
43 | 18 | public function getIndex(Project $project, Issue $issue, CommentForm $form) |
|
69 | |||
70 | /** |
||
71 | * Ajax: Assign new user to an issue. |
||
72 | * |
||
73 | * @param Issue $issue |
||
74 | * @param Request $request |
||
75 | * |
||
76 | * @return \Symfony\Component\HttpFoundation\Response |
||
77 | */ |
||
78 | 1 | View Code Duplication | public function postAssign(Issue $issue, Request $request) |
79 | { |
||
80 | 1 | $response = ['status' => false]; |
|
81 | 1 | if ($issue->reassign((int) $request->input('user_id'), $this->auth->user()->id)) { |
|
82 | 1 | $response['status'] = true; |
|
83 | } |
||
84 | |||
85 | 1 | return response()->json($response); |
|
86 | } |
||
87 | |||
88 | /** |
||
89 | * Ajax: save comment. |
||
90 | * |
||
91 | * @param Comment $comment |
||
92 | * @param Request $request |
||
93 | * |
||
94 | * @return \Symfony\Component\HttpFoundation\Response |
||
95 | */ |
||
96 | 1 | public function postEditComment(Comment $comment, Request $request) |
|
97 | { |
||
98 | 1 | $body = ''; |
|
99 | 1 | if ($request->has('body')) { |
|
100 | 1 | $comment->fill(['comment' => $request->input('body')])->save(); |
|
101 | 1 | $body = \Html::format($comment->comment); |
|
102 | } |
||
103 | |||
104 | 1 | return response()->json(['text' => $body]); |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * To add new comment to an issue. |
||
109 | * |
||
110 | * @param Project $project |
||
111 | * @param Issue $issue |
||
112 | * @param Comment $comment |
||
113 | * @param FormRequest\Comment $request |
||
114 | * |
||
115 | * @return \Illuminate\Http\RedirectResponse |
||
116 | */ |
||
117 | 4 | public function getAddComment(Project $project, Issue $issue, Comment $comment, FormRequest\Comment $request) |
|
118 | { |
||
119 | 4 | $comment->setRelation('project', $project); |
|
120 | 4 | $comment->setRelation('issue', $issue); |
|
121 | 4 | $comment->setRelation('user', $this->auth->user()); |
|
122 | 4 | $comment->createComment($request->all()); |
|
123 | |||
124 | 4 | return redirect($issue->to() . '#comment' . $comment->id) |
|
125 | 4 | ->with('notice', trans('tinyissue.your_comment_added')); |
|
126 | } |
||
127 | |||
128 | /** |
||
129 | * Ajax: to delete a comment. |
||
130 | * |
||
131 | * @param Comment $comment |
||
132 | * |
||
133 | * @return \Symfony\Component\HttpFoundation\Response |
||
134 | */ |
||
135 | 1 | public function getDeleteComment(Comment $comment) |
|
136 | { |
||
137 | 1 | $comment->deleteComment(); |
|
138 | |||
139 | 1 | return response()->json(['status' => true]); |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * New issue form. |
||
144 | * |
||
145 | * @param Project $project |
||
146 | * @param IssueForm $form |
||
147 | * |
||
148 | * @return \Illuminate\View\View |
||
149 | */ |
||
150 | 5 | public function getNew(Project $project, IssueForm $form) |
|
151 | { |
||
152 | 5 | return view('project.issue.new', [ |
|
153 | 5 | 'project' => $project, |
|
154 | 5 | 'form' => $form, |
|
155 | 5 | 'sidebar' => 'project', |
|
156 | ]); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * To create a new issue. |
||
161 | * |
||
162 | * @param Project $project |
||
163 | * @param Issue $issue |
||
164 | * @param FormRequest\Issue $request |
||
165 | * |
||
166 | * @return \Illuminate\Http\RedirectResponse |
||
167 | */ |
||
168 | 2 | View Code Duplication | public function postNew(Project $project, Issue $issue, FormRequest\Issue $request) |
169 | { |
||
170 | 2 | $issue->setRelation('project', $project); |
|
171 | 2 | $issue->setRelation('user', $this->auth->user()); |
|
172 | 2 | $issue->createIssue($request->all()); |
|
173 | |||
174 | 2 | return redirect($issue->to()) |
|
175 | 2 | ->with('notice', trans('tinyissue.issue_has_been_created')); |
|
176 | } |
||
177 | |||
178 | /** |
||
179 | * Edit an existing issue form. |
||
180 | * |
||
181 | * @param Project $project |
||
182 | * @param Issue $issue |
||
183 | * @param IssueForm $form |
||
184 | * |
||
185 | * @return \Illuminate\View\View |
||
186 | */ |
||
187 | 2 | public function getEdit(Project $project, Issue $issue, IssueForm $form) |
|
202 | |||
203 | /** |
||
204 | * To update an existing issue details. |
||
205 | * |
||
206 | * @param Project $project |
||
207 | * @param Issue $issue |
||
208 | * @param FormRequest\Issue $request |
||
209 | * |
||
210 | * @return \Illuminate\Http\RedirectResponse |
||
211 | */ |
||
212 | 1 | View Code Duplication | public function postEdit(Project $project, Issue $issue, FormRequest\Issue $request) |
213 | { |
||
214 | 1 | $issue->setRelation('project', $project); |
|
215 | 1 | $issue->setRelation('updatedBy', $this->auth->user()); |
|
216 | 1 | $issue->updateIssue($request->all()); |
|
217 | |||
218 | 1 | return redirect($issue->to()) |
|
219 | 1 | ->with('notice', trans('tinyissue.issue_has_been_updated')); |
|
220 | } |
||
221 | |||
222 | /** |
||
223 | * To close or reopen an issue. |
||
224 | * |
||
225 | * @param Project $project |
||
226 | * @param Issue $issue |
||
227 | * @param int $status |
||
228 | * |
||
229 | * @return \Illuminate\Http\RedirectResponse |
||
230 | */ |
||
231 | 2 | public function getClose(Project $project, Issue $issue, $status = 0) |
|
232 | { |
||
233 | 2 | if ($status == 0) { |
|
234 | 2 | $message = trans('tinyissue.issue_has_been_closed'); |
|
235 | } else { |
||
236 | 1 | $message = trans('tinyissue.issue_has_been_reopened'); |
|
237 | } |
||
238 | |||
239 | 2 | $issue->setRelation('project', $project); |
|
240 | 2 | $issue->changeStatus($status, $this->auth->user()->id); |
|
241 | |||
242 | 2 | return redirect($issue->to()) |
|
243 | 2 | ->with('notice', $message); |
|
244 | } |
||
245 | |||
246 | /** |
||
247 | * To upload an attachment file. |
||
248 | * |
||
249 | * @param Project $project |
||
250 | * @param Attachment $attachment |
||
251 | * @param Request $request |
||
252 | * |
||
253 | * @return \Symfony\Component\HttpFoundation\Response |
||
254 | */ |
||
255 | 3 | public function postUploadAttachment(Project $project, Attachment $attachment, Request $request) |
|
286 | |||
287 | /** |
||
288 | * Ajax: to remove an attachment file. |
||
289 | * |
||
290 | * @param Project $project |
||
291 | * @param Attachment $attachment |
||
292 | * @param Request $request |
||
293 | * |
||
294 | * @return \Symfony\Component\HttpFoundation\Response |
||
295 | */ |
||
296 | 1 | public function postRemoveAttachment(Project $project, Attachment $attachment, Request $request) |
|
297 | { |
||
298 | 1 | $attachment->remove($request->all(), $project, $this->auth->user()); |
|
299 | |||
300 | 1 | return response()->json(['status' => true]); |
|
301 | } |
||
302 | |||
303 | /** |
||
304 | * Display an attachment file such as image. |
||
305 | * |
||
306 | * @param Project $project |
||
307 | * @param Issue $issue |
||
308 | * @param Attachment $attachment |
||
309 | * @param Request $request |
||
310 | * |
||
311 | * @return Response |
||
312 | */ |
||
313 | 2 | public function getDisplayAttachment(Project $project, Issue $issue, Attachment $attachment, Request $request) |
|
346 | |||
347 | /** |
||
348 | * Download an attachment file. |
||
349 | * |
||
350 | * @param Project $project |
||
351 | * @param Issue $issue |
||
352 | * @param Attachment $attachment |
||
353 | * |
||
354 | * @return \Symfony\Component\HttpFoundation\BinaryFileResponse |
||
355 | */ |
||
356 | 1 | public function getDownloadAttachment(Project $project, Issue $issue, Attachment $attachment) |
|
357 | { |
||
358 | 1 | $issue->setRelation('project', $project); |
|
359 | 1 | $attachment->setRelation('issue', $issue); |
|
360 | |||
361 | 1 | $path = config('filesystems.disks.local.root') . '/' . config('tinyissue.uploads_dir') . '/' . $issue->project_id . '/' . $attachment->upload_token . '/' . $attachment->filename; |
|
362 | |||
363 | 1 | return response()->download($path, $attachment->filename); |
|
364 | } |
||
365 | |||
366 | /** |
||
367 | * Ajax: move an issue to another project. |
||
368 | * |
||
369 | * @param Issue $issue |
||
370 | * @param Request $request |
||
371 | * |
||
372 | * @return \Symfony\Component\HttpFoundation\Response |
||
373 | */ |
||
374 | 1 | public function postChangeProject(Issue $issue, Request $request) |
|
375 | { |
||
376 | 1 | $issue->changeProject($request->input('project_id')); |
|
377 | |||
378 | 1 | return response()->json(['status' => true, 'url' => $issue->to()]); |
|
379 | } |
||
380 | |||
381 | /** |
||
382 | * Ajax: change status of an issue. |
||
383 | * |
||
384 | * @param Issue $issue |
||
385 | * @param Request $request |
||
386 | * |
||
387 | * @return \Symfony\Component\HttpFoundation\Response |
||
388 | */ |
||
389 | public function postChangeKanbanTag(Issue $issue, Request $request) |
||
398 | } |
||
399 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.