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

DemoModeController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 0
cbo 3
dl 0
loc 31
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A grantAccess() 0 8 1
A catchFallback() 0 10 2
B hasDemoAccess() 0 8 5
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