Completed
Push — master ( 80411a...1281a7 )
by wen
03:35
created

RoleController::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
4
namespace Sco\Admin\Http\Controllers\Users;
5
6
use Auth;
7
use DB;
8
use Illuminate\Http\Request;
9
use Illuminate\Routing\Controller;
10
use Sco\Admin\Exceptions\AdminHttpException;
11
use Sco\Admin\Http\Requests\StoreRoleRequest;
12
use Sco\Admin\Http\Requests\UpdateRoleRequest;
13
use Sco\Admin\Models\Permission;
14
use Sco\Admin\Models\Role;
15
16
class RoleController extends Controller
17
{
18
    public function get($id)
19
    {
20
        return Role::with(['perms' => function ($query) {
0 ignored issues
show
Bug introduced by
The method findOrFail does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

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...
21
            return $query->select('id');
22
        }])->findOrFail($id);
23
    }
24
25
    public function getList()
26
    {
27
        $roles = Role::paginate();
28
        return response()->json($roles);
29
    }
30
31
    public function getPermissionList()
32
    {
33
        $perms = (new Permission())->getPermRouteList();
34
        return response()->json($perms, 200, [], JSON_FORCE_OBJECT);
0 ignored issues
show
Documentation introduced by
$perms is of type object<Illuminate\Support\Collection>|null, but the function expects a string|array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
35
    }
36
37
    public function store(StoreRoleRequest $request)
38
    {
39
        $role = new Role();
40
        $role->fill($request->input());
0 ignored issues
show
Bug introduced by
It seems like $request->input() targeting Illuminate\Http\Concerns...ractsWithInput::input() can also be of type string; however, Illuminate\Database\Eloquent\Model::fill() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
41
        DB::transaction(function () use ($role, $request) {
42
            $role->save();
43
            $role->savePermissions($request->input('perms'));
44
        });
45
        return response()->json(['message' => 'ok']);
46
    }
47
48
    public function update(UpdateRoleRequest $request)
49
    {
50
        $role = Role::findOrFail($request->input('id'));
51
        if ($role->name == 'admin' && !Auth::user()->hasRole($role->name)) {
52
            return response()->json(['error' => 'Unauthenticated.'], 401);
53
        }
54
55
        $role->fill($request->input());
56
        DB::transaction(function () use ($role, $request) {
57
            $role->save();
58
            $role->savePermissions($request->input('perms'));
59
        });
60
        return response()->json(['message' => 'ok']);
61
    }
62
63 View Code Duplication
    public function destroy($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...
64
    {
65
        $role = Role::findOrFail($id);
66
        if ($role->name == 'admin') {
67
            throw new AdminHttpException('超级管理员角色不能删除');
68
        }
69
70
        $role->delete();
71
72
        return response()->json(['message' => 'ok']);
73
    }
74
75
    /**
76
     * 批量删除角色
77
     *
78
     * @param \Illuminate\Http\Request $request
79
     *
80
     * @return \Illuminate\Http\JsonResponse
81
     */
82
    public function batchDestroy(Request $request)
83
    {
84
        if (!is_array($request->input('ids'))) {
85
            throw new AdminHttpException('参数错误');
86
        }
87
88
        DB::transaction(function () use ($request) {
89
            foreach ($request->input('ids') as $id) {
0 ignored issues
show
Bug introduced by
The expression $request->input('ids') of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
90
                $role = Role::findOrFail($id);
91
                if ($role->name == 'admin') {
92
                    throw new AdminHttpException('超级管理员角色不能删除');
93
                }
94
95
                $role->delete();
96
            }
97
        });
98
99
        return response()->json(['message' => 'ok']);
100
    }
101
}
102