Completed
Push — master ( 1281a7...176cc8 )
by wen
03:59
created

UserController::destroy()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 10
loc 10
rs 9.4285
c 1
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\Auth\AuthenticationException;
9
use Illuminate\Routing\Controller;
10
use Sco\Admin\Exceptions\AdminHttpException;
11
use Sco\Admin\Http\Requests\StoreUserRequest;
12
use Sco\Admin\Http\Requests\UpdateUserRequest;
13
use Sco\Admin\Models\Role;
14
15
class UserController extends Controller
16
{
17
18
    /**
19
     * @return \Illuminate\Foundation\Auth\User
20
     */
21
    private function getUserModel()
22
    {
23
        $userModelName = config('admin.user');
24
        return new $userModelName();
25
    }
26
27
    public function getList()
28
    {
29
        $users = $this->getUserModel()->with('roles')->paginate();
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...
30
        return response()->json($users);
31
    }
32
33
    public function store(StoreUserRequest $request)
34
    {
35
        $model = $this->getUserModel();
36
        $data = $request->only(['name', 'email', 'password']);
37
        $data['password'] = bcrypt($data['password']);
38
        $model->fill($data);
39
        DB::transaction(function () use ($model, $request) {
40
            $model->save();
41
            $model->roles()->sync($request->input('roles'));
42
        });
43
        return response()->json(['message' => 'ok']);
44
    }
45
46
    public function update(UpdateUserRequest $request)
47
    {
48
        $user = $this->getUserModel()->findOrFail($request->input('id'));
49
        if ($user->id == 1 && Auth::id() != $user->id) {
50
            throw new AuthenticationException();
51
        }
52
        $data = $request->only(['name', 'email']);
53
        if (!empty($request->input('password'))) {
54
            $data['password'] = bcrypt($request->input('password'));
0 ignored issues
show
Bug introduced by
It seems like $request->input('password') targeting Illuminate\Http\Concerns...ractsWithInput::input() can also be of type array; however, bcrypt() does only seem to accept string, 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...
55
        }
56
        $user->fill($data);
57
        DB::transaction(function () use ($user, $request) {
58
            $user->save();
59
            $user->roles()->sync($request->input('roles'));
60
        });
61
        return response()->json(['message' => 'ok']);
62
    }
63
64 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...
65
    {
66
        if ($id == 1) {
67
            throw new AdminHttpException('超级管理员不能删除');
68
        }
69
70
        $model = $this->getUserModel()->findOrFail($id);
71
        $model->delete();
72
        return response()->json(['message' => 'ok']);
73
    }
74
75
    public function getAllRole()
76
    {
77
        $roles = Role::all();
78
        return response()->json($roles);
0 ignored issues
show
Bug introduced by
It seems like $roles defined by \Sco\Admin\Models\Role::all() on line 77 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...
79
    }
80
}
81