Completed
Pull Request — master (#1384)
by Chris
01:30
created

Guard::getConfigAuthGuards()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 1
nop 1
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Permission;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Database\Eloquent\Model;
7
8
class Guard
9
{
10
    /**
11
     * Return a collection of guard names suitable for the $model.
12
     *
13
     * @param string|Model $model model class object or name
14
     * @return Collection
15
     */
16
    public static function getNames($model): Collection
17
    {
18
        if (is_object($model)) {
19
            if (\method_exists($model, 'guardName')) {
20
                $guardName = $model->guardName();
21
            } else {
22
                $guardName = $model->guard_name ?? null;
23
            }
24
        }
25
26
        if (! isset($guardName)) {
27
            $class = is_object($model) ? get_class($model) : $model;
28
29
            $guardName = (new \ReflectionClass($class))->getDefaultProperties()['guard_name'] ?? null;
30
        }
31
32
        if ($guardName) {
33
            return collect($guardName);
34
        }
35
36
        return self::getConfigAuthGuards($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...
37
    }
38
39
    /**
40
     * Get list of relevant guards for the $class model based on config(auth) settings.
41
     *
42
     * Lookup flow:
43
     * - get names of models for guards defined in auth.guards where a provider is set
44
     * - filter for provider models matching the model $class being checked
45
     * - keys() gives just the names of the matched guards
46
     * - if logged in, filter out any guards for which the user does not pass the auth->guard->check() test
47
     * - return collection of guard names
48
     *
49
     * @param string $class
50
     * @return Collection
51
     */
52
    protected static function getConfigAuthGuards(string $class): Collection
53
    {
54
        return collect(config('auth.guards'))
55
            ->map(function ($guard) {
56
                if (! isset($guard['provider'])) {
57
                    return;
58
                }
59
60
                return config("auth.providers.{$guard['provider']}.model");
61
            })
62
            ->filter(function ($model) use ($class) {
63
                return $class === $model;
64
            })
65
            ->keys()
66
            ->filter(function ($guard) {
67
                return app('request')->user() ? app('auth')->guard($guard)->check() : true;
68
            });
69
    }
70
71
    /**
72
     * Lookup a guard name relevant for the $class model and the current user.
73
     *
74
     * @param string|Model $class model class object or name
75
     * @return string guard name
76
     */
77
    public static function getDefaultName($class): string
78
    {
79
        $default = config('auth.defaults.guard');
80
81
        return static::getNames($class)->first() ?: $default;
82
    }
83
}
84