Completed
Push — master ( e8056d...9a7672 )
by wen
04:02
created

RoleController::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
4
namespace Sco\Admin\Http\Controllers\Manager;
5
6
use DB;
7
use Illuminate\Http\Request;
8
use Illuminate\Routing\Controller;
9
use Sco\Admin\Exceptions\AdminHttpException;
10
use Sco\Admin\Http\Requests\RoleRequest;
11
use Sco\Admin\Models\Permission;
12
use Sco\Admin\Models\Role;
13
14
class RoleController extends Controller
15
{
16
    public function get($id)
17
    {
18
        return Role::findOrFail($id);
19
    }
20
21
    public function getList()
22
    {
23
        $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...
24
            'perms' => function ($query) {
25
                $query->select('id');
26
            },
27
        ])->paginate();
28
        return response()->json($roles);
29
    }
30
31
    public function getAll()
32
    {
33
        $roles = Role::all();
34
        return response()->json($roles);
0 ignored issues
show
Bug introduced by
It seems like $roles defined by \Sco\Admin\Models\Role::all() on line 33 can also be of type object<Illuminate\Database\Eloquent\Collection>; however, Illuminate\Contracts\Rou...ResponseFactory::json() does only seem to accept string|array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
35
    }
36
37
    public function getPermissionList()
38
    {
39
        $perms = (new Permission())->getPermRouteList();
40
        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...
41
    }
42
43
    public function save(RoleRequest $request)
44
    {
45 View Code Duplication
        if (empty($request->input('id'))) {
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...
46
            $model = new Role();
47
        } else {
48
            $model = Role::findOrFail($request->input('id'));
49
        }
50
51
        $model->name         = $request->input('name');
52
        $model->display_name = $request->input('display_name');
53
54
        DB::transaction(function () use ($model, $request) {
55
            $model->save();
56
            $model->savePermissions($request->input('perms'));
57
        });
58
59
        return response()->json(['message' => 'ok']);
60
    }
61
62 View Code Duplication
    public function delete($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...
63
    {
64
        if ($id == 1) {
65
            throw new AdminHttpException('超级管理员角色不能删除');
66
        }
67
68
        $role = Role::findOrFail($id);
69
        $role->delete();
70
71
        return response()->json(['message' => 'ok']);
72
    }
73
74
    /**
75
     * 批量删除角色
76
     *
77
     * @param \Illuminate\Http\Request $request
78
     *
79
     * @return \Illuminate\Http\JsonResponse
80
     */
81
    public function batchDelete(Request $request)
82
    {
83
        if (!is_array($request->input('ids'))) {
84
            throw new AdminHttpException('参数错误');
85
        }
86
87
        DB::transaction(function () use ($request) {
88
            Role::destroy($request->input('ids'));
89
        });
90
91
        return response()->json(['message' => 'ok']);
92
    }
93
}
94