IssueController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 95
Duplicated Lines 16.84 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 8
c 2
b 1
f 0
lcom 1
cbo 8
dl 16
loc 95
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getIssues() 0 9 2
A getIssue() 0 4 1
A postIssues() 0 15 2
A putIssue() 16 16 2
A deleteIssue() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

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
2
3
/*
4
 * This file is part of Gitamin.
5
 *
6
 * Copyright (C) 2015-2016 The Gitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Gitamin\Http\Controllers\Api;
13
14
use Gitamin\Commands\Issue\AddIssueCommand;
15
use Gitamin\Commands\Issue\RemoveIssueCommand;
16
use Gitamin\Commands\Issue\UpdateIssueCommand;
17
use Gitamin\Models\Issue;
18
use Illuminate\Contracts\Auth\Guard;
19
use Illuminate\Database\QueryException;
20
use Illuminate\Foundation\Bus\DispatchesJobs;
21
use Illuminate\Http\Request;
22
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
23
24
class IssueController extends AbstractApiController
25
{
26
    use DispatchesJobs;
27
28
    /**
29
     * Get all issues.
30
     *
31
     * @param \Symfony\Component\HttpFoundation\Request $request
32
     * @param \Illuminate\Contracts\Auth\Guard          $auth
33
     *
34
     * @return \Illuminate\Http\JsonResponse
35
     */
36
    public function getIssues(Request $request, Guard $auth)
37
    {
38
        //$issuePosition = $auth->check() ? 0 : 1;
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
39
        $issuePosition = $auth->check() ? 0 : -1;
40
41
        $issues = Issue::where('position', '>=', $issuePosition)->paginate($request->get('per_page', 20));
42
43
        return $this->paginator($issues, $request);
44
    }
45
46
    /**
47
     * Get a single issue.
48
     *
49
     * @param \Gitamin\Models\Issue $issue
50
     *
51
     * @return \Illuminate\Http\JsonResponse
52
     */
53
    public function getIssue(Issue $issue)
54
    {
55
        return $this->item($issue);
56
    }
57
58
    /**
59
     * Create a new issue.
60
     *
61
     * @param \Illuminate\Contracts\Auth\Guard $auth
62
     *
63
     * @return \Illuminate\Http\JsonResponse
64
     */
65
    public function postIssues(Request $request, Guard $auth)
0 ignored issues
show
Unused Code introduced by
The parameter $auth is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
66
    {
67
        try {
68
            $issue = $this->dispatch(new AddIssueCommand(
69
                $request->get('title'),
70
                $request->get('description'),
71
                $request->get('author_id'),
72
                $request->get('project_id')
73
            ));
74
        } catch (QueryException $e) {
75
            throw new BadRequestHttpException();
76
        }
77
78
        return $this->item($issue);
79
    }
80
81
    /**
82
     * Update an existing issue.
83
     *
84
     * @param \Gitamin\Models\Inicdent $issue
85
     *
86
     * @return \Illuminate\Http\JsonResponse
87
     */
88 View Code Duplication
    public function putIssue(Request $request, Issue $issue)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
    {
90
        try {
91
            $issue = $this->dispatch(new UpdateIssueCommand(
92
                $issue,
93
                $request->get('title'),
94
                $request->get('description'),
95
                $request->get('author_id'),
96
                $request->get('project_id')
97
            ));
98
        } catch (QueryException $e) {
99
            throw new BadRequestHttpException();
100
        }
101
102
        return $this->item($issue);
103
    }
104
105
    /**
106
     * Delete an existing issue.
107
     *
108
     * @param \Gitamin\Models\Issue $issue
109
     *
110
     * @return \Illuminate\Http\JsonResponse
111
     */
112
    public function deleteIssue(Issue $issue)
113
    {
114
        $this->dispatch(new RemoveIssueCommand($issue));
115
116
        return $this->noContent();
117
    }
118
}
119