Test Failed
Pull Request — master (#70)
by Mostafa Abd El-Salam
03:31
created

Guard   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 1
dl 0
loc 52
ccs 0
cts 21
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getNames() 0 26 6
A getDefaultName() 0 5 2
1
<?php
2
3
namespace Maklad\Permission;
4
5
use Illuminate\Support\Collection;
6
7
/**
8
 * Class Guard
9
 * @package Maklad\Permission
10
 */
11
class Guard
12
{
13
    /**
14
     * return collection of (guard_name) property if exist on class or object
15
     * otherwise will return collection of guards names that exists in config/auth.php.
16
     *
17
     * @param $model
18
     *
19
     * @return Collection
20
     * @throws \ReflectionException
21
     */
22
    public function getNames($model) : Collection
23
    {
24
        if (\is_object($model)) {
25
            $guardName = $model->guard_name ?? null;
26
        }
27
28
        if (! isset($guardName)) {
29
            $class = \is_object($model) ? \get_class($model) : $model;
30
            $guardName = (new \ReflectionClass($class))->getDefaultProperties()['guard_name'] ?? null;
31
        }
32
33
        if ($guardName) {
34
            return collect($guardName);
35
        }
36
        return collect(config('auth.guards'))
37
            ->map(function ($guard) {
38
                if (! isset($guard['provider'])) {
39
                    return;
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
    /**
50
     * Return Default Guard name
51
     *
52
     * @param $class
53
     *
54
     * @return string
55
     * @throws \ReflectionException
56
     */
57
    public function getDefaultName($class): string
58
    {
59
        $default = config('auth.defaults.guard');
60
        return $this->getNames($class)->first() ?: $default;
61
    }
62
}
63