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.

CookieConsentMiddleware   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 50
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 18 4
A containsBodyTag() 0 4 1
A addCookieConsentScriptToResponse() 0 13 1
A getLastClosingBodyTagPosition() 0 4 1
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 (! config('cookie-consent.enabled')) {
15
            return $response;
16
        }
17
18
        if (! $response instanceof Response) {
19
            return $response;
20
        }
21
22
        if (! $this->containsBodyTag($response)) {
23
            return $response;
24
        }
25
26
        return $this->addCookieConsentScriptToResponse($response);
27
    }
28
29
    protected function containsBodyTag(Response $response): bool
30
    {
31
        return $this->getLastClosingBodyTagPosition($response->getContent()) !== false;
0 ignored issues
show
Security Bug introduced by
It seems like $response->getContent() targeting Symfony\Component\HttpFo...\Response::getContent() can also be of type false; however, Spatie\CookieConsent\Coo...losingBodyTagPosition() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
32
    }
33
34
    /**
35
     * @param \Illuminate\Http\Response $response
36
     *
37
     * @return $this
38
     */
39
    protected function addCookieConsentScriptToResponse(Response $response)
40
    {
41
        $content = $response->getContent();
42
43
        $closingBodyTagPosition = $this->getLastClosingBodyTagPosition($content);
0 ignored issues
show
Security Bug introduced by
It seems like $content defined by $response->getContent() on line 41 can also be of type false; however, Spatie\CookieConsent\Coo...losingBodyTagPosition() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
44
45
        $content = ''
46
            .substr($content, 0, $closingBodyTagPosition)
47
            .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...
48
            .substr($content, $closingBodyTagPosition);
49
50
        return $response->setContent($content);
51
    }
52
53
    protected function getLastClosingBodyTagPosition(string $content = '')
54
    {
55
        return strripos($content, '</body>');
56
    }
57
}
58