Guard::getNames()   B
last analyzed

Complexity

Conditions 7
Paths 18

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 18
nop 1
dl 0
loc 33
rs 8.4586
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Permission;
4
5
use Illuminate\Support\Collection;
6
7
class Guard
8
{
9
    /**
10
     * return collection of (guard_name) property if exist on class or object
11
     * otherwise will return collection of guards names that exists in config/auth.php.
12
     * @param $model
13
     * @return Collection
14
     */
15
    public static function getNames($model): Collection
16
    {
17
        if (is_object($model)) {
18
            if (\method_exists($model, 'guardName')) {
19
                $guardName = $model->guardName();
20
            } else {
21
                $guardName = $model->guard_name ?? null;
22
            }
23
        }
24
25
        if (! isset($guardName)) {
26
            $class = is_object($model) ? get_class($model) : $model;
27
28
            $guardName = (new \ReflectionClass($class))->getDefaultProperties()['guard_name'] ?? null;
29
        }
30
31
        if ($guardName) {
32
            return collect($guardName);
33
        }
34
35
        return collect(config('auth.guards'))
36
            ->map(function ($guard) {
37
                if (! isset($guard['provider'])) {
38
                    return;
39
                }
40
41
                return config("auth.providers.{$guard['provider']}.model");
42
            })
43
            ->filter(function ($model) use ($class) {
0 ignored issues
show
Bug introduced by
The variable $class does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
44
                return $class === $model;
45
            })
46
            ->keys();
47
    }
48
49
    public static function getDefaultName($class): string
50
    {
51
        $default = config('auth.defaults.guard');
52
53
        return static::getNames($class)->first() ?: $default;
54
    }
55
}
56