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 ( 17370b...216ab3 )
by Benjamin
02:35
created

UrlMatcher   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 52
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A createRegex() 0 14 1
A matchCollection() 0 13 3
A match() 0 6 2
1
<?php
2
3
namespace App\Lib\Routing;
4
5
class UrlMatcher
6
{
7
    /**
8
     * @var RouteCollection
9
     */
10
    private $collection;
11
12 4
    public function __construct(RouteCollection $collection)
13
    {
14 4
        $this->collection = $collection;
15 4
    }
16
17 2
    public function match($path)
18
    {
19 2
        if ($match = $this->matchCollection($path)) {
20 1
            return $match;
21
        }
22 1
        return ['controller' => 'Default', 'action' => 'notFound', 'params' => []];
23
    }
24
25 4
    public function matchCollection($path)
26
    {
27 4
        foreach ($this->collection->all() as $route) {
28 4
            $regex = $this->createRegex($route);
29
30 4
            if (preg_match('#^'.$regex.'$#', $path, $matches)) {
31
                $params = array_filter($matches, function ($key) {
32 2
                    return !is_int($key);
33 2
                }, ARRAY_FILTER_USE_KEY);
34
                return [
35 2
                    'controller' => $route->getDefault('controller'),
36 2
                    'action'     => $route->getDefault('action'),
37 4
                    'params'     => $params
38
                ];
39
            }
40
        }
41 2
    }
42
43 4
    private function createRegex(RouteDefinition $route): string
44
    {
45 4
        $regex = $route->getPathRegex();
46 4
        $reqs  = $route->getRequirements();
47 4
        $regex = preg_replace_callback(
48 4
            '/{(\w+)}/',
49
            function ($matches) use ($reqs) {
50 4
                $req = $reqs[$matches[1]] ?? '.+';
51 4
                return "(?<{$matches[1]}>{$req})";
52 4
            },
53 4
            $regex
54
        );
55
56 4
        return $regex;
57
    }
58
}
59