UserPolicy   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 129
Duplicated Lines 13.95 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 18
loc 129
ccs 0
cts 28
cp 0
rs 10
wmc 14

9 Methods

Rating   Name   Duplication   Size   Complexity  
A store() 0 3 1
A index() 0 3 1
A create() 0 3 1
A show() 0 6 2
A edit() 8 8 2
A destroy() 0 8 2
A updateRole() 0 8 2
A restore() 0 3 1
A update() 8 8 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace App\Policies;
4
5
use App\User;
6
use Illuminate\Auth\Access\HandlesAuthorization;
7
8
class UserPolicy
9
{
10
    use HandlesAuthorization;
11
    
12
    /**
13
     * Determine whether the user can view the list of users.
14
     *
15
     * @param  \App\User  $user
16
     * @return mixed
17
     */
18
    public function index(User $user)
19
    {
20
        return $user->isAdmin();
21
    }
22
    
23
    /**
24
     * Determine whether the user can view the create users page.
25
     *
26
     * @param  \App\User  $user
27
     * @return boolean
28
     */
29
    public function create(User $user)
30
    {
31
        return $user->isAdmin();
32
    }
33
    
34
    /**
35
     * Determine whether the user can create a user.
36
     *
37
     * @param  \App\User  $user
38
     * @return boolean
39
     */
40
    public function store(User $user)
41
    {
42
        return $user->isAdmin();
43
    }
44
45
    /**
46
     * Determine whether the user can view the user.
47
     *
48
     * @param  \App\User  $user the current user
49
     * @param  \App\User  $user2 the user to be viewed
50
     * @return boolean
51
     */
52
    public function show(User $user, User $user2)
53
    {
54
        if ($user->isAdmin())
55
            return true;
56
        else
57
            return $user->id === $user2->id;
58
    }
59
    
60
    /**
61
     * Determine whether the user can view the edit page for a user.
62
     *
63
     * @param  \App\User  $user
64
     * @param  \App\User  $user2 the user to be edited
65
     * @return mixed
66
     */
67 View Code Duplication
    public function edit(User $user, User $user2)
68
    {
69
        if ($user->role > $user2->role)
70
        {
71
            return $user->isAdmin();
72
        }
73
        else
74
            return $user->id === $user2->id;
75
    }
76
77
    /**
78
     * Determine whether the user can update themselves or another user.
79
     *
80
     * @param  \App\User  $user
81
     * @param  \App\User  $user2
82
     * @return boolean
83
     */
84 View Code Duplication
    public function update(User $user, User $user2)
85
    {
86
        if ($user->role > $user2->role)
87
        {
88
            return $user->isAdmin();
89
        }
90
        else
91
            return $user->id === $user2->id;
92
    }
93
    
94
    /**
95
     * Determine whether the user can update another user's role.
96
     *
97
     * @param  \App\User  $user
98
     * @param  \App\User  $user2
99
     * @return boolean
100
     */
101
    public function updateRole(User $user, $user2)
102
    {
103
        if ($user->role > $user2->role)
104
        {
105
            return $user->isAdmin();
106
        }
107
        else
108
            return false;
109
    }
110
    
111
    /**
112
     * Determine whether the user can delete the user.
113
     *
114
     * @param  \App\User  $user
115
     * @param  \App\User  $user2
116
     * @return boolean
117
     */
118
    public function destroy(User $user, $user2)
119
    {
120
        if ($user->role > $user2->role)
121
        {
122
            return $user->isAdmin();
123
        }
124
        else
125
            return false;
126
    }
127
    
128
    /**
129
     * Determine whether the user can restore the user.
130
     *
131
     * @param  \App\User  $user
132
     * @return mixed
133
     */
134
    public function restore(User $user)
135
    {
136
        return $user->isAdmin();
137
    }
138
}
139