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

Guard::getDefaultName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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