Completed
Pull Request — master (#134)
by
unknown
05:19
created

UserPolicy   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 113
Duplicated Lines 18.58 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 21
loc 113
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0
wmc 15

9 Methods

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

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 View Code Duplication
    public function show(User $user, User $user2)
53
    {
54
        if ($user->isAdmin())
55
            return true;
56
        else
57
            return $user->isUser() && $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->isAdmin())
70
            return true;
71
        else
72
            return $user->isUser() && $user->id === $user2->id;
73
    }
74
75
    /**
76
     * Determine whether the user can update user2.
77
     *
78
     * @param  \App\User  $user
79
     * @param  \App\User  $user2
80
     * @return boolean
81
     */
82 View Code Duplication
    public function update(User $user, User $user2)
83
    {
84
        if ($user->isAdmin())
85
            return true;
86
        else
87
            return $user->isUser() && $user->id === $user2->id;
88
    }
89
    
90
    /**
91
     * Determine whether the user can update a user's role.
92
     *
93
     * @param  \App\User  $user
94
     * @return boolean
95
     */
96
    public function updateRole(User $user)
97
    {
98
        return $user->isAdmin();
99
    }
100
101
    /**
102
     * Determine whether the user can delete the user.
103
     *
104
     * @param  \App\User  $user
105
     * @return boolean
106
     */
107
    public function destroy(User $user)
108
    {
109
        return $user->isAdmin();
110
    }
111
    
112
    /**
113
     * Determine whether the user can restore the user.
114
     *
115
     * @param  \App\User  $user
116
     * @return mixed
117
     */
118
    public function restore(User $user)
119
    {
120
        return $user->isAdmin();
121
    }
122
}
123