Completed
Push — master ( 380473...b1e8a8 )
by Renato
02:46
created

IssueController::edit()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 2
eloc 18
nc 1
nop 1
1
<?php
2
/**
3
 * GitScrum v0.1.
4
 *
5
 * @author  Renato Marinho <[email protected]>
6
 * @license http://opensource.org/licenses/GPL-3.0 GPLv3
7
 */
8
9
namespace GitScrum\Http\Controllers;
10
11
use Illuminate\Http\Request;
12
use GitScrum\Http\Requests\IssueRequest;
13
use GitScrum\Models\Sprint;
14
use GitScrum\Models\UserStory;
15
use GitScrum\Models\Issue;
16
use GitScrum\Models\Organization;
17
use GitScrum\Models\IssueType;
18
use GitScrum\Models\ConfigStatus;
19
use GitScrum\Models\ConfigIssueEffort;
20
use Carbon\Carbon;
21
use Auth;
22
23
class IssueController extends Controller
24
{
25
    /**
26
     * Display a listing of the resource.
27
     *
28
     * @return \Illuminate\Http\Response
29
     */
30
    public function index($slug)
31
    {
32
        if ($slug) {
33
            $sprint = Sprint::where('slug', $slug)
34
                ->with('issues.user')
35
                ->with('issues.users')
36
                ->with('issues.commits')
37
                ->with('issues.statuses')
38
                ->with('issues.status')
39
                ->with('issues.comments')
40
                ->with('issues.attachments')
41
                ->with('issues.type')
42
                ->first();
43
44
            $issues = $sprint->issues;
45
        } else {
46
            $sprint = null;
47
            $issues = Auth::user()->issues;
48
        }
49
50
        $issues = $issues->sortBy('position')->groupBy('config_status_id');
51
52
        $configStatus = configStatus::where('type', 'issue')
53
            ->orderby('position', 'ASC')->get();
54
55
        if (!is_null($sprint) && !count($sprint)) {
56
            return redirect()->route('sprints.index');
57
        }
58
59
        return view('issues.index')
0 ignored issues
show
Bug introduced by
The method with does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

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...
60
            ->with('sprint', $sprint)
61
            ->with('issues', $issues)
62
            ->with('configStatus', $configStatus);
63
    }
64
65
    /**
66
     * Show the form for creating a new resource.
67
     *
68
     * @return \Illuminate\Http\Response
69
     */
70
    public function create($slug_sprint = null, $slug_user_story = null, $parent_id = null)
71
    {
72
        $issue_types = IssueType::where('enabled', 1)
73
            ->orderby('position', 'ASC')
74
            ->get();
75
76
        $issue_efforts = ConfigIssueEffort::where('enabled', 1)
77
            ->orderby('position', 'ASC')
78
            ->get();
79
80
        $userStory = $productBacklogs = null;
81
82
        if ((is_null($slug_sprint) || !$slug_sprint) && $slug_user_story) {
83
            $userStory = UserStory::where('slug', $slug_user_story)->first();
84
            $productBacklogs = Auth::user()->productBacklogs($userStory->product_backlog_id);
85
            $usersByOrganization = Organization::find($userStory->productBacklog->organization_id)->users;
86
        } else if ( $slug_sprint ) {
87
            $usersByOrganization = Organization::find(Sprint::where('slug', $slug_sprint)->first()
88
                ->productBacklog->organization_id)->users;
89
        } else {
90
            $issue = Issue::find($parent_id);
91
            $productBacklogs = $issue->product_backlog_id;
92
            $usersByOrganization = Organization::find($issue->productBacklog->organization_id)->users;
93
        }
94
95
        return view('issues.create')
0 ignored issues
show
Bug introduced by
The method with does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

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...
96
            ->with('productBacklogs', $productBacklogs)
97
            ->with('userStory', $userStory)
98
            ->with('slug', $slug_sprint)
99
            ->with('parent_id', $parent_id)
100
            ->with('issue_types', $issue_types)
101
            ->with('issue_efforts', $issue_efforts)
102
            ->with('usersByOrganization', $usersByOrganization)
103
            ->with('action', 'Create');
104
    }
105
106
    /**
107
     * Store a newly created resource in storage.
108
     *
109
     * @param \Illuminate\Http\Request $request
110
     *
111
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\RedirectResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
112
     */
113
    public function store(IssueRequest $request)
114
    {
115
        $issue = Issue::create($request->all());
116
117
        if (is_array($request->members)) {
0 ignored issues
show
Documentation introduced by
The property members does not exist on object<GitScrum\Http\Requests\IssueRequest>. Since you implemented __get, maybe consider adding a @property annotation.

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.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

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.

Loading history...
118
            $issue->users()->sync($request->members);
0 ignored issues
show
Documentation introduced by
The property members does not exist on object<GitScrum\Http\Requests\IssueRequest>. Since you implemented __get, maybe consider adding a @property annotation.

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.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

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.

Loading history...
119
        }
120
121
        return redirect()->route('issues.show', ['slug' => $issue->slug])
0 ignored issues
show
Documentation introduced by
The property slug does not exist on object<GitScrum\Models\Issue>. Since you implemented __get, maybe consider adding a @property annotation.

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.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

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.

Loading history...
122
            ->with('success', trans('Congratulations! The Issue has been created with successfully'));
123
    }
124
125
    /**
126
     * Display the specified resource.
127
     *
128
     * @param int $id
0 ignored issues
show
Bug introduced by
There is no parameter named $id. 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...
129
     *
130
     * @return \Illuminate\Http\Response
131
     */
132
    public function show($slug)
133
    {
134
        $issue = Issue::where('slug', '=', $slug)
135
            ->with('sprint')
136
            ->with('type')
137
            ->with('configEffort')
138
            ->with('labels')
139
            ->first();
140
141
        $usersByOrganization = Organization::find($issue->productBacklog->organization_id)->users;
142
143
        $configStatus = configStatus::where('type', 'issue')
144
            ->orderby('position', 'ASC')->get();
145
146
        return view('issues.show')
0 ignored issues
show
Bug introduced by
The method with does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

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...
147
            ->with('issue', $issue)
148
            ->with('usersByOrganization', $usersByOrganization)
149
            ->with('configStatus', $configStatus);
150
    }
151
152
    /**
153
     * Show the form for editing the specified resource.
154
     *
155
     * @param int $id
0 ignored issues
show
Bug introduced by
There is no parameter named $id. 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...
156
     *
157
     * @return \Illuminate\Http\Response
158
     */
159
    public function edit($slug)
160
    {
161
        $issue = Issue::where('slug', '=', $slug)->first();
162
163
        $issue_types = IssueType::where('enabled', 1)
164
            ->orderby('position', 'ASC')
165
            ->get();
166
167
        $issue_efforts = ConfigIssueEffort::where('enabled', 1)
168
            ->orderby('position', 'ASC')
169
            ->get();
170
171
        $usersByOrganization = Organization::find($issue->productBacklog->organization_id)->users;
172
173
        return view('issues.edit')
0 ignored issues
show
Bug introduced by
The method with does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

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...
174
            ->with('productBacklogs', $issue->productBacklog->id)
175
            ->with('userStory', $issue->userStory)
176
            ->with('slug', isset($issue->sprint->slug) ? $issue->sprint->slug : null)
177
            ->with('issue_types', $issue_types)
178
            ->with('issue_efforts', $issue_efforts)
179
            ->with('usersByOrganization', $usersByOrganization)
180
            ->with('issue', $issue)
181
            ->with('action', 'Edit');
182
    }
183
184
    /**
185
     * Update the specified resource in storage.
186
     *
187
     * @param \Illuminate\Http\Request $request
188
     * @param int                      $id
0 ignored issues
show
Bug introduced by
There is no parameter named $id. 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...
189
     *
190
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\RedirectResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
191
     */
192
    public function update(IssueRequest $request, $slug)
193
    {
194
        $issue = Issue::where('slug', '=', $slug)->first();
195
        $issue->update($request->all());
196
197
        if (is_array($request->members)) {
0 ignored issues
show
Documentation introduced by
The property members does not exist on object<GitScrum\Http\Requests\IssueRequest>. Since you implemented __get, maybe consider adding a @property annotation.

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.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

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.

Loading history...
198
            $issue->users()->sync($request->members);
0 ignored issues
show
Documentation introduced by
The property members does not exist on object<GitScrum\Http\Requests\IssueRequest>. Since you implemented __get, maybe consider adding a @property annotation.

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.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

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.

Loading history...
199
        }
200
201
        return back()
202
            ->with('success', trans('Congratulations! The Issue has been edited with successfully'));
203
    }
204
205
    public function statusUpdate(Request $request, $slug = null, int $status = 0)
206
    {
207
        if (!isset($request->status_id)) {
208
            $request->status_id = $status;
209
        }
210
        $status = ConfigStatus::find($request->status_id);
211
        $save = function ($issue, $position = null) use ($request, $status) {
212
            $issue->config_status_id = $request->status_id;
213
            $issue->closed_user_id = null;
214
            $issue->closed_at = null;
215
216
            if (!is_null($status->is_closed)) {
217
                $issue->closed_user_id = Auth::id();
218
                $issue->closed_at = Carbon::now();
219
            }
220
221
            if ($position) {
222
                $issue->position = $position;
223
            }
224
225
            return $issue->save();
226
        };
227
228
        if ($request->ajax()) {
229
            $position = 1;
230
            try {
231
                foreach (json_decode($request->json) as $id) {
232
                    $issue = Issue::find($id);
233
                    $save($issue, $position);
234
                    ++$position;
235
                }
236
237
                return response()->json([
238
                    'success' => true,
239
                ]);
240
            } catch (\Exception $e) {
241
                return response()->json([
242
                    'success' => false,
243
                ]);
244
            }
245
        } else {
246
            $issue = Issue::where('slug', $slug)
247
                ->firstOrFail();
248
            $save($issue);
249
250
            return back()->with('success', trans('Updated successfully'));
251
        }
252
    }
253
254
    /**
255
     * Remove the specified resource from storage.
256
     *
257
     * @param int $id
0 ignored issues
show
Bug introduced by
There is no parameter named $id. 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...
258
     *
259
     * @return \Illuminate\Http\Response
0 ignored issues
show
Documentation introduced by
Should the return type not be \Illuminate\Http\RedirectResponse?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
260
     */
261
    public function destroy($slug)
262
    {
263
        $issue = Issue::where('slug', $slug)->firstOrFail();
264
265
        if (isset($issue->userStory)) {
266
            $redirect = redirect()->route('user_stories.show', ['slug' => $issue->userStory->slug]);
267
        } else {
268
            $redirect = redirect()->route('sprints.show', ['slug' => $issue->sprint->slug]);
269
        }
270
271
        $issue->delete();
272
273
        return $redirect;
274
    }
275
}
276