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