Completed
Push — master ( 24a4dc...fe3ba7 )
by Ricardo
04:03
created

LabelsController::updIssueLabels()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 9.0008
c 0
b 0
f 0
cc 5
nc 5
nop 3
1
<?php
2
3
namespace Fabrica\Http\Api;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\Event;
7
8
use Fabrica\Events\IssueEvent;
9
use Fabrica\Http\Requests;
10
use Fabrica\Http\Api\Controller;
11
use Fabrica\Customization\Eloquent\State;
12
use Fabrica\Project\Eloquent\Labels;
13
use Fabrica\Project\Eloquent\Board;
14
use Fabrica\Project\Provider;
15
use DB;
16
17
class LabelsController extends Controller
18
{
19
    public function __construct()
20
    {
21
        $this->middleware('privilege:manage_project');
22
        parent::__construct();
23
    }
24
25
    /**
26
     * Display a listing of the resource.
27
     *
28
     * @return \Illuminate\Http\Response
29
     */
30
    public function index(Request $request, $project_key)
31
    {
32
        $labels = Labels::where([ 'project_key' => $project_key ])->orderBy('_id', 'asc')->get();
33
        foreach ($labels as $key => $label)
34
        {
35
            $label->is_used = $this->isFieldUsedByIssue($project_key, 'labels', $label->toArray());
36
37
            $completed_issue_cnt = $incompleted_issue_cnt = 0;
0 ignored issues
show
Unused Code introduced by
$incompleted_issue_cnt is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
Unused Code introduced by
$completed_issue_cnt is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
38
            $unresolved_cnt = DB::collection('issue_' . $project_key)
39
                ->where('resolution', 'Unresolved')
40
                ->where('labels', $label['name'])
41
                ->where('del_flg', '<>', 1)
42
                ->count();
43
            $label->unresolved_cnt = $unresolved_cnt;
44
45
            $all_cnt = DB::collection('issue_' . $project_key)
46
                ->where('labels', $label['name'])
47
                ->where('del_flg', '<>', 1)
48
                ->count();
49
            $label->all_cnt = $all_cnt;
50
        }
51
            
52
        return response()->json([ 'ecode' => 0, 'data' => $labels ]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
53
    }
54
55
    /**
56
     * Store a newly created resource in storage.
57
     *
58
     * @param  \Illuminate\Http\Request $request
59
     * @return \Illuminate\Http\Response
60
     */
61 View Code Duplication
    public function store(Request $request, $project_key)
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...
62
    {
63
        $name = $request->input('name');
64
        if (!$name) {
65
            throw new \UnexpectedValueException('the name can not be empty.', -16100);
66
        }
67
68
        if (Provider::isLabelExisted($project_key, $name)) {
69
            throw new \UnexpectedValueException('label name cannot be repeated', -16102);
70
        }
71
72
        $label = Labels::create([ 'project_key' => $project_key ] + $request->all());
73
        return response()->json(['ecode' => 0, 'data' => $label]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
74
    }
75
76
    /**
77
     * Update the specified resource in storage.
78
     *
79
     * @param  \Illuminate\Http\Request $request
80
     * @param  int                      $id
81
     * @return \Illuminate\Http\Response
82
     */
83 View Code Duplication
    public function update(Request $request, $project_key, $id)
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...
84
    {
85
        $name = $request->input('name');
86
        if (isset($name)) {
87
            if (!$name) {
88
                throw new \UnexpectedValueException('the name can not be empty.', -16100);
89
            }
90
        }
91
92
        $label = Labels::find($id);
93
        if (!$label || $project_key != $label->project_key) {
94
            throw new \UnexpectedValueException('the label does not exist or is not in the project.', -16103);
95
        }
96
97
        if ($label->name !== $name && Provider::isLabelExisted($project_key, $name)) {
98
            throw new \UnexpectedValueException('label name cannot be repeated', -16102);
99
        }
100
101
        if ($label->name !== $name) {
102
            $this->updIssueLabels($project_key, $label->name, $name);
103
        }
104
105
        $label->fill($request->except(['project_key']))->save();
106
107
        return response()->json(['ecode' => 0, 'data' => Labels::find($id)]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
108
    }
109
110
    /**
111
     * Remove the specified resource from storage.
112
     *
113
     * @param  \Illuminate\Http\Request $request
114
     * @param  int                      $id
115
     * @return \Illuminate\Http\Response
116
     */
117 View Code Duplication
    public function delete(Request $request, $project_key, $id)
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...
118
    {
119
        $label = Labels::find($id);
120
        if (!$label || $project_key != $label->project_key) {
121
            throw new \UnexpectedValueException('the label does not exist or is not in the project.', -16103);
122
        }
123
124
        $operate_flg = $request->input('operate_flg');
125
        if (!isset($operate_flg) || $operate_flg === '0') {
126
            $is_used = $this->isFieldUsedByIssue($project_key, 'labels', $label->toArray());
127
            if ($is_used) {
128
                throw new \UnexpectedValueException('the label has been used by some issues.', -16104);
129
            }
130
        }
131
        else if ($operate_flg === '1') {
132
            $swap_label = $request->input('swap_label');
133
            if (!isset($swap_label) || !$swap_label) {
134
                throw new \UnexpectedValueException('the swap label cannot be empty.', -16106);
135
            }
136
137
            $slabel = Labels::find($swap_label);
138
            if (!$slabel || $project_key != $slabel->project_key) {
139
                throw new \UnexpectedValueException('the swap label does not exist or is not in the project.', -16107);
140
            }
141
142
            $this->updIssueLabels($project_key, $label->name, $slabel->name);
143
        }
144
        else if ($operate_flg === '2') {
145
            $this->updIssueLabels($project_key, $label->name, '');
146
        }
147
        else
148
        {
149
            throw new \UnexpectedValueException('the operation has error.', -16105);
150
        }
151
152
        Labels::destroy($id);
153
154
        return response()->json(['ecode' => 0, 'data' => [ 'id' => $id ]]);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
155
156
        //if ($operate_flg === '1')
157
        //{
158
        //    return $this->show($project_key, $request->input('swap_label'));
159
        //}
160
        //else
161
        //{
162
        //    return response()->json(['ecode' => 0, 'data' => [ 'id' => $id ]]);
163
        //}
164
    }
165
166
    /**
167
     * update the issues label
168
     *
169
     * @param  array  $issues
0 ignored issues
show
Bug introduced by
There is no parameter named $issues. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
170
     * @param  string $source
171
     * @param  string $dest
172
     * @return \Illuminate\Http\Response
173
     */
174
    public function updIssueLabels($project_key, $source, $dest)
175
    {
176
        $issues = DB::collection('issue_' . $project_key)
177
            ->where('labels', $source)
178
            ->where('del_flg', '<>', 1)
179
            ->get();
180
181
        foreach ($issues as $issue)
182
        {
183
            $updValues = [];
184
185
            $newLabels = [];
186
            foreach ($issue['labels'] as $label)
187
            {
188
                if ($source == $label) {
189
                    if ($dest) {
190
                        $newLabels[] = $dest;
191
                    }
192
                } 
193
                else 
194
                {
195
                    $newLabels[] = $label;
196
                }
197
            }
198
            $updValues['labels'] = array_values(array_unique(array_filter($newLabels)));
199
200
            $updValues['modifier'] = [ 'id' => $this->user->id, 'name' => $this->user->first_name, 'email' => $this->user->email ];
201
            $updValues['updated_at'] = time();
202
203
            $issue_id = $issue['_id']->__toString();
204
205
            DB::collection('issue_' . $project_key)->where('_id', $issue_id)->update($updValues);
206
            // add to histroy table
207
            $snap_id = Provider::snap2His($project_key, $issue_id, [], [ 'labels' ]);
208
            // trigger event of issue edited
209
            Event::fire(new IssueEvent($project_key, $issue_id, $updValues['modifier'], [ 'event_key' => 'edit_issue', 'snap_id' => $snap_id ]));
210
        }
211
    }
212
}
213