GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 3d1bbe...e64a0a )
by Freek
02:07 queued 54s
created

CookieConsentMiddleware::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\CookieConsent;
4
5
use Closure;
6
use Illuminate\Http\Response;
7
8
class CookieConsentMiddleware
9
{
10
    public function handle($request, Closure $next)
11
    {
12
        $response = $next($request);
13
14
        if (! $response instanceof Response) {
15
            return $response;
16
        }
17
18
        if (! $this->containsBodyTag($response)) {
19
            return $response;
20
        }
21
22
        return $this->addCookieConsentScriptToResponse($response);
23
    }
24
25
    protected function containsBodyTag(Response $response): bool
26
    {
27
        return $this->getLastClosingBodyTagPosition($response->getContent()) !== false;
28
    }
29
30
    /**
31
     * @param \Illuminate\Http\Response $response
32
     *
33
     * @return $this
34
     */
35
    protected function addCookieConsentScriptToResponse(Response $response)
36
    {
37
        $content = $response->getContent();
38
39
        $closingBodyTagPosition = $this->getLastClosingBodyTagPosition($content);
40
41
        $content = ''
42
            .substr($content, 0, $closingBodyTagPosition)
43
            .view('cookieConsent::index')->render()
0 ignored issues
show
Bug introduced by
The method render does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\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...
44
            .substr($content, $closingBodyTagPosition);
45
46
        return $response->setContent($content);
47
    }
48
49
    protected function getLastClosingBodyTagPosition(string $content = '')
50
    {
51
        return strripos($content, '</body>');
52
    }
53
}
54