Conditions | 5 |
Paths | 12 |
Total Lines | 25 |
Code Lines | 14 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
15 | public static function getNames($model) : Collection |
||
16 | { |
||
17 | if (is_object($model)) { |
||
18 | $guardName = $model->guard_name ?? null; |
||
19 | } |
||
20 | |||
21 | if (! isset($guardName)) { |
||
22 | $class = is_object($model) ? get_class($model) : $model; |
||
23 | |||
24 | $guardName = (new \ReflectionClass($class))->getDefaultProperties()['guard_name'] ?? null; |
||
25 | } |
||
26 | |||
27 | if ($guardName) { |
||
28 | return collect($guardName); |
||
29 | } |
||
30 | |||
31 | return collect(config('auth.guards')) |
||
32 | ->map(function ($guard) { |
||
33 | return config("auth.providers.{$guard['provider']}.model"); |
||
34 | }) |
||
35 | ->filter(function ($model) use ($class) { |
||
|
|||
36 | return $class === $model; |
||
37 | }) |
||
38 | ->keys(); |
||
39 | } |
||
40 | |||
48 |
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: