Completed
Push — master ( c89276...395830 )
by Freek
01:06
created

DemoGuard::isIpAuthorized()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Spatie\DemoMode;
4
5
use Illuminate\Http\Request;
6
7
class DemoGuard
8
{
9
    public function hasDemoAccess(Request $request): bool
10
    {
11
        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...
12
            return true;
13
        }
14
15
        return $this->demoRouteEnabled() && session()->has('demo_access_route_visited');
16
    }
17
18
    protected function demoRouteEnabled(): bool
19
    {
20
        return ! config('demo-mode.strict_mode');
21
    }
22
23
    protected function isIpAuthorized(Request $request): bool
24
    {
25
        return in_array($request->ip(), config('demo-mode.authorized_ips'));
26
27
    }
28
}