Completed
Push — master ( 40c217...411b11 )
by Freek
01:16
created

DemoMode::isIpAuthorized()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Spatie\DemoMode;
4
5
use Closure;
6
use Illuminate\Config\Repository;
7
use Illuminate\Http\RedirectResponse;
8
use Illuminate\Http\Request;
9
10
class DemoMode
11
{
12
    /** @var array */
13
    protected $config;
14
15
    public function __construct(Repository $config)
16
    {
17
        $this->config = $config->get('demo-mode');
0 ignored issues
show
Documentation Bug introduced by
It seems like $config->get('demo-mode') of type * is incompatible with the declared type array of property $config.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
18
    }
19
20
    /**
21
     * Handle an incoming request.
22
     *
23
     * @param \Illuminate\Http\Request $request
24
     * @param \Closure                 $next
25
     *
26
     * @return mixed
27
     */
28
    public function handle($request, Closure $next)
29
    {
30
        if (! $this->config['enabled']) {
31
            return $next($request);
32
        }
33
34
        if ($this->protectedByDemoMode($request)) {
35
            if (! $this->hasDemoAccess($request)) {
36
                return new RedirectResponse($this->config['redirect_unauthorized_users_to_url']);
37
            }
38
        }
39
40
        return $next($request);
41
    }
42
43
    protected function protectedByDemoMode(Request $request): bool
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
44
    {
45
        return true;
46
    }
47
48
    protected function hasDemoAccess(Request $request): bool
49
    {
50
        if ($this->isIpAuthorized($request) || 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...
51
            return true;
52
        }
53
54
        return $this->demoRouteEnabled() && session()->has('demo_access_route_visited');
55
    }
56
57
    protected function demoRouteEnabled(): bool
58
    {
59
        return ! $this->config['strict_mode'];
60
    }
61
62
    protected function isIpAuthorized(Request $request): bool
63
    {
64
        return in_array($request->ip(), $this->config['authorized_ips']);
65
    }
66
}
67