Completed
Push — master ( 3dec05...c1872b )
by wen
03:33
created

RoleController::save()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 9.0534
cc 4
eloc 12
nc 3
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::paginate();
27
        return response()->json($roles);
28
    }
29
30
    public function getPermissionList()
31
    {
32
        $perms = (new Permission())->getPermRouteList();
33
        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...
34
    }
35
36
    public function save(RoleRequest $request)
37
    {
38
        if (empty($request->input('id'))) {
39
            $model = new Role();
40
        } else {
41
            $model = Role::findOrFail($request->input('id'));
42
            if ($model->name == 'admin' && !Auth::user()->hasRole($model->name)) {
43
                return response()->json(['error' => 'Unauthenticated.'], 401);
44
            }
45
        }
46
47
        //$model->name         = $request->input('name');
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

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