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 (#13)
by Dev
01:19
created

RobotsHeaders::getWildCardUserAgent()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 4
nc 3
nop 1
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
        $userAgent = strtolower($userAgent);
45
46
        return
47
            $this->robotHeadersProperties[$userAgent]['noindex']
48
            ?? $this->robotHeadersProperties[$this->getWildCardUserAgent($userAgent)]['noindex']
49
            ?? $this->robotHeadersProperties['*']['noindex']
50
            ?? false;
51
    }
52
53
    public function nofollow(string $userAgent = '*'): bool
54
    {
55
        $userAgent = strtolower($userAgent);
56
57
        return
58
            $this->robotHeadersProperties[$userAgent]['nofollow']
59
            ?? $this->robotHeadersProperties[$this->getWildCardUserAgent($userAgent)]['nofollow']
60
            ?? $this->robotHeadersProperties['*']['nofollow']
61
            ?? false;
62
    }
63
64
    protected function getWildCardUserAgent(string $userAgent): ?string
65
    {
66
        if ($userAgent !== '*') {
67
            for ($i = 1; $i <= strlen($userAgent); $i++) {
68
                $wildCardUserAgent = substr($userAgent, 0, $i).'*';
69
                if (isset($this->robotHeadersProperties[$wildCardUserAgent])) {
70
                    return $wildCardUserAgent;
71
                }
72
            }
73
        }
74
75
        return null;
76
    }
77
78
    protected function parseHeaders(array $headers): array
79
    {
80
        $robotHeaders = $this->filterRobotHeaders($headers);
81
82
        return array_reduce($robotHeaders, function (array $parsedHeaders, $header) {
83
            $header = $this->normalizeHeaders($header);
84
85
            $headerParts = explode(':', $header);
86
87
            $userAgent = count($headerParts) === 3
88
                ? trim($headerParts[1])
89
                : '*';
90
91
            $userAgent = strtolower($userAgent);
92
            $options = end($headerParts);
93
94
            $parsedHeaders[$userAgent] = [];
95
96
            if (strpos(strtolower($options), 'index')) {
97
                $parsedHeaders[$userAgent]['noindex'] = strpos(strtolower($options), 'noindex') !== false;
98
            }
99
100
            if (strpos(strtolower($options), 'follow')) {
101
                $parsedHeaders[$userAgent]['nofollow'] = strpos(strtolower($options), 'nofollow') !== false;
102
            }
103
104
            return $parsedHeaders;
105
        }, []);
106
    }
107
108
    protected function filterRobotHeaders(array $headers): array
109
    {
110
        return array_filter($headers, function ($header) use ($headers) {
111
            $headerContent = $this->normalizeHeaders($headers[$header] ?? []);
112
113
            return strpos(strtolower($header), 'x-robots-tag') === 0
114
                || strpos(strtolower($headerContent), 'x-robots-tag') === 0;
115
        }, ARRAY_FILTER_USE_KEY);
116
    }
117
118
    protected function normalizeHeaders($headers): string
119
    {
120
        return implode(',', (array) $headers);
121
    }
122
}
123