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
Push — master ( d93276...d734fb )
by Sebastian
02:26
created

ActiveUrlChecker   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 41
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B check() 0 38 5
1
<?php
2
3
namespace Spatie\Menu;
4
5
use Spatie\Menu\Helpers\Str;
6
use Spatie\Url\Url;
7
8
class ActiveUrlChecker
9
{
10
    public static function check(string $url, string $requestUrl, string $rootUrl = '/'): bool
11
    {
12
        $url = Url::fromString($url);
13
        $requestUrl = Url::fromString($requestUrl);
14
15
        // If the hosts don't match, this url isn't active.
16
        if ($url->getHost() !== $requestUrl->getHost()) {
17
            return false;
18
        }
19
20
        $rootUrl = Str::ensureLeft('/', $rootUrl);
21
22
        // All paths used in this method should be terminated by a /
23
        // otherwise startsWith at the end will be too greedy and
24
        // also matches items which are on the same level
25
        $rootUrl = Str::ensureRight('/', $rootUrl);
26
27
        $itemPath = Str::ensureRight('/', $url->getPath());
28
29
        // If this url doesn't start with the rootUrl, it's inactive.
30
        if (! Str::startsWith($itemPath, $rootUrl)) {
31
            return false;
32
        }
33
34
        $matchPath = Str::ensureRight('/', $requestUrl->getPath());
35
36
        // For the next comparisons we just need the paths, and we'll remove
37
        // the rootUrl first.
38
        $itemPath = Str::removeFromStart($rootUrl, $itemPath);
39
        $matchPath = Str::removeFromStart($rootUrl, $matchPath);
40
41
        // If this url starts with the url we're matching with, it's active.
42
        if ($matchPath === $itemPath || Str::startsWith($matchPath, $itemPath)) {
43
            return true;
44
        }
45
46
        return false;
47
    }
48
}
49