Show::handle()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 0
dl 0
loc 36
rs 9.344
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\Contracts\Role as RoleContract;
8
use Spatie\Permission\Contracts\Permission as PermissionContract;
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
        $permissionClass = app(PermissionContract::class);
21
        $roleClass = app(RoleContract::class);
22
23
        $style = $this->argument('style') ?? 'default';
24
        $guard = $this->argument('guard');
25
26
        if ($guard) {
27
            $guards = Collection::make([$guard]);
28
        } else {
29
            $guards = $permissionClass::pluck('guard_name')->merge($roleClass::pluck('guard_name'))->unique();
30
        }
31
32
        foreach ($guards as $guard) {
33
            $this->info("Guard: $guard");
34
35
            $roles = $roleClass::whereGuardName($guard)->orderBy('name')->get()->mapWithKeys(function ($role) {
36
                return [$role->name => $role->permissions->pluck('name')];
37
            });
38
39
            $permissions = $permissionClass::whereGuardName($guard)->orderBy('name')->pluck('name');
40
41
            $body = $permissions->map(function ($permission) use ($roles) {
42
                return $roles->map(function (Collection $role_permissions) use ($permission) {
43
                    return $role_permissions->contains($permission) ? ' ✔' : ' ·';
44
                })->prepend($permission);
45
            });
46
47
            $this->table(
48
                $roles->keys()->prepend('')->toArray(),
49
                $body->toArray(),
50
                $style
0 ignored issues
show
Bug introduced by
It seems like $style defined by $this->argument('style') ?? 'default' on line 23 can also be of type array; however, Illuminate\Console\Conce...nteractsWithIO::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...
51
            );
52
        }
53
    }
54
}
55