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) |
|
| 44 | { |
||
| 45 | $issue->attachments->each(function (Attachment $attachment) use ($issue) { |
||
|
|
|||
| 46 | 1 | $attachment->setRelation('issue', $issue); |
|
| 47 | 18 | }); |
|
| 48 | 18 | $activities = $issue->activities()->with('activity', 'user', 'comment', 'assignTo', |
|
| 49 | 18 | 'comment.attachments')->get(); |
|
| 50 | 18 | $activities->each(function (UserActivity $activity) use ($issue) { |
|
| 51 | 18 | $activity->setRelation('issue', $issue); |
|
| 52 | 18 | }); |
|
| 53 | |||
| 54 | // Projects should be limited to issue-modify |
||
| 55 | 18 | $projects = null; |
|
| 56 | 18 | if (!$this->auth->guest() && $this->auth->user()->permission('issue-modify')) { |
|
| 57 | 15 | $projects = $this->auth->user()->projects()->get(); |
|
| 58 | } |
||
| 59 | |||
| 60 | 18 | return view('project.issue.index', [ |
|
| 61 | 18 | 'issue' => $issue, |
|
| 62 | 18 | 'project' => $project, |
|
| 63 | 18 | 'commentForm' => $form, |
|
| 64 | 18 | 'activities' => $activities, |
|
| 65 | 18 | 'sidebar' => 'project', |
|
| 66 | 18 | 'projects' => $projects, |
|
| 67 | ]); |
||
| 68 | } |
||
| 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) |
| 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) |
|
| 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) |
|
| 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) |
|
| 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) |
|
| 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) |
| 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) |
|
| 188 | { |
||
| 189 | // Cannot edit closed issue |
||
| 190 | 2 | if ($issue->status == Issue::STATUS_CLOSED) { |
|
| 191 | 1 | return redirect($issue->to()) |
|
| 192 | 1 | ->with('notice', trans('tinyissue.cant_edit_closed_issue')); |
|
| 193 | } |
||
| 194 | |||
| 195 | 2 | return view('project.issue.edit', [ |
|
| 196 | 2 | 'issue' => $issue, |
|
| 197 | 2 | 'project' => $project, |
|
| 198 | 2 | 'form' => $form, |
|
| 199 | 2 | 'sidebar' => 'project', |
|
| 200 | ]); |
||
| 201 | } |
||
| 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) |
| 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) |
|
| 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) |
|
| 256 | { |
||
| 257 | try { |
||
| 258 | 3 | if (!$this->auth->user()->permission('project-all')) { |
|
| 259 | abort(404); |
||
| 260 | } |
||
| 261 | |||
| 262 | 3 | $attachment->upload($request->all(), $project, $this->auth->user()); |
|
| 263 | |||
| 264 | $response = [ |
||
| 265 | 'upload' => [ |
||
| 266 | [ |
||
| 267 | 3 | 'name' => $attachment->filename, |
|
| 268 | 3 | 'size' => $attachment->filesize, |
|
| 269 | 3 | 'fileId' => $attachment->id, |
|
| 270 | ], |
||
| 271 | ], |
||
| 272 | ]; |
||
| 273 | } catch (\Exception $exception) { |
||
| 274 | $file = $request->file('upload'); |
||
| 275 | |||
| 276 | $response = [ |
||
| 277 | 'status' => false, |
||
| 278 | 'name' => $file->getClientOriginalName(), |
||
| 279 | 'error' => $exception->getMessage(), |
||
| 280 | 'trace' => $exception->getTraceAsString(), |
||
| 281 | ]; |
||
| 282 | } |
||
| 283 | |||
| 284 | 3 | return response()->json($response); |
|
| 285 | } |
||
| 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) |
|
| 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) |
|
| 314 | { |
||
| 315 | 2 | $issue->setRelation('project', $project); |
|
| 316 | 2 | $attachment->setRelation('issue', $issue); |
|
| 317 | |||
| 318 | 2 | $path = config('tinyissue.uploads_dir') . '/' . $issue->project_id . '/' . $attachment->upload_token . '/' . $attachment->filename; |
|
| 319 | 2 | $storage = \Storage::disk('local'); |
|
| 320 | 2 | $length = $storage->size($path); |
|
| 321 | 2 | $time = $storage->lastModified($path); |
|
| 322 | 2 | $type = $storage->getDriver()->getMimetype($path); |
|
| 323 | |||
| 324 | 2 | $response = new Response(); |
|
| 325 | 2 | $response->setEtag(md5($time . $path)); |
|
| 326 | 2 | $response->setExpires(new \DateTime('@' . ($time + 60))); |
|
| 327 | 2 | $response->setLastModified(new \DateTime('@' . $time)); |
|
| 328 | 2 | $response->setPublic(); |
|
| 329 | 2 | $response->setStatusCode(200); |
|
| 330 | |||
| 331 | 2 | $response->header('Content-Type', $type); |
|
| 332 | 2 | $response->header('Content-Length', $length); |
|
| 333 | 2 | $response->header('Content-Disposition', 'inline; filename="' . $attachment->filename . '"'); |
|
| 334 | 2 | $response->header('Cache-Control', 'must-revalidate'); |
|
| 335 | |||
| 336 | 2 | if ($response->isNotModified($request)) { |
|
| 337 | // Return empty response if not modified |
||
| 338 | return $response; |
||
| 339 | } |
||
| 340 | |||
| 341 | // Return file if first request / modified |
||
| 342 | 2 | $response->setContent($storage->get($path)); |
|
| 343 | |||
| 344 | 2 | return $response; |
|
| 345 | } |
||
| 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) |
|
| 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) |
|
| 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 postChangeStatusTag(Issue $issue, Request $request) |
||
| 397 | } |
||
| 398 |
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@propertyannotation 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.