Completed
Push — master ( 0cef4e...0f713b )
by Chris
12s
created

Show::handle()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 0
dl 0
loc 33
rs 9.392
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\Role;
8
use Spatie\Permission\Models\Permission;
9
10
class Show extends Command
11
{
12
    protected $signature = 'permission:show
13
            {guard? : The name of the guard}
14
            {style? : The display style (default|borderless|compact|box)}';
15
16
    protected $description = 'Show a table of roles and permissions per guard';
17
18
    public function handle()
19
    {
20
        $style = $this->argument('style') ?? 'default';
21
        $guard = $this->argument('guard');
22
23
        if ($guard) {
24
            $guards = Collection::make([$guard]);
25
        } else {
26
            $guards = Permission::pluck('guard_name')->merge(Role::pluck('guard_name'))->unique();
27
        }
28
29
        foreach ($guards as $guard) {
30
            $this->info("Guard: $guard");
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
            $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...
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(
45
                $roles->keys()->prepend('')->toArray(),
46
                $body->toArray(),
47
                $style
0 ignored issues
show
Bug introduced by
It seems like $style defined by $this->argument('style') ?? 'default' on line 20 can also be of type array; however, Illuminate\Console\Command::table() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
48
            );
49
        }
50
    }
51
}
52