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