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 (#3)
by
unknown
01:59
created

FilterIfPjax::getCrawler()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4286
cc 2
eloc 4
nc 2
nop 1
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
     * Handle an incoming request.
14
     *
15
     * @param \Illuminate\Http\Request $request
16
     * @param \Closure                 $next
17
     *
18
     * @return mixed
19
     */
20
    public function handle(Request $request, Closure $next)
21
    {
22
        $response = $next($request);
23
24
        if (! $request->pjax() || $response->isRedirection()) {
25
            return $response;
26
        }
27
28
        $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...
29
            ->setUriHeader($response, $request)
30
            ->setVersionHeader($response, $request);
31
32
        return $response;
33
    }
34
35
    /**
36
     * @param \Illuminate\Http\Response $response
37
     * @param string                    $container
38
     *
39
     * @return $this
40
     */
41
    protected function filterResponse(Response $response, $container)
42
    {
43
        $crawler = $this->getCrawler($response);
44
45
        $response->setContent(
46
            $this->makeTitle($crawler).
47
            $this->fetchContainer($crawler, $container)
48
        );
49
50
        return $this;
51
    }
52
53
    /**
54
     * @param \Symfony\Component\DomCrawler\Crawler $crawler
55
     *
56
     * @return string
57
     */
58
    protected function makeTitle(Crawler $crawler)
59
    {
60
        $pageTitle = $crawler->filter('head > title')->html();
61
62
        return "<title>{$pageTitle}</title>";
63
    }
64
65
    /**
66
     * @param \Symfony\Component\DomCrawler\Crawler $crawler
67
     * @param string                                $container
68
     *
69
     * @return string
70
     */
71
    protected function fetchContainer(Crawler $crawler, $container)
72
    {
73
        $content = $crawler->filter($container);
74
75
        if (! $content->count()) {
76
            abort(422);
77
        }
78
79
        return $content->html();
80
    }
81
82
    /**
83
     * @param \Illuminate\Http\Response $response
84
     * @param \Illuminate\Http\Request  $request
85
     */
86
    protected function setUriHeader(Response $response, Request $request)
87
    {
88
        $response->header('X-PJAX-URL', $request->getRequestUri());
89
90
        return $this;
91
    }
92
93
    /**
94
     * @param \Illuminate\Http\Response $response
95
     * @param \Illuminate\Http\Request  $request
96
     */
97
    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...
98
    {
99
        $crawler = $this->getCrawler($response);
100
        $node = $crawler->filter('head > meta[http-equiv]');
101
102
        if ($node->count()) {
103
            $response->header('X-PJAX-VERSION', $node->attr('content'));
104
        }
105
106
        return $this;
107
    }
108
109
    /**
110
     * Get the DomCrawler instance.
111
     *
112
     * @param \Illuminate\Http\Response $response
113
     * @return \Symfony\Component\DomCrawler\Crawler
114
     */
115
    protected function getCrawler(Response $response)
116
    {
117
        if ($this->crawler) {
118
            return $this->crawler;
0 ignored issues
show
Bug introduced by
The property crawler does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
119
        }
120
121
        return new Crawler($response->getContent());
122
    }
123
}
124