RoleController::index()   B
last analyzed

Complexity

Conditions 8
Paths 7

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.2114
c 0
b 0
f 0
cc 8
nc 7
nop 1
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\AddUserToRoleEvent;
9
use Fabrica\Events\DelUserFromRoleEvent;
10
use Fabrica\Events\AddGroupToRoleEvent;
11
use Fabrica\Events\DelGroupFromRoleEvent;
12
13
use Fabrica\Project\Eloquent\Project;
14
use Fabrica\Project\Provider;
15
use Fabrica\Http\Requests;
16
use Fabrica\Http\Api\Controller;
17
use Fabrica\Acl\Eloquent\Group;
18
use Fabrica\Acl\Eloquent\Role;
19
use Fabrica\Acl\Eloquent\RolePermissions;
20
use Fabrica\Acl\Eloquent\Roleactor;
21
use Fabrica\Acl\Acl;
22
23
use Cartalyst\Sentinel\Users\EloquentUser;
24
25
class RoleController extends Controller
26
{
27
    /**
28
     * Display a listing of the resource.
29
     *
30
     * @return \Illuminate\Http\Response
31
     */
32
    public function index($project_key)
33
    {
34
        $roles = Provider::getRoleList($project_key)->toArray();
35
        foreach ($roles as $key => $role)
36
        {
37
            if ($project_key === '$_sys_$') {
38
                $actor = Roleactor::where('role_id', $role['_id'])->first();
39
                if ($actor && ($actor->user_ids || $actor->group_ids)) {
40
                    $roles[$key]['is_used'] = true;
41
                }
42
            }
43
            else 
44
            {
45
                $user_groups = $this->getGroupsAndUsers($project_key, $role['_id']);
46
                $roles[$key]['users'] = $user_groups['users'];
47
                $roles[$key]['groups'] = $user_groups['groups'];
48
49
                if (isset($role['user_ids'])) {
50
                    unset($roles[$key]['user_ids']);
51
                }
52
                if (isset($role['group_ids'])) {
53
                    unset($roles[$key]['group_ids']);
54
                }
55
            }
56
57
            $roles[$key]['permissions'] = $this->getPermissions($project_key, $role['_id']);
58
        }
59
        return response()->json([ 'ecode' => 0, 'data' => $roles ]);
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...
60
    }
61
62
    /**
63
     * Store a newly created resource in storage.
64
     *
65
     * @param  \Illuminate\Http\Request $request
66
     * @return \Illuminate\Http\Response
67
     */
68
    public function store(Request $request, $project_key)
69
    {
70
        $name = $request->input('name');
71
        if (!$name) {
72
            throw new \UnexpectedValueException('the name can not be empty.', -12700);
73
        }
74
75
        $permissions = $request->input('permissions');
76 View Code Duplication
        if (isset($permissions)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
77
            $allPermissions = Acl::getAllPermissions();
78
            if (array_diff($permissions, $allPermissions)) {
79
                throw new \UnexpectedValueException('the illegal permission.', -12701);
80
            }
81
        }
82
83
        $role = Role::create($request->all() + [ 'project_key' => $project_key ]);
84
85
        if (isset($permissions) && $role) {
86
            RolePermissions::create([ 'project_key' => $project_key, 'role_id' => $role->id, 'permissions' => $permissions ]);
87
            $role->permissions = $permissions;
88
        }
89
90
        return response()->json([ 'ecode' => 0, 'data' => $role ]);
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...
91
    }
92
93
    /**
94
     * Display the specified resource.
95
     *
96
     * @param  int $id
97
     * @return \Illuminate\Http\Response
98
     */
99
    public function show($project_key, $id)
100
    {
101
        $role = Role::find($id);
102
        return response()->json([ 'ecode' => 0, 'data' => $role ]);
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...
103
    }
104
105
    /**
106
     * set the role actor.
107
     *
108
     * @param  \Illuminate\Http\Request $request
109
     * @param  int                      $id
110
     * @return \Illuminate\Http\Response
111
     */
112 View Code Duplication
    public function setActor(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...
113
    {
114
        $new_user_ids = $request->input('users');
115
        if (isset($new_user_ids)) {
116
            $actor = Roleactor::where([ 'project_key' => $project_key, 'role_id' => $id ])->first();
117
            $old_user_ids = $actor && $actor->user_ids ? $actor->user_ids : [];
118
119
            $this->setUsers($project_key, $id, $new_user_ids ?: []);
120
121
            $add_user_ids = array_diff($new_user_ids, $old_user_ids);
122
            $del_user_ids = array_diff($old_user_ids, $new_user_ids);
123
124
            Event::fire(new AddUserToRoleEvent($add_user_ids, $project_key));
125
            Event::fire(new DelUserFromRoleEvent($del_user_ids, $project_key));
126
        }
127
128
        $data = Role::find($id);
129
        $user_groups = $this->getGroupsAndUsers($project_key, $id);
130
        $data->users = $user_groups['users'];
131
        $data->groups = $user_groups['groups'];
132
133
        return response()->json([ 'ecode' => 0, 'data' => $data ]);
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...
134
    }
135
136
    /**
137
     * set the role group actor.
138
     *
139
     * @param  \Illuminate\Http\Request $request
140
     * @param  int                      $id
141
     * @return \Illuminate\Http\Response
142
     */
143 View Code Duplication
    public function setGroupActor(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...
144
    {
145
        $new_group_ids = $request->input('groups');
146
        if (isset($new_group_ids)) {
147
            $actor = Roleactor::where([ 'project_key' => $project_key, 'role_id' => $id ])->first();
148
            $old_group_ids = $actor && $actor->group_ids ? $actor->group_ids : [];
149
150
            $this->setGroups($project_key, $id, $new_group_ids ?: []);
151
152
            $add_group_ids = array_diff($new_group_ids, $old_group_ids);
153
            $del_group_ids = array_diff($old_group_ids, $new_group_ids);
154
155
            Event::fire(new AddGroupToRoleEvent($add_group_ids, $project_key));
156
            Event::fire(new DelGroupFromRoleEvent($del_group_ids, $project_key));
157
        }
158
159
        $data = Role::find($id);
160
        $user_groups = $this->getGroupsAndUsers($project_key, $id);
161
        $data->users = $user_groups['users'];
162
        $data->groups = $user_groups['groups'];
163
164
        return response()->json([ 'ecode' => 0, 'data' => $data ]);
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...
165
    }
166
167
    /**
168
     * Update the specified resource in storage.
169
     *
170
     * @param  \Illuminate\Http\Request $request
171
     * @param  int                      $id
172
     * @return \Illuminate\Http\Response
173
     */
174
    public function update(Request $request, $project_key, $id)
175
    {
176
        $name = $request->input('name');
177
        if (isset($name)) {
178
            if (!$name) {
179
                throw new \UnexpectedValueException('the name can not be empty.', -12700);
180
            }
181
        }
182
        $role = Role::find($id);
183
        if (!$role || $project_key != $role->project_key) {
184
            throw new \UnexpectedValueException('the role does not exist or is not in the project.', -12702);
185
        }
186
187
        $permissions = $request->input('permissions');
188 View Code Duplication
        if (isset($permissions)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
189
            $allPermissions = Acl::getAllPermissions();
190
            if (array_diff($permissions, $allPermissions)) {
191
                throw new \UnexpectedValueException('the illegal permission.', -12701);
192
            }
193
            $role->permissions = $permissions;
194
        }
195
        $role->fill($request->except(['project_key']))->save();
196
197
        $data = Role::find($id);
198
        return response()->json([ 'ecode' => 0, 'data' => $data ]);
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...
199
    }
200
201
    /**
202
     * Remove the specified resource from storage.
203
     *
204
     * @param  int $id
205
     * @return \Illuminate\Http\Response
206
     */
207
    public function destroy($project_key, $id)
208
    {
209
        $role = Role::find($id);
210
        if (!$role || $project_key != $role->project_key) {
211
            throw new \UnexpectedValueException('the role does not exist or is not in the project.', -12702);
212
        }
213
214
        if ($project_key === '$_sys_$') {
215
            $actors = Roleactor::where('role_id', $role->id)->get();
216
            foreach($actors as $actor)
217
            {
218
                if ($actor->user_ids || $actor->group_ids) {
219
                    throw new \UnexpectedValueException('the role has been used in some projects.', -12703);
220
                }
221
            }
222
            foreach($actors as $actor)
223
            {
224
                $actor->delete();
225
            }
226
        }
227
        else
228
        {
229
            $actor = Roleactor::where([ 'project_key' => $project_key, 'role_id' => $id ])->first();
230
            if ($actor) {
231
                $user_ids = isset($actor->user_ids) ? $actor->user_ids : []; 
232
                $user_ids && Event::fire(new DelUserFromRoleEvent($user_ids, $project_key));
233
                $group_ids = isset($actor->group_ids) ? $actor->group_ids : [];
234
                $group_ids && Event::fire(new DelGroupFromRoleEvent($group_ids, $project_key));
235
                $actor->delete();
236
            }
237
        }
238
        Role::destroy($id);
239
240
        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...
241
    }
242
243
    /**
244
     * set users.
245
     *
246
     * @param  string $project_key
247
     * @param  array  $uids
248
     * @return array
249
     */
250 View Code Duplication
    public function setUsers($project_key, $role_id, $uids)
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...
251
    {
252
        $actor = Roleactor::where([ 'project_key' => $project_key, 'role_id' => $role_id ])->first();
253
        $actor && $actor->delete();
254
        
255
        Roleactor::create([ 'role_id' => $role_id, 'project_key' => $project_key, 'user_ids' => $uids, 'group_ids' => isset($actor->group_ids) ? $actor->group_ids : [] ]);
256
    }
257
258
    /**
259
     * set groups.
260
     *
261
     * @param  string $project_key
262
     * @param  array  $gids
263
     * @return array
264
     */
265 View Code Duplication
    public function setGroups($project_key, $role_id, $gids)
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...
266
    {
267
        $actor = Roleactor::where([ 'project_key' => $project_key, 'role_id' => $role_id ])->first();
268
        $actor && $actor->delete();
269
270
        Roleactor::create([ 'role_id' => $role_id, 'project_key' => $project_key, 'group_ids' => $gids, 'user_ids' => isset($actor->user_ids) ? $actor->user_ids : [] ]);
271
    }
272
273
    /**
274
     * get users and Groups by role id.
275
     *
276
     * @param  string $project_key
277
     * @param  string $role_id
278
     * @return array 
279
     */
280
    public function getGroupsAndUsers($project_key, $role_id)
281
    {
282
        $actor = Roleactor::where([ 'project_key' => $project_key, 'role_id' => $role_id ])->first();
283
        if (!$actor) { return [ 'users' => [], 'groups' => [] ]; 
284
        }
285
286
        $new_users = [];
287
        if (isset($actor->user_ids) && $actor->user_ids) {
288
            $users = EloquentUser::find($actor->user_ids);
289
            foreach ($users as $user)
290
            {
291
                $new_users[] = [ 'id' => $user->id, 'name' => $user->first_name, 'email' => $user->email, 'nameAndEmail' => $user->first_name . '('. $user->email . ')' ];
292
            }
293
        }
294
295
        $new_groups = [];
296
        if (isset($actor->group_ids) && $actor->group_ids) {
297
            $new_groups = Group::find($actor->group_ids)->toArray();
298
        }
299
300
        return [ 'users' => $new_users, 'groups' => $new_groups ];
301
    }
302
303
    /**
304
     * set permissions 
305
     *
306
     * @param  string $project_key
307
     * @param  string $role_id
0 ignored issues
show
Bug introduced by
There is no parameter named $role_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...
308
     * @return array
309
     */
310
    public function setPermissions(Request $request, $project_key, $id)
311
    {
312
        $role = Role::find($id);
313 View Code Duplication
        if (!$role || ($role->project_key != '$_sys_$' && $project_key != $role->project_key)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
314
            throw new \UnexpectedValueException('the role does not exist or is not in the project.', -12702);
315
        }
316
317
        $permissions = $request->input('permissions');
318
        if (isset($permissions)) {
319
            $allPermissions = Acl::getAllPermissions();
320
            if (array_diff($permissions, $allPermissions)) {
321
                throw new \UnexpectedValueException('the illegal permission.', -12701);
322
            }
323
324
            $rp = RolePermissions::where([ 'project_key' => $project_key, 'role_id' => $id ])->first();
325
            $rp && $rp->delete();
326
327
            RolePermissions::create([ 'project_key' => $project_key, 'role_id' => $id, 'permissions' => $permissions ]);
328
        }
329
330
        $role->permissions = $this->getPermissions($project_key, $id);
331
        return response()->json(['ecode' => 0, 'data' => $role]);
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...
332
    }
333
334
    /**
335
     * get permissions by project_key and roleid.
336
     *
337
     * @param  string $project_key
338
     * @param  string $role_id
339
     * @return array
340
     */
341 View Code Duplication
    public function getPermissions($project_key, $role_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...
342
    {
343
        $rp = RolePermissions::where([ 'project_key' => $project_key, 'role_id' => $role_id ])->first();
344
        if (!$rp && $project_key !== '$_sys_$') {
345
            $rp = RolePermissions::where([ 'project_key' => '$_sys_$', 'role_id' => $role_id ])->first();
346
        }
347
        return $rp && isset($rp->permissions) ? $rp->permissions : [];
348
    }
349
350
    /**
351
     * reset the role permissions.
352
     *
353
     * @param  string $project_key
354
     * @param  string $role_id
355
     * @return \Illuminate\Http\Response
356
     */
357 View Code Duplication
    public function reset($project_key, $role_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...
358
    {
359
        $rp = RolePermissions::where([ 'project_key' => $project_key, 'role_id' => $role_id ])->first();
360
        $rp && $rp->delete();
361
362
        $role = Role::find($role_id)->toArray();
363
364
        $rp = RolePermissions::where([ 'project_key' => '$_sys_$', 'role_id' => $role_id ])->first();
365
        $role['permissions'] = $rp && isset($rp->permissions) ? $rp->permissions : [];
366
367
        return response()->json(['ecode' => 0, 'data' => $role]);
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...
368
    }
369
370
    /**
371
     * view the application in the all projects.
372
     *
373
     * @return \Illuminate\Http\Response
374
     */
375
    public function viewUsedInProject($project_key, $id)
376
    {
377
        if ($project_key !== '$_sys_$') {
378
            return response()->json(['ecode' => 0, 'data' => [] ]);
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...
379
        }
380
381
        $res = [];
382
        $projects = Project::all();
383
        foreach($projects as $project)
384
        {
385
            $roleactor = Roleactor::where('role_id', $id)
386
                ->where('project_key', '<>', '$_sys_$')
387
                ->where('project_key', $project->key)
388
                ->first();
389
390
            if ($roleactor && ($roleactor->user_ids || $roleactor->group_ids)) {
391
                $res[] = [ 'key' => $project->key, 'name' => $project->name, 'status' => $project->status ];
392
            }
393
        }
394
395
        return response()->json(['ecode' => 0, 'data' => $res ]);
396
    }
397
}
398