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 ( cc1a95...484e84 )
by Freek
06:04
created

FilterIfPjax   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 12
c 4
b 0
f 1
lcom 1
cbo 3
dl 0
loc 121
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 14 3
A filterResponse() 0 11 1
A makeTitle() 0 6 1
A fetchContainer() 0 10 2
A setUriHeader() 0 6 1
A setVersionHeader() 0 11 2
A getCrawler() 0 8 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
10
class FilterIfPjax
11
{
12
    /**
13
     * The DomCrawler instance.
14
     * 
15
     * @var \Symfony\Component\DomCrawler\Crawler
16
     */
17
    protected $crawler;
18
19
    /**
20
     * Handle an incoming request.
21
     *
22
     * @param \Illuminate\Http\Request $request
23
     * @param \Closure                 $next
24
     *
25
     * @return mixed
26
     */
27
    public function handle(Request $request, Closure $next)
28
    {
29
        $response = $next($request);
30
31
        if (! $request->pjax() || $response->isRedirection()) {
32
            return $response;
33
        }
34
35
        $this->filterResponse($response, $request->header('X-PJAX-CONTAINER'))
0 ignored issues
show
Bug introduced by
It seems like $request->header('X-PJAX-CONTAINER') targeting Illuminate\Http\Request::header() can also be of type array; however, Spatie\Pjax\Middleware\F...fPjax::filterResponse() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
36
            ->setUriHeader($response, $request)
37
            ->setVersionHeader($response, $request);
38
39
        return $response;
40
    }
41
42
    /**
43
     * @param \Illuminate\Http\Response $response
44
     * @param string                    $container
45
     *
46
     * @return $this
47
     */
48
    protected function filterResponse(Response $response, $container)
49
    {
50
        $crawler = $this->getCrawler($response);
51
52
        $response->setContent(
53
            $this->makeTitle($crawler).
54
            $this->fetchContainer($crawler, $container)
55
        );
56
57
        return $this;
58
    }
59
60
    /**
61
     * @param \Symfony\Component\DomCrawler\Crawler $crawler
62
     *
63
     * @return string
64
     */
65
    protected function makeTitle(Crawler $crawler)
66
    {
67
        $pageTitle = $crawler->filter('head > title')->html();
68
69
        return "<title>{$pageTitle}</title>";
70
    }
71
72
    /**
73
     * @param \Symfony\Component\DomCrawler\Crawler $crawler
74
     * @param string                                $container
75
     *
76
     * @return string
77
     */
78
    protected function fetchContainer(Crawler $crawler, $container)
79
    {
80
        $content = $crawler->filter($container);
81
82
        if (! $content->count()) {
83
            abort(422);
84
        }
85
86
        return $content->html();
87
    }
88
89
    /**
90
     * @param \Illuminate\Http\Response $response
91
     * @param \Illuminate\Http\Request  $request
92
     */
93
    protected function setUriHeader(Response $response, Request $request)
94
    {
95
        $response->header('X-PJAX-URL', $request->getRequestUri());
96
97
        return $this;
98
    }
99
100
    /**
101
     * @param \Illuminate\Http\Response $response
102
     * @param \Illuminate\Http\Request  $request
103
     */
104
    protected function setVersionHeader(Response $response, Request $request)
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...
105
    {
106
        $crawler = $this->getCrawler($response);
107
        $node = $crawler->filter('head > meta[http-equiv]');
108
109
        if ($node->count()) {
110
            $response->header('X-PJAX-VERSION', $node->attr('content'));
111
        }
112
113
        return $this;
114
    }
115
116
    /**
117
     * Get the DomCrawler instance.
118
     *
119
     * @param \Illuminate\Http\Response $response
120
     * @return \Symfony\Component\DomCrawler\Crawler
121
     */
122
    protected function getCrawler(Response $response)
123
    {
124
        if ($this->crawler) {
125
            return $this->crawler;
126
        }
127
128
        return $this->crawler = new Crawler($response->getContent());
129
    }
130
}
131