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 ( e40910...de9df1 )
by Freek
02:00
created

addCookieConsentScriptToResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 8
nc 1
nop 1
1
<?php
2
3
namespace Spatie\CookieConsent;
4
5
use Closure;
6
use Illuminate\Http\Response;
7
use Illuminate\Contracts\Foundation\Application;
8
9
class CookieConsentMiddleware
10
{
11
    /** @var \Illuminate\Contracts\Foundation\Application */
12
    protected $app;
13
14
    public function __construct(Application $app)
15
    {
16
        $this->app = $app;
17
    }
18
19
    /**
20
     * @param  \Illuminate\Http\Request $request
21
     * @param  \Closure $next
22
     *
23
     * @return mixed
24
     *
25
     * @throws \Symfony\Component\HttpKernel\Exception\HttpException
26
     */
27
    public function handle($request, Closure $next)
28
    {
29
        $response = $next($request);
30
31
        if (!$response instanceof Response) {
32
            return $response;
33
        }
34
35
        if (! $this->containsBodyTag($response)) {
36
            return $response;
37
        }
38
39
        return $this->addCookieConsentScriptToResponse($response);
40
    }
41
42
    protected function containsBodyTag(Response $response): bool
43
    {
44
        return $this->getLastClosingBodyTagPosition($response->getContent()) !== false;
45
    }
46
47
    protected function addCookieConsentScriptToResponse(Response $response): Response
48
    {
49
        $content = $response->getContent();
50
51
        $closingBodyTagPosition = $this->getLastClosingBodyTagPosition($content);
52
53
        $content = ''
54
            . substr($content, 0, $closingBodyTagPosition)
55
            . 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...
56
            . substr($content, $closingBodyTagPosition);
57
58
        return $response->setContent($content);
59
    }
60
61
62
    /**
63
     * @param string $content
64
     *
65
     * @return int|bool
66
     */
67
    protected function getLastClosingBodyTagPosition(string $content = '')
68
    {
69
        return strripos($content, '</body>');
70
    }
71
}
72