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.

UrlMatcher   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

4 Methods

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