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 (#17)
by Benjamin
58s
created

RobotsTxt::parseDisallow()   A

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
class RobotsTxt
6
{
7
    protected static $robotsCache = [];
8
9
    protected $disallowsPerUserAgent = [];
10
11
    public static function readFrom(string $source): self
12
    {
13
        $content = @file_get_contents($source);
14
15
        return new self($content !== false ? $content : '');
16
    }
17
18
    public function __construct(string $content)
19
    {
20
        $this->disallowsPerUserAgent = $this->getDisallowsPerUserAgent($content);
21
    }
22
23
    public static function create(string $source): self
24
    {
25
        if (
26
            strpos($source, 'http') !== false
27
            && strpos($source, 'robots.txt') !== false
28
        ) {
29
            return self::readFrom($source);
30
        }
31
32
        return new self($source);
33
    }
34
35
    public function allows(string $url, ?string $userAgent = '*'): bool
36
    {
37
        $requestUri = '';
38
39
        $url = parse_url($url);
40
41
        if ($url !== false) {
42
            if (isset($url['path'])) {
43
                $requestUri .= $url['path'];
44
            }
45
46
            if (isset($url['query'])) {
47
                $requestUri .= '?' . $url['query'];
48
            }
49
        }
50
51
        $disallows = $this->disallowsPerUserAgent[$userAgent] ?? $this->disallowsPerUserAgent['*'] ?? [];
52
53
        return ! $this->pathIsDenied($requestUri, $disallows);
54
    }
55
56
    protected function pathIsDenied(string $requestUri, array $disallows): bool
57
    {
58
        foreach ($disallows as $disallow) {
59
            if ($disallow === '') {
60
                continue;
61
            }
62
63
            $stopAtEndOfString = false;
64
65
            if ($disallow[-1] === '$') {
66
                // if the pattern ends with a dollar sign, the string must end there
67
                $disallow = substr($disallow, 0, -1);
68
                $stopAtEndOfString = true;
69
            }
70
71
            // convert to regexp
72
            $disallowRegexp = preg_quote($disallow, '/');
73
74
            // the pattern must start at the beginning of the string...
75
            $disallowRegexp = '^' . $disallowRegexp;
76
77
            // ...and optionally stop at the end of the string
78
            if ($stopAtEndOfString) {
79
                $disallowRegexp .= '$';
80
            }
81
82
            // replace (preg_quote'd) stars with an eager match
83
            $disallowRegexp = str_replace('\\*', '.*', $disallowRegexp);
84
85
            // enclose in delimiters
86
            $disallowRegexp = '/' . $disallowRegexp . '/';
87
88
            if (preg_match($disallowRegexp, $requestUri) === 1) {
89
                return true;
90
            }
91
        }
92
93
        return false;
94
    }
95
96
    protected function getDisallowsPerUserAgent(string $content): array
97
    {
98
        $lines = explode(PHP_EOL, $content);
99
100
        $lines = array_filter($lines);
101
102
        $disallowsPerUserAgent = [];
103
104
        $currentUserAgent = null;
105
106
        foreach ($lines as $line) {
107
            if ($this->isCommentLine($line)) {
108
                continue;
109
            }
110
111
            if ($this->isUserAgentLine($line)) {
112
                $disallowsPerUserAgent[$this->parseUserAgent($line)] = [];
113
114
                $currentUserAgent = &$disallowsPerUserAgent[$this->parseUserAgent($line)];
115
116
                continue;
117
            }
118
119
            if ($currentUserAgent === null) {
120
                continue;
121
            }
122
123
            $disallowUrl = $this->parseDisallow($line);
124
125
            $currentUserAgent[$disallowUrl] = $disallowUrl;
126
        }
127
128
        return $disallowsPerUserAgent;
129
    }
130
131
    protected function isCommentLine(string $line): bool
132
    {
133
        return strpos(trim($line), '#') === 0;
134
    }
135
136
    protected function isUserAgentLine(string $line): bool
137
    {
138
        return strpos(trim(strtolower($line)), 'user-agent') === 0;
139
    }
140
141
    protected function parseUserAgent(string $line): string
142
    {
143
        return trim(str_replace('user-agent', '', strtolower(trim($line))), ': ');
144
    }
145
146
    protected function parseDisallow(string $line): string
147
    {
148
        return trim(substr_replace(strtolower(trim($line)), '', 0, 8), ': ');
149
    }
150
151
    /**
152
     * @deprecated
153
     */
154
    protected function concernsDirectory(string $path): bool
155
    {
156
        return substr($path, strlen($path) - 1, 1) === '/';
157
    }
158
159
    /**
160
     * @deprecated
161
     */
162
    protected function isUrlInDirectory(string $url, string $path): bool
163
    {
164
        return strpos($url, $path) === 0;
165
    }
166
}
167