Completed
Pull Request — master (#138)
by
unknown
02:51
created

IssueController::removeFromSprint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
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::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::type('issue')->get();
53
54
        if (!is_null($sprint) && !count($sprint)) {
55
            return redirect()->route('sprints.index');
56
        }
57
58
        //get all stories from this product with an open issue
59
        //@TODO make sure stories are managable by user
60
        $userStoriesIds = UserStory::select(['user_stories.id', 'user_stories.config_priority_id'])
61
            ->where('user_stories.product_backlog_id', $sprint->product_backlog_id)
62
            ->join('issues', function ($join) {
63
                $join->on('user_stories.id', '=', 'issues.user_story_id')
64
                    ->where('issues.config_status_id', 1);
65
            })
66
            ->groupBy(['user_stories.id', 'user_stories.config_priority_id'])
67
            ->orderBy('user_stories.config_priority_id')
68
            ->orderBy('user_stories.id')
69
            ->get();
70
        $openUserStories = [];
71
        foreach ($userStoriesIds as $oneUserStoryId) {
72
            $openUserStory = UserStory::find($oneUserStoryId->id);
73
            $openUserStory->load(['issues' => function($query) {
74
                    $query->where('config_status_id',1)
75
                    ->whereNull('sprint_id');
76
            }, 'issues.type']);
77
            if ($openUserStory->issues->count() > 0) {
78
                $openUserStory->load('priority');
79
                $openUserStories[] = $openUserStory;
80
            }
81
        }
82
83
        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...
84
            ->with('sprint', $sprint)
85
            ->with('issues', $issues)
86
            ->with('configStatus', $configStatus)
87
            ->with('openUserStories', $openUserStories);
88
    }
89
90
    public function create($slug_sprint = null, $slug_user_story = null, $parent_id = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
91
    {
92
        $issue_types = IssueType::where('enabled', 1)
93
            ->orderby('position', 'ASC')
94
            ->get();
95
96
        $issue_efforts = ConfigIssueEffort::where('enabled', 1)
97
            ->orderby('position', 'ASC')
98
            ->get();
99
100
        $userStory = $productBacklogs = null;
101
102
        if ((is_null($slug_sprint) || !$slug_sprint) && $slug_user_story) {
103
            $userStory = UserStory::slug($slug_user_story)->first();
104
            $productBacklogs = Auth::user()->productBacklogs($userStory->product_backlog_id);
105
            $usersByOrganization = Organization::find($userStory->productBacklog->organization_id)->users;
106
        } elseif ($slug_sprint) {
107
            $usersByOrganization = Organization::find(Sprint::slug($slug_sprint)->first()
108
                ->productBacklog->organization_id)->users;
109
        } else {
110
            $issue = Issue::find($parent_id);
111
            $productBacklogs = $issue->product_backlog_id;
112
            $usersByOrganization = Organization::find($issue->productBacklog->organization_id)->users;
113
        }
114
115
        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...
116
            ->with('productBacklogs', $productBacklogs)
117
            ->with('userStory', $userStory)
118
            ->with('slug', $slug_sprint)
119
            ->with('parent_id', $parent_id)
120
            ->with('issue_types', $issue_types)
121
            ->with('issue_efforts', $issue_efforts)
122
            ->with('usersByOrganization', $usersByOrganization)
123
            ->with('action', 'Create');
124
    }
125
126
    public function store(IssueRequest $request)
127
    {
128
        $issue = Issue::create($request->all());
129
130
        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...
131
            $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...
132
        }
133
134
        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...
135
            ->with('success', trans('Congratulations! The Issue has been created with successfully'));
136
    }
137
138
    public function show($slug)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
139
    {
140
        $issue = Issue::slug($slug)
141
            ->with('sprint')
142
            ->with('type')
143
            ->with('configEffort')
144
            ->with('labels')
145
            ->first();
146
147
        $usersByOrganization = Organization::find($issue->productBacklog->organization_id)->users;
148
149
        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...
150
            ->with('issue', $issue)
151
            ->with('usersByOrganization', $usersByOrganization);
152
    }
153
154
    public function edit($slug)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
155
    {
156
        $issue = Issue::slug($slug)->first();
157
158
        $issue_types = IssueType::where('enabled', 1)
159
            ->orderby('position', 'ASC')
160
            ->get();
161
162
        $issue_efforts = ConfigIssueEffort::where('enabled', 1)
163
            ->orderby('position', 'ASC')
164
            ->get();
165
166
        $usersByOrganization = Organization::find($issue->productBacklog->organization_id)->users;
167
168
        $productBacklogs = Auth::user()->productBacklogs($issue->productBacklog->id, false);
169
170
        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...
171
            ->with('productBacklogs', $productBacklogs)
172
            ->with('userStory', $issue->userStory)
173
            ->with('slug', isset($issue->sprint->slug) ? $issue->sprint->slug : null)
174
            ->with('issue_types', $issue_types)
175
            ->with('issue_efforts', $issue_efforts)
176
            ->with('usersByOrganization', $usersByOrganization)
177
            ->with('issue', $issue)
178
            ->with('action', 'Edit');
179
    }
180
181
    public function update(IssueRequest $request, $slug)
182
    {
183
        $issue = Issue::slug($slug)->first();
184
185
        $issue->update($request->all());
186
187
        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...
188
            $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...
189
        }
190
191
        return back()
192
            ->with('success', trans('Congratulations! The Issue has been edited with successfully'));
193
    }
194
195
    public function statusUpdate(Request $request, $slug = null, int $status = 0)
196
    {
197
198
        if (!isset($request->status_id)) {
199
            $request->status_id = $status;
200
        }
201
        $status = ConfigStatus::find($request->status_id);
202
203
        if ($request->ajax()) {
204
            $position = 1;
205
206
            try {
207
                foreach (json_decode($request->json) as $id) {
208
                    $issue = Issue::find($id);
209
                    if (empty($issue->sprint_id) && !empty($request->sprint_id)) {
210
                        $issue->assignToSprint($request->sprint_id);
211
                    }
212
                    $updateSuccess = $issue->updateStatusAndPosition($request->status_id, $status, $position);
213
                    ++$position;
214
                }
215
216
                return response()->json([
217
                    'success' => $updateSuccess,
0 ignored issues
show
Bug introduced by
The variable $updateSuccess does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
218
                ]);
219
            } catch (\Exception $e) {
220
                return response()->json([
221
                    'success' => false,
222
                ]);
223
            }
224
        } else {
225
            $issue = Issue::slug($slug)->firstOrFail();
226
            $issue->updateStatusAndPosition($request->status_id, $status);
227
228
            return back()->with('success', trans('Updated successfully'));
229
        }
230
    }
231
232
    public function destroy(Request $request)
233
    {
234
        $issue = Issue::slug($request->slug)->firstOrFail();
235
236
        if (isset($issue->userStory)) {
237
            $redirect = redirect()->route('user_stories.show', ['slug' => $issue->userStory->slug]);
238
        } else {
239
            $redirect = redirect()->route('sprints.show', ['slug' => $issue->sprint->slug]);
240
        }
241
242
        $issue->delete();
243
244
        return $redirect;
245
    }
246
247
    public function removeFromSprint($slug)
248
    {
249
        $issue = Issue::slug($slug)->firstOrFail();
250
        $sprintSlug = $issue->sprint->slug;
251
        $issue->removeFromSprint();
252
        return redirect()->route('issues.index', ['slug' => $sprintSlug]);
253
    }
254
}
255