Conditions | 5 |
Paths | 12 |
Total Lines | 23 |
Code Lines | 14 |
Lines | 0 |
Ratio | 0 % |
Tests | 16 |
CRAP Score | 5 |
Changes | 0 |
1 | <?php |
||
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) { |
|
|
|||
47 | 121 | return $class === $model; |
|
48 | 121 | }) |
|
49 | 121 | ->keys(); |
|
50 | } |
||
51 | |||
66 |
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:
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
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: