Completed
Pull Request — 5.4 (#4)
by
unknown
03:23
created

RoleHasRelations::parent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace SlFomin\Roles\Traits;
4
5
use Illuminate\Database\Eloquent\Collection;
6
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
7
use Ultraware\Roles\Models\Permission;
8
9
trait RoleHasRelations
10
{
11
    /**
12
     * Role belongs to many permissions.
13
     *
14
     * @return BelongsToMany
15
     */
16
    public function permissions()
17
    {
18
        return $this->belongsToMany(config('roles.models.permission'))->withTimestamps();
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
     * Role belongs to many users.
23
     *
24
     * @return BelongsToMany
25
     */
26
    public function users()
27
    {
28
        return $this->belongsToMany(config('auth.providers.users.model'))->withTimestamps();
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...
29
    }
30
31
    /**
32
     * Role belongs to parent role.
33
     *
34
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
35
     */
36
    public function parent()
37
    {
38
        return $this->belongsTo(config('roles.models.role'),'parent_id');
0 ignored issues
show
Bug introduced by
It seems like belongsTo() 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...
39
    }
40
41
    public function ancestors()
42
    {
43
        $ancestors = $this->where('id', '=', $this->parent_id)->get();
0 ignored issues
show
Bug introduced by
The property parent_id 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...
Bug introduced by
It seems like where() 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...
44
        while ($ancestors->last() && $ancestors->last()->parent_id !== null)
45
        {
46
            $parent = $this->where('id', '=', $ancestors->last()->parent_id)->get();
0 ignored issues
show
Bug introduced by
It seems like where() 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...
47
            $ancestors = $ancestors->merge($parent);
48
        }
49
        return $ancestors;
50
    }
51
52
    /**
53
     * Role has many children roles
54
     *
55
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
56
     */
57
    public function children()
58
    {
59
        return $this->hasMany(config('roles.models.role'),'parent_id');
0 ignored issues
show
Bug introduced by
It seems like hasMany() 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...
60
    }
61
62
    public function descendants()
63
    {
64
        $descendants = $this->where('parent_id', '=', $this->id)->get();
0 ignored issues
show
Bug introduced by
The property id 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...
Bug introduced by
It seems like where() 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...
65
        foreach($descendants as $descendant)
66
            $descendants = $descendants->merge($descendant->descendants());
67
        return $descendants;
68
    }
69
70
    /**
71
     * Attach permission to a role.
72
     *
73
     * @param int|Permission $permission
74
     * @return int|bool
75
     */
76
    public function attachPermission($permission)
77
    {
78
        return (!$this->permissions()->get()->contains($permission)) ? $this->permissions()->attach($permission) : true;
79
    }
80
81
    /**
82
     * Detach permission from a role.
83
     *
84
     * @param int|Permission $permission
85
     * @return int
86
     */
87
    public function detachPermission($permission)
88
    {
89
        return $this->permissions()->detach($permission);
90
    }
91
92
    /**
93
     * Detach all permissions.
94
     *
95
     * @return int
96
     */
97
    public function detachAllPermissions()
98
    {
99
        return $this->permissions()->detach();
100
    }
101
102
    /**
103
     * Sync permissions for a role.
104
     *
105
     * @param array|Permission[]|Collection $permissions
106
     * @return array
107
     */
108
    public function syncPermissions($permissions)
109
    {
110
        return $this->permissions()->sync($permissions);
111
    }
112
}
113