Completed
Push — master ( 023448...f16a65 )
by wen
04:07
created

RoleController::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
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\RoleRequest;
12
use Sco\Admin\Models\Permission;
13
use Sco\Admin\Models\Role;
14
15
class RoleController extends Controller
16
{
17
    public function get($id)
18
    {
19
        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...
20
            return $query->select('id');
21
        }])->findOrFail($id);
22
    }
23
24
    public function getList()
25
    {
26
        $roles = Role::with([
0 ignored issues
show
Bug introduced by
The method paginate 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...
27
            'perms' => function ($query) {
28
                $query->select('id');
29
            },
30
        ])->paginate();
31
        return response()->json($roles);
32
    }
33
34
    public function getPermissionList()
35
    {
36
        $perms = (new Permission())->getPermRouteList();
37
        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...
38
    }
39
40
    public function save(RoleRequest $request)
41
    {
42
        if (empty($request->input('id'))) {
43
            $model = new Role();
44
        } else {
45
            $model = Role::findOrFail($request->input('id'));
46
            if ($model->id == 1 && $model->id != Auth::id()) {
47
                return response()->json(['error' => 'Unauthenticated.'], 401);
48
            }
49
        }
50
51
52
        $model->name         = $request->input('name');
53
        $model->display_name = $request->input('display_name');
54
55
        DB::transaction(function () use ($model, $request) {
56
            $model->save();
57
            $model->savePermissions($request->input('perms'));
58
        });
59
60
        return response()->json(['message' => 'ok']);
61
    }
62
63
    public function delete($id)
64
    {
65
        if ($id == 1) {
66
            throw new AdminHttpException('超级管理员角色不能删除');
67
        }
68
69
        $role = Role::findOrFail($id);
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 batchDelete(Request $request)
83
    {
84
        if (!is_array($request->input('ids'))) {
85
            throw new AdminHttpException('参数错误');
86
        }
87
88
        DB::transaction(function () use ($request) {
89
            Role::destroy($request->input('ids'));
90
        });
91
92
        return response()->json(['message' => 'ok']);
93
    }
94
}
95