| Conditions | 6 |
| Paths | 12 |
| Total Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 42 |
| Changes | 0 | ||
| 1 | <?php |
||
| 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) { |
||
|
|
|||
| 44 | return $class === $model; |
||
| 45 | }) |
||
| 46 | ->keys(); |
||
| 47 | } |
||
| 48 | |||
| 63 |
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: