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.

FilterIfPjax::filterResponse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Spatie\Pjax\Middleware;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
use Illuminate\Http\Response;
8
use Symfony\Component\DomCrawler\Crawler;
9
use Symfony\Component\HttpFoundation\Response as BaseResponse;
10
11
class FilterIfPjax
12
{
13
    /** @var \Symfony\Component\DomCrawler\Crawler */
14
    protected $crawler;
15
16
    public function handle(Request $request, Closure $next): BaseResponse
17
    {
18
        $response = $next($request);
19
20
        if (! $request->pjax() || $response->isRedirection()) {
21
            return $response;
22
        }
23
24
        $this->filterResponse($response, $request->header('X-PJAX-Container'))
25
            ->setUriHeader($response, $request)
26
            ->setVersionHeader($response, $request);
27
28
        return $response;
29
    }
30
31
    protected function filterResponse(BaseResponse $response, $container): self
32
    {
33
        $crawler = $this->getCrawler($response);
34
35
        $response->setContent(
36
            $this->makeTitle($crawler).
37
            $this->fetchContainer($crawler, $container)
38
        );
39
40
        return $this;
41
    }
42
43
    protected function makeTitle(Crawler $crawler): ?string
44
    {
45
        $pageTitle = $crawler->filter('head > title');
46
47
        if (! $pageTitle->count()) {
48
            return null;
49
        }
50
51
        return "<title>{$pageTitle->html()}</title>";
52
    }
53
54
    protected function fetchContainer(Crawler $crawler, $container): string
55
    {
56
        $content = $crawler->filter($container);
57
58
        if (! $content->count()) {
59
            abort(422);
60
        }
61
62
        return $content->html();
63
    }
64
65
    protected function setUriHeader(Response $response, Request $request): self
66
    {
67
        $response->header('X-PJAX-URL', $request->getRequestUri());
68
69
        return $this;
70
    }
71
72
    protected function setVersionHeader(Response $response, Request $request): self
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...
73
    {
74
        $crawler = $this->getCrawler($this->createResponseWithLowerCaseContent($response));
75
        $node = $crawler->filter('head > meta[http-equiv="x-pjax-version"]');
76
77
        if ($node->count()) {
78
            $response->header('x-pjax-version', $node->attr('content'));
79
        }
80
81
        return $this;
82
    }
83
84
    protected function getCrawler(BaseResponse $response): Crawler
85
    {
86
        if ($this->crawler) {
87
            return $this->crawler;
88
        }
89
90
        return $this->crawler = new Crawler($response->getContent());
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, Symfony\Component\DomCra...\Crawler::__construct() does only seem to accept object<DOMNodeList>|obje...t<DOMNode>>|string|null, did you maybe forget to handle an error condition?
Loading history...
91
    }
92
93
    protected function createResponseWithLowerCaseContent(Response $response): Response
94
    {
95
        $lowercaseContent = strtolower($response->getContent());
96
97
        return Response::create($lowercaseContent);
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\HttpFo...tion\Response::create() has been deprecated with message: since Symfony 5.1, use __construct() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
98
    }
99
}
100