HasRole   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 0
loc 122
ccs 35
cts 35
cp 1
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A delete() 0 6 1
A getDeleteableAttribute() 0 7 2
A getRoles() 0 4 1
A getRoleIds() 0 10 2
A getMergedPermissions() 0 10 2
A getPermissions() 0 4 1
A isSuperUser() 0 4 1
A isAdmin() 0 4 1
A getAccessLevels() 0 11 3
A roles() 0 4 1
A getLowestAccessLevel() 0 4 1
1
<?php
2
3
namespace Napp\Core\Acl\Role;
4
5
use Illuminate\Database\Eloquent\Collection;
6
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
7
8
/**
9
 * Trait HasRole.
10
 */
11
trait HasRole
12
{
13
    /**
14
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
15
     */
16 94
    public function roles(): BelongsToMany
17
    {
18 94
        return $this->belongsToMany(config('acl.models.role'), config('acl.table_names.users_roles'));
0 ignored issues
show
Bug introduced by
It seems like belongsToMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
19
    }
20
21
    /**
22
     * @throws \Exception
23
     *
24
     * @return bool
25
     */
26 2
    public function delete()
27
    {
28 2
        $this->roles()->detach();
29
30 2
        return parent::delete();
31
    }
32
33
    /**
34
     * @return bool
35
     */
36 2
    public function getDeleteableAttribute(): bool
37
    {
38 2
        $user = auth()->user();
0 ignored issues
show
Bug introduced by
The method user does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

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...
39
40 2
        return $user->getAuthIdentifier() != $this->attributes['id']
0 ignored issues
show
Bug introduced by
The property attributes does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
41 2
            && $user->roles[0]->access_level <= $this->roles[0]->access_level;
0 ignored issues
show
Bug introduced by
The property roles does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
42
    }
43
44
    /**
45
     * @return Collection
46
     */
47 56
    public function getRoles()
48
    {
49 56
        return $this->roles;
50
    }
51
52
    /**
53
     * @return array
54
     */
55 46
    public function getRoleIds(): array
56
    {
57 46
        $ids = [];
58
59 46
        foreach ($this->getRoles() as $role) {
60 46
            $ids[] = $role->id;
61
        }
62
63 46
        return $ids;
64
    }
65
66
    /**
67
     * Returns an array of merged permissions for each group the user is in.
68
     *
69
     * @return array
70
     */
71 34
    public function getMergedPermissions(): array
72
    {
73 34
        $permissions = [];
74 34
        foreach ($this->getRoles() as $role) {
75 34
            $permissions = \array_merge($permissions, $role->getPermissions() ?? []);
76
        }
77
78
        // merge with users own permissions
79 34
        return \array_merge($permissions, $this->getPermissions());
80
    }
81
82
    /**
83
     * @return array
84
     */
85 34
    public function getPermissions(): array
86
    {
87 34
        return [];
88
    }
89
90
    /**
91
     * @return bool
92
     */
93 44
    public function isSuperUser(): bool
94
    {
95 44
        return \in_array(1, $this->getRoleIds(), true);
96
    }
97
98
    /**
99
     * @return bool
100
     */
101 46
    public function isAdmin(): bool
102
    {
103 46
        return \in_array(2, $this->getRoleIds(), true);
104
    }
105
106
    /**
107
     * @return int Lowest value of the users groups access levels
108
     */
109 2
    public function getAccessLevels(): int
110
    {
111 2
        $accessLevel = PHP_INT_MAX;
112
113 2
        foreach ($this->getRoles() as $role) {
114 2
            $role->access_level_parent = (null === $role->access_level_parent) ? PHP_INT_MAX : $role->access_level_parent;
115 2
            $accessLevel = $this->getLowestAccessLevel($role->access_level, $role->access_level_parent, $accessLevel);
116
        }
117
118 2
        return $accessLevel;
119
    }
120
121
    /**
122
     * @param int $level
123
     * @param int $parent
124
     * @param int $current
125
     *
126
     * @return int
127
     */
128 2
    protected function getLowestAccessLevel($level, $parent, $current): int
129
    {
130 2
        return min([$level, $parent, $current]);
131
    }
132
}
133