Completed
Pull Request — master (#1516)
by
unknown
14:00
created

helpers.php ➔ currentAuthGuard()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 0
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
if (! function_exists('getModelForGuard')) {
4
    /**
5
     * @param string $guard
6
     *
7
     * @return string|null
8
     */
9
    function getModelForGuard(string $guard)
10
    {
11
        return collect(config('auth.guards'))
12
            ->map(function ($guard) {
13
                if (! isset($guard['provider'])) {
14
                    return;
15
                }
16
17
                return config("auth.providers.{$guard['provider']}.model");
18
            })->get($guard);
19
    }
20
}
21
22
if (! function_exists('currentAuthGuard')) {
23
    /**
24
     *
25
     * @return string|null
26
     */
27
    function currentAuthGuard()
28
    {
29
        if(! config('permission.enable_dynamic_auth_guard_checks')) {
30
            return null;
31
        }
32
33
        foreach (array_keys(config('auth.guards')) as $guard) {
34
35
            if (auth()->guard($guard)->check()) return $guard;
0 ignored issues
show
Bug introduced by
The method guard does only exist in Illuminate\Contracts\Auth\Factory, but not in Illuminate\Contracts\Auth\Guard.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
36
        }
37
        return null;
38
    }
39
}
40