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::matchCollection()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 1
dl 0
loc 14
ccs 9
cts 9
cp 1
crap 3
rs 9.9332
c 0
b 0
f 0
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