Completed
Pull Request — master (#52)
by Mostafa Abd El-Salam
13:23
created

Guard   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 49
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

2 Methods

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