Completed
Push — master ( 8e9a2a...c89276 )
by Freek
01:11
created

DemoModeController::catchFallback()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Spatie\DemoMode;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Http\RedirectResponse;
7
8
class DemoModeController extends \Illuminate\Routing\Controller
9
{
10
    public function grantAccess(): RedirectResponse
11
    {
12
        session()->put('demo_access_route_visited', true);
13
14
        return new RedirectResponse(
15
            config('demo-mode.redirect_authorized_users_to_url')
16
        );
17
    }
18
19
    public function catchFallback(Request $request): RedirectResponse
20
    {
21
        if (! $this->hasDemoAccess($request)) {
22
            return new RedirectResponse(
23
                config('demo-mode.redirect_unauthorized_users_to_url')
24
            );
25
        }
26
27
        abort(404);
28
    }
29
30
    protected function hasDemoAccess(Request $request): bool
31
    {
32
        if (! config('demo-mode.enabled') || in_array($request->ip(), config('demo-mode.authorized_ips')) || auth()->check()) {
0 ignored issues
show
Bug introduced by
The method check does only exist in Illuminate\Contracts\Auth\Guard, but not in Illuminate\Contracts\Auth\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
33
            return true;
34
        }
35
36
        return ! config('demo-mode.strict_mode') && session()->has('demo_access_route_visited');
37
    }
38
}
39