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
Pull Request — master (#7)
by
unknown
31:07
created

FilterIfPjax::filter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 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
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
            ->filter($response, $request);
39
40
        return $response;
41
    }
42
    
43
    /**
44
     * Easily add extra filters for PJAX requests.
45
     * 
46
     * @param \Illuminate\Http\Response $response
47
     * @param \Illuminate\Http\Request  $request
48
     */
49
    protected function filter(Response $response, Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $response 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...
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...
50
    {
51
        //
52
    }
53
54
    /**
55
     * @param \Illuminate\Http\Response $response
56
     * @param string                    $container
57
     *
58
     * @return $this
59
     */
60
    protected function filterResponse(Response $response, $container)
61
    {
62
        $crawler = $this->getCrawler($response);
63
64
        $response->setContent(
65
            $this->makeTitle($crawler).
66
            $this->fetchContainer($crawler, $container)
67
        );
68
69
        return $this;
70
    }
71
72
    /**
73
     * @param \Symfony\Component\DomCrawler\Crawler $crawler
74
     *
75
     * @return null|string
76
     */
77
    protected function makeTitle(Crawler $crawler)
78
    {
79
        $pageTitle = $crawler->filter('head > title');
80
81
        if (!$pageTitle->count()) {
82
            return;
83
        }
84
85
        return "<title>{$pageTitle->html()}</title>";
86
    }
87
88
    /**
89
     * @param \Symfony\Component\DomCrawler\Crawler $crawler
90
     * @param string                                $container
91
     *
92
     * @return string
93
     */
94
    protected function fetchContainer(Crawler $crawler, $container)
95
    {
96
        $content = $crawler->filter($container);
97
98
        if (!$content->count()) {
99
            abort(422);
100
        }
101
102
        return $content->html();
103
    }
104
105
    /**
106
     * @param \Illuminate\Http\Response $response
107
     * @param \Illuminate\Http\Request  $request
108
     */
109
    protected function setUriHeader(Response $response, Request $request)
110
    {
111
        $response->header('X-PJAX-URL', $request->getRequestUri());
112
113
        return $this;
114
    }
115
116
    /**
117
     * @param \Illuminate\Http\Response $response
118
     * @param \Illuminate\Http\Request  $request
119
     */
120
    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...
121
    {
122
        $crawler = $this->getCrawler($response);
123
        $node = $crawler->filter('head > meta[http-equiv]');
124
125
        if ($node->count()) {
126
            $response->header('X-PJAX-Version', $node->attr('content'));
127
        }
128
129
        return $this;
130
    }
131
132
    /**
133
     * Get the DomCrawler instance.
134
     *
135
     * @param \Illuminate\Http\Response $response
136
     *
137
     * @return \Symfony\Component\DomCrawler\Crawler
138
     */
139
    protected function getCrawler(Response $response)
140
    {
141
        if ($this->crawler) {
142
            return $this->crawler;
143
        }
144
145
        return $this->crawler = new Crawler($response->getContent());
146
    }
147
}
148