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.

RobotsHeaders::mayIndex()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Robots;
4
5
use InvalidArgumentException;
6
7
class RobotsHeaders
8
{
9
    protected $robotHeadersProperties = [];
10
11
    public static function readFrom(string $source): self
12
    {
13
        $content = @file_get_contents($source);
14
15
        if ($content === false) {
16
            throw new InvalidArgumentException("Could not read from source `{$source}`");
17
        }
18
19
        return new self($http_response_header ?? []);
20
    }
21
22
    public static function create(array $headers): self
23
    {
24
        return new self($headers);
25
    }
26
27
    public function __construct(array $headers)
28
    {
29
        $this->robotHeadersProperties = $this->parseHeaders($headers);
30
    }
31
32
    public function mayIndex(string $userAgent = '*'): bool
33
    {
34
        return ! $this->noindex($userAgent);
35
    }
36
37
    public function mayFollow(string $userAgent = '*'): bool
38
    {
39
        return ! $this->nofollow($userAgent);
40
    }
41
42
    public function noindex(string $userAgent = '*'): bool
43
    {
44
        return
45
            $this->robotHeadersProperties[$userAgent]['noindex']
46
            ?? $this->robotHeadersProperties['*']['noindex']
47
            ?? false;
48
    }
49
50
    public function nofollow(string $userAgent = '*'): bool
51
    {
52
        return
53
            $this->robotHeadersProperties[$userAgent]['nofollow']
54
            ?? $this->robotHeadersProperties['*']['nofollow']
55
            ?? false;
56
    }
57
58
    protected function parseHeaders(array $headers): array
59
    {
60
        $robotHeaders = $this->filterRobotHeaders($headers);
61
62
        return array_reduce($robotHeaders, function (array $parsedHeaders, $header) {
63
            $header = $this->normalizeHeaders($header);
64
65
            $headerParts = explode(':', $header);
66
67
            $userAgent = count($headerParts) === 3
68
                ? trim($headerParts[1])
69
                : '*';
70
71
            $options = end($headerParts);
72
73
            $parsedHeaders[$userAgent] = [
74
                'noindex' => strpos(strtolower($options), 'noindex') !== false,
75
                'nofollow' => strpos(strtolower($options), 'nofollow') !== false,
76
            ];
77
78
            return $parsedHeaders;
79
        }, []);
80
    }
81
82
    protected function filterRobotHeaders(array $headers): array
83
    {
84
        return array_filter($headers, function ($header) use ($headers) {
85
            $headerContent = $this->normalizeHeaders($headers[$header] ?? []);
86
87
            return strpos(strtolower($header), 'x-robots-tag') === 0
88
                || strpos(strtolower($headerContent), 'x-robots-tag') === 0;
89
        }, ARRAY_FILTER_USE_KEY);
90
    }
91
92
    protected function normalizeHeaders($headers): string
93
    {
94
        return implode(',', (array) $headers);
95
    }
96
}
97