Completed
Pull Request — master (#1061)
by
unknown
01:39
created

Table::handle()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 0
dl 0
loc 30
rs 9.44
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Permission\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Collection;
7
use Spatie\Permission\Models\Permission;
8
use Spatie\Permission\Models\Role;
9
10
class Table extends Command
11
{
12
    protected $signature = 'permission:table
13
            {guard? : The name of the guard}';
14
15
    protected $description = 'Show a table of roles and permissions per guard';
16
17
    public function handle()
18
    {
19
        $guard = $this->argument('guard');
20
21
        if($guard){
22
            $guards = Collection::make([$guard]);
23
        } else {
24
            $guards = Permission::pluck('guard_name')->merge(Role::pluck('guard_name'))->unique();
25
        }
26
27
        foreach ($guards as $guard) {
28
            $this->info("Guard: $guard");
29
30
            $permissions = Permission::whereGuardName($guard)->orderBy('name')->pluck('name');
0 ignored issues
show
Bug introduced by
The method whereGuardName() does not exist on Spatie\Permission\Models\Permission. Did you maybe mean reguard()?

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...
31
32
            $roles = Role::whereGuardName($guard)->orderBy('name')->get()->mapWithKeys(function (Role $role) {
0 ignored issues
show
Bug introduced by
The method whereGuardName() does not exist on Spatie\Permission\Models\Role. Did you maybe mean reguard()?

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...
33
                return [$role->name => $role->permissions->pluck('name')];
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<Spatie\Permission\Models\Role>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property permissions does not exist on object<Spatie\Permission\Models\Role>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
34
            });
35
36
            $header = $roles->keys()->prepend('');
37
38
            $body = $permissions->map(function ($permission) use ($roles) {
39
                return $roles->map(function (Collection $role_permissions) use ($permission) {
40
                    return $role_permissions->contains($permission) ? ' ✔' : ' ·';
41
                })->prepend($permission);
42
            });
43
44
            $this->table($header->toArray(), $body->toArray());
45
        }
46
    }
47
}
48