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