Completed
Push — master ( f01446...e52d08 )
by Freek
02:09
created

HasPermissions::givePermissionTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 15
rs 9.4285
cc 1
eloc 9
nc 1
nop 1
1
<?php
2
3
namespace Spatie\Permission\Traits;
4
5
use Spatie\Permission\Contracts\Permission;
6
7
trait HasPermissions
8
{
9
    /**
10
     * Grant the given permission(s) to a role.
11
     *
12
     * @param string|array|Permission|\Illuminate\Support\Collection $permissions
13
     *
14
     * @return HasPermissions
0 ignored issues
show
Comprehensibility Bug introduced by
The return type HasPermissions is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?

In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.

If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.

Loading history...
15
     */
16
    public function givePermissionTo(...$permissions)
17
    {
18
        collect($permissions)
19
            ->flatten()
20
            ->map(function ($permission) {
21
                return $this->getStoredPermission($permission);
22
            })
23
            ->each(function (Permission $permission) {
24
                return $this->permissions()->save($permission);
0 ignored issues
show
Bug introduced by
The method permissions() does not exist on Spatie\Permission\Traits\HasPermissions. Did you maybe mean syncPermissions()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
25
            });
26
27
        $this->forgetCachedPermissions();
0 ignored issues
show
Bug introduced by
It seems like forgetCachedPermissions() 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...
28
29
        return $this;
30
    }
31
32
    /**
33
     * Remove all current permissions and set the given ones.
34
     *
35
     * @param array ...$permissions
36
     */
37
    public function syncPermissions(...$permissions)
38
    {
39
        $this->permissions()->detach();
0 ignored issues
show
Bug introduced by
The method permissions() does not exist on Spatie\Permission\Traits\HasPermissions. Did you maybe mean syncPermissions()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
40
41
        collect($permissions)
42
            ->flatten()
43
            ->map(function ($permission) {
44
                return $this->getStoredPermission($permission);
45
            })->each(function (Permission $permission) {
46
                $this->permissions()->save($permission);
0 ignored issues
show
Bug introduced by
The method permissions() does not exist on Spatie\Permission\Traits\HasPermissions. Did you maybe mean syncPermissions()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
47
            });
48
    }
49
50
    /**
51
     * Revoke the given permission.
52
     *
53
     * @param $permission
54
     *
55
     * @return HasPermissions
0 ignored issues
show
Comprehensibility Bug introduced by
The return type HasPermissions is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?

In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.

If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.

Loading history...
56
     */
57
    public function revokePermissionTo($permission)
58
    {
59
        $this->permissions()->detach($this->getStoredPermission($permission));
0 ignored issues
show
Bug introduced by
The method permissions() does not exist on Spatie\Permission\Traits\HasPermissions. Did you maybe mean syncPermissions()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
60
61
        $this->forgetCachedPermissions();
0 ignored issues
show
Bug introduced by
It seems like forgetCachedPermissions() 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...
62
63
        return $this;
64
    }
65
66
    /**
67
     * @param string|array|Permission|\Illuminate\Support\Collection $permissions
68
     *
69
     * @return Permission
70
     */
71
    protected function getStoredPermission($permissions)
72
    {
73
        if (is_string($permissions)) {
74
            return app(Permission::class)->findByName($permissions);
75
        }
76
77
        if (is_array($permissions)) {
78
            return app(Permission::class)->whereIn('name', $permissions)->get();
79
        }
80
81
        return $permissions;
82
    }
83
}
84